X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=server%2Fplugininterface.js;h=3810bc0fad80827e6f5b9f817a73e676615875cb;hb=a885d1c9104a578a6667b230841e3466abd53fdb;hp=070d05d381a12bc6f6250fe1277850dc50548f10;hpb=2aefb4ed627476849fea95dad9455dfd04375345;p=KiwiIRC.git diff --git a/server/plugininterface.js b/server/plugininterface.js index 070d05d..3810bc0 100644 --- a/server/plugininterface.js +++ b/server/plugininterface.js @@ -7,6 +7,10 @@ function PluginInterface () { // Holder for all the bound listeners by this module this._listeners = {}; + + // Event proxies + this._parent = null; + this._children = []; } @@ -55,13 +59,51 @@ PluginInterface.prototype.off = function (event_name, fn, scope) { +PluginInterface.prototype.getListeners = function(event_name) { + return this._listeners[event_name] || []; +}; + + + +PluginInterface.prototype.createProxy = function() { + var proxy = new PluginInterface(); + proxy._parent = this._parent || this; + proxy._parent._children.push(proxy); + + return proxy; +}; + + + +PluginInterface.prototype.dispose = function() { + this.off(); + + if (this._parent) { + var idx = this._parent._children.indexOf(this); + if (idx > -1) { + this._parent._children.splice(idx, 1); + } + } +}; + + + // Call all the listeners for a certain event, passing them some event data that may be changed PluginInterface.prototype.emit = function (event_name, event_data) { - var emitter = new this.EmitCall(event_name, event_data); - var listeners = this._listeners[event_name] || []; + var emitter = new this.EmitCall(event_name, event_data), + listeners = [], + child_idx; + + // Get each childs event listeners in order of last created + for(child_idx=this._children.length-1; child_idx>=0; child_idx--) { + listeners = listeners.concat(this._children[child_idx].getListeners(event_name)); + } + + // Now include any listeners directly on this instance + listeners = listeners.concat(this.getListeners(event_name)); // Once emitted, remove any 'once' bound listeners - emitter.done(function () { + emitter.then(function () { var len = listeners.length, idx; @@ -197,8 +239,8 @@ PluginInterface.prototype.EmitCall = function EmitCall (event_name, event_data) return { callListeners: callListeners, - done: addCompletedFunc, - prevented: addPreventedFunc + then: addCompletedFunc, + catch: addPreventedFunc }; };