Repository: cordova-lib
Updated Branches:
  refs/heads/master d857645df -> ecba9324f


CB-10430 Adds forwardEvents method to easily connect two EventEmitters. This 
closes #382


Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/ecba9324
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/ecba9324
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/ecba9324

Branch: refs/heads/master
Commit: ecba9324f42d4a205f034c68afe38f8faf0e0941
Parents: d857645
Author: Vladimir Kotikov <[email protected]>
Authored: Tue Feb 9 09:26:52 2016 +0300
Committer: Vladimir Kotikov <[email protected]>
Committed: Tue Feb 9 21:39:15 2016 +0300

----------------------------------------------------------------------
 cordova-common/src/events.js | 48 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/ecba9324/cordova-common/src/events.js
----------------------------------------------------------------------
diff --git a/cordova-common/src/events.js b/cordova-common/src/events.js
index a6ec340..868d363 100644
--- a/cordova-common/src/events.js
+++ b/cordova-common/src/events.js
@@ -16,4 +16,50 @@
     specific language governing permissions and limitations
     under the License.
 */
-module.exports = new (require('events').EventEmitter)();
+
+var EventEmitter = require('events').EventEmitter;
+
+var INSTANCE = new EventEmitter();
+var EVENTS_RECEIVER;
+
+module.exports = INSTANCE;
+
+/**
+ * Sets up current instance to forward emitted events to another EventEmitter
+ *   instance.
+ *
+ * @param   {EventEmitter}  [eventEmitter]  The emitter instance to forward
+ *   events to. Falsy value, when passed, disables forwarding.
+ */
+module.exports.forwardEventsTo = function (eventEmitter) {
+
+    // If no argument is specified disable events forwarding
+    if (!eventEmitter) {
+        EVENTS_RECEIVER = undefined;
+        return;
+    }
+
+    if (!(eventEmitter instanceof EventEmitter))
+        throw new Error('Cordova events could be redirected to another 
EventEmitter instance only');
+
+    EVENTS_RECEIVER = eventEmitter;
+};
+
+var emit = INSTANCE.emit;
+
+/**
+ * This method replaces original 'emit' method to allow events forwarding.
+ *
+ * @return  {eventEmitter}  Current instance to allow calls chaining, as
+ *   original 'emit' does
+ */
+module.exports.emit = function () {
+
+    var args = Array.prototype.slice.call(arguments);
+
+    if (EVENTS_RECEIVER) {
+        EVENTS_RECEIVER.emit.apply(EVENTS_RECEIVER, args);
+    }
+
+    return emit.apply(this, args);
+};


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to