diff options
author | anekos | 2012-08-16 20:40:36 +0900 |
---|---|---|
committer | anekos | 2012-08-16 20:40:36 +0900 |
commit | 5c08fa58051c8315c042e193a1049e1b9f6f58e3 (patch) | |
tree | 4fce58273cfd034f48714fe632f1ae96d3acae8c /twittperator.js | |
parent | c4c6a2f0df63d0c9eced030b0bd87af0dac8f027 (diff) | |
download | vimperator-plugins-5c08fa58051c8315c042e193a1049e1b9f6f58e3.tar.bz2 |
Implement "require" method.
Diffstat (limited to 'twittperator.js')
-rw-r--r-- | twittperator.js | 78 |
1 files changed, 51 insertions, 27 deletions
diff --git a/twittperator.js b/twittperator.js index 76e4023..651afb0 100644 --- a/twittperator.js +++ b/twittperator.js @@ -1770,6 +1770,9 @@ let INFO = } xhr.send(null); }, // }}} + getPluginNameFromFile: function(file) { // {{{ + return file.leafName.replace(/\..*/, ""); + }, // }}} }; // }}} let Twittperator = { // {{{ activitySummary: function(id) { // {{{ @@ -1838,20 +1841,33 @@ let INFO = let (name = file.leafName.replace(/\..*/, "").replace(/-/g, "_")) liberator.globalVariables["twittperator_plugin_" + name]; - function loadPluginFromDir(checkGV) { + function loadPluginFromDir(checkGV, candidates) { return function(dir) { dir.readDirectory().forEach(function(file) { - if (/\.tw$/.test(file.path) && (!checkGV || isEnabled(file))) - Twittperator.sourceScriptFile(file); + if (/\.tw$/.test(file.path) && (!checkGV || isEnabled(file))) { + if (candidates) { + self._plugins.candidates[Utils.getPluginNameFromFile(file)] = file; + } else { + Twittperator.sourceScriptFile(file); + } + } }); } } + let self = this; + this._plugins = { + candidates: {}, + loaded: {} + }; + ChirpUserStream.clearPluginData(); TrackingStream.clearPluginData(); - io.getRuntimeDirectories("plugin/twittperator").forEach(loadPluginFromDir(true)); - io.getRuntimeDirectories("twittperator").forEach(loadPluginFromDir(false)); + for (let [, c] in Iterator([true, false])) { + io.getRuntimeDirectories("plugin/twittperator").forEach(loadPluginFromDir(true, c)); + io.getRuntimeDirectories("twittperator").forEach(loadPluginFromDir(false, c)); + } }, // }}} lookupUser: function(users) { // {{{ function showUsersInfo(json) { // {{{ @@ -2082,31 +2098,39 @@ let INFO = }); }, // }}} sourceScriptFile: function(file) { // {{{ - // XXX 悪い子向けのハックです。すみません。 *.tw ファイルを *.js のように読み込みます。 - let script = liberator.plugins.contexts[file.path]; - - let originalPath = file.path; - let hackedPath = originalPath.replace(/\.tw$/, ".js"); - - let ugly = { - __noSuchMethod__: function (name, args) originalPath[name].apply(originalPath, args), - toString: function() { - function isFile (caller) { - if (!caller) - return false; - if (caller === io.File) - return true; - return isFile(caller.caller); - } - return isFile(arguments.callee.caller) ? originalPath : hackedPath; - } - }; + let self = this; + + if (self._plugins.loaded[Utils.getPluginNameFromFile(file)]) + return true; + + let stdScript = liberator.plugins.contexts[file.path]; try { - io.source(ugly, false); + let script = Script(file); + + script.__context__.require = function (names) { + if (!(names instanceof Array)) + names = [names]; + names.forEach(function (name) { + if (self._plugins.loaded[name]) + return; + let lib = self._plugins.candidates[name]; + if (lib) + self.sourceScriptFile(lib); + else + throw "Not found twittperator plugin: " + name; + }); + }; + + let uri = services.get("io").newFileURI(file); + let suffix = '?' + encodeURIComponent(services.get("UUID").generateUUID().toString()); + + liberator.loadScript(uri.spec + suffix, script); + self._plugins.loaded[Utils.getPluginNameFromFile(file)] = script; + } finally { - if (script) - liberator.plugins[script.NAME] = script; + if (stdScript) + liberator.plugins[stdScript.NAME] = stdScript; } }, // }}} withProtectedUserConfirmation: function(check, actionName, action) { // {{{ |