You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2012/08/24 23:57:38 UTC

[12/50] [abbrv] js commit: Channel: allow the same function to be a listener on multiple channels.

Channel: allow the same function to be a listener on multiple channels.

Also added test.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/0911e19e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/0911e19e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/0911e19e

Branch: refs/heads/master
Commit: 0911e19e3cd65601c277565c0c779ddce0bdb3ef
Parents: a7ea012
Author: Andrew Grieve <ag...@chromium.org>
Authored: Thu Aug 23 16:16:35 2012 -0400
Committer: Anis Kadri <an...@gmail.com>
Committed: Fri Aug 24 13:50:03 2012 -0700

----------------------------------------------------------------------
 lib/common/channel.js |   16 ++++++++--------
 test/test.channel.js  |   10 ++++++++++
 2 files changed, 18 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0911e19e/lib/common/channel.js
----------------------------------------------------------------------
diff --git a/lib/common/channel.js b/lib/common/channel.js
index bd6fef6..446f105 100755
--- a/lib/common/channel.js
+++ b/lib/common/channel.js
@@ -146,16 +146,16 @@ Channel.prototype.subscribe = function(f, c, g) {
         // first time any channel has seen this subscriber
         g = nextGuid++;
     }
-    else {
-        // subscriber already handled; don't set it twice
-        return g;
-    }
     func.observer_guid = g;
     f.observer_guid = g;
-    this.handlers[g] = func;
-    this.numHandlers++;
-    if (this.events.onSubscribe) this.events.onSubscribe.call(this);
-    if (this.fired) func.call(this);
+
+    // Don't add the same handler more than once.
+    if (!this.handlers[g]) {
+        this.handlers[g] = func;
+        this.numHandlers++;
+        if (this.events.onSubscribe) this.events.onSubscribe.call(this);
+        if (this.fired) func.call(this);
+    }
     return g;
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0911e19e/test/test.channel.js
----------------------------------------------------------------------
diff --git a/test/test.channel.js b/test/test.channel.js
index a6bcf66..8e97685 100644
--- a/test/test.channel.js
+++ b/test/test.channel.js
@@ -50,6 +50,16 @@ describe("channel", function () {
 
             expect(c.numHandlers).toEqual(initialLength+1);
         });
+        it("should be able to use the same function with multiple channels.", function() {
+            var c2 = channel.create('jables');
+            var handler = function(){};
+
+            c.subscribe(handler);
+            c2.subscribe(handler);
+
+            expect(c.numHandlers).toEqual(1);
+            expect(c2.numHandlers).toEqual(1);
+        });
     });
 
     describe("unsubscribe method", function() {