You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2012/05/31 20:07:51 UTC

[4/4] js commit: channel.subscribe doesnt register same handler multiple times

channel.subscribe doesnt register same handler multiple times


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/71c7a270
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/71c7a270
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/71c7a270

Branch: refs/heads/master
Commit: 71c7a27050ffe903f01e80b60b5753ebc49ecf31
Parents: 16091ff
Author: Julien Bouquillon <ju...@bouquillon.com>
Authored: Thu May 31 10:13:11 2012 +0200
Committer: Fil Maj <ma...@gmail.com>
Committed: Thu May 31 10:27:01 2012 -0700

----------------------------------------------------------------------
 lib/common/channel.js |   10 +++++++++-
 test/test.channel.js  |    5 +++++
 2 files changed, 14 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/71c7a270/lib/common/channel.js
----------------------------------------------------------------------
diff --git a/lib/common/channel.js b/lib/common/channel.js
index 455892a..71ec066 100755
--- a/lib/common/channel.js
+++ b/lib/common/channel.js
@@ -141,7 +141,15 @@ Channel.prototype.subscribe = function(f, c, g) {
     var func = f;
     if (typeof c == "object") { func = utils.close(c, f); }
 
-    g = g || func.observer_guid || f.observer_guid || this.guid++;
+    g = g || func.observer_guid || f.observer_guid;
+    if (!g) {
+        // first time we've seen this subscriber
+        g = this.guid++;
+    }
+    else {
+        // subscriber already handled; dont set it twice
+        return g;
+    }
     func.observer_guid = g;
     f.observer_guid = g;
     this.handlers[g] = func;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/71c7a270/test/test.channel.js
----------------------------------------------------------------------
diff --git a/test/test.channel.js b/test/test.channel.js
index cd86cec..d64e770 100644
--- a/test/test.channel.js
+++ b/test/test.channel.js
@@ -82,9 +82,14 @@ describe("channel", function () {
 
             c.subscribe(firstHandler);
             c.subscribe(firstHandler);
+            c.subscribe(firstHandler);
+
+            expect(c.numHandlers).toEqual(1);
 
             c.unsubscribe(firstHandler);
             c.unsubscribe(firstHandler);
+            c.unsubscribe(firstHandler);
+            c.unsubscribe(firstHandler);
 
             expect(c.numHandlers).toEqual(0);