From 32d9255a7913d7a5030f5c887f12190b221b4c60 Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 25 Nov 2014 10:45:14 +0000 Subject: [PATCH] PluginInterface event proxy support --- client/src/helpers/plugininterface.js | 46 +++++++++++++++++++++++++-- server/plugininterface.js | 46 +++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/client/src/helpers/plugininterface.js b/client/src/helpers/plugininterface.js index c218d21..3810bc0 100644 --- a/client/src/helpers/plugininterface.js +++ b/client/src/helpers/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,10 +59,48 @@ 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.then(function () { diff --git a/server/plugininterface.js b/server/plugininterface.js index c218d21..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,10 +59,48 @@ 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.then(function () { -- 2.25.1