aboutsummaryrefslogtreecommitdiffstats
path: root/_libly.js
diff options
context:
space:
mode:
authoranekos2009-12-29 05:29:43 +0000
committeranekos2009-12-29 05:29:43 +0000
commita611f1c6ff91dc1fbea8adc5fbfe6c2dcf16ab78 (patch)
treedfa04808f94256cf681e4578cff6058602447dc9 /_libly.js
parent6087dbc76a62fe81aa30ce7fc1f6f05c8ed98dc3 (diff)
downloadvimperator-plugins-a611f1c6ff91dc1fbea8adc5fbfe6c2dcf16ab78.tar.bz2
$U.around に二重包み込み防止機能をつけた
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@36303 d0d07461-0603-4401-acd4-de1884942a52
Diffstat (limited to '_libly.js')
-rwxr-xr-x_libly.js62
1 files changed, 49 insertions, 13 deletions
diff --git a/_libly.js b/_libly.js
index c7f6e7c..11d3fa8 100755
--- a/_libly.js
+++ b/_libly.js
@@ -12,7 +12,7 @@ var PLUGIN_INFO =
<description lang="ja">適当なライブラリっぽいものたち。</description>
<author mail="suvene@zeromemory.info" homepage="http://zeromemory.sblo.jp/">suVene</author>
<license>MIT</license>
- <version>0.1.29</version>
+ <version>0.1.30</version>
<minVersion>2.3pre</minVersion>
<maxVersion>2.3pre</maxVersion>
<updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/_libly.js</updateURL>
@@ -34,7 +34,7 @@ extend(dst, src):
オブジェクトを拡張します。
A(iterable):
オブジェクトを配列にします。
-around(obj, name, func):
+around(obj, name, func, autoRestore):
obj がもつ name 関数を、func に置き換えます。
func は
function (next, args) {...}
@@ -43,6 +43,8 @@ around(obj, name, func):
args はオリジナルの引数列です。
通常、next には引数を渡す必要はありません。
(任意の引数を渡したい場合は配列で渡します。)
+ また、autoRestore が真であれば、プラグインの再ロードなどで around が再実行されたときに、関数の置き換え前にオリジナル状態に書き戻します。
+ (多重に置き換えられなくなるので、auto_source.js などを使ったプラグイン開発で便利です)
返値は以下のオブジェクトです
>||
{
@@ -232,18 +234,52 @@ libly.$U = {//{{{
}
return ret;
},
- around: function around (obj, name, func) {
- let next = obj[name];
- let current = obj[name] = function () {
- let self = this, args = arguments;
- return func.call(self, function (_args) next.apply(self, _args || args), args);
+ around: (function () {
+ function getPluginPath () {
+ let pluginPath;
+ Error('hoge').stack.split(/\n/).some(
+ function (s)
+ let (m = s.match(/^\(\)@chrome:\/\/liberator\/content\/liberator\.js -> (.+):\d+$/))
+ (m && (pluginPath = m[1]))
+ );
+ return pluginPath;
+ }
+
+ let restores = {};
+
+ return function (obj, name, func, autoRestore) {
+ let original;
+ let restore = function () obj[name] = original;
+ if (autoRestore) {
+ let pluginPath = getPluginPath();
+ if (!pluginPath)
+ throw 'getPluginPath failed';
+ restores[pluginPath] =
+ (restores[pluginPath] || []).filter(
+ function (res) (
+ res.object != obj ||
+ res.name != name ||
+ (res.restore() && false)
+ )
+ );
+ restores[pluginPath].push({
+ object: obj,
+ name: name,
+ restore: restore
+ });
+ }
+ original = obj[name];
+ let current = obj[name] = function () {
+ let self = this, args = arguments;
+ return func.call(self, function (_args) original.apply(self, _args || args), args);
+ };
+ return libly.$U.extend({
+ original: original,
+ current: current,
+ restore: restore
+ }, [original, current]);
};
- return libly.$U.extend({
- original: next,
- current: current,
- restore: function () (obj[name] = next)
- }, [next, current]);
- },
+ })(),
bind: function(obj, func) {
return function() {
return func.apply(obj, arguments);