Make GUID counter shared for all channels. Added test showing why this is important.
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/a7ea012d Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/a7ea012d Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/a7ea012d Branch: refs/heads/master Commit: a7ea012d7f76de0a629fb2bdbc975a976fe40c24 Parents: a930ee1 Author: Andrew Grieve <agri...@chromium.org> Authored: Thu Aug 23 16:07:24 2012 -0400 Committer: Anis Kadri <anis.ka...@gmail.com> Committed: Fri Aug 24 13:50:03 2012 -0700 ---------------------------------------------------------------------- lib/common/channel.js | 8 ++++---- test/test.channel.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a7ea012d/lib/common/channel.js ---------------------------------------------------------------------- diff --git a/lib/common/channel.js b/lib/common/channel.js index 242a2d7..bd6fef6 100755 --- a/lib/common/channel.js +++ b/lib/common/channel.js @@ -1,4 +1,5 @@ -var utils = require('cordova/utils'); +var utils = require('cordova/utils'), + nextGuid = 1; /** * Custom pub-sub "channel" that can have functions subscribed to it @@ -50,7 +51,6 @@ var Channel = function(type, opts) { this.type = type; this.handlers = {}; this.numHandlers = 0; - this.guid = 1; this.fired = false; this.enabled = true; this.events = { @@ -143,8 +143,8 @@ Channel.prototype.subscribe = function(f, c, g) { g = g || func.observer_guid || f.observer_guid; if (!g) { - // first time we've seen this subscriber - g = this.guid++; + // first time any channel has seen this subscriber + g = nextGuid++; } else { // subscriber already handled; don't set it twice http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a7ea012d/test/test.channel.js ---------------------------------------------------------------------- diff --git a/test/test.channel.js b/test/test.channel.js index 5a43802..a6bcf66 100644 --- a/test/test.channel.js +++ b/test/test.channel.js @@ -103,6 +103,22 @@ describe("channel", function () { expect(c.numHandlers).toEqual(0); }); + it("should not unregister a function registered with a different handler", function() { + var cHandler = function(){}; + var c2Handler = function(){}; + var c2 = channel.create('jables'); + c.subscribe(cHandler); + c2.subscribe(c2Handler); + + expect(c.numHandlers).toEqual(1); + expect(c2.numHandlers).toEqual(1); + + c.unsubscribe(c2Handler); + c2.unsubscribe(cHandler); + + expect(c.numHandlers).toEqual(1); + expect(c2.numHandlers).toEqual(1); + }); }); describe("fire method", function() {