Updated Branches: refs/heads/master fed6a2917 -> e4448110f
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/dd21b352 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/dd21b352 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/dd21b352 Branch: refs/heads/master Commit: dd21b352aae1c0a26c7f76b5faf8463f52301751 Parents: b5c5184 Author: Andrew Grieve <agri...@chromium.org> Authored: Thu Aug 23 16:16:35 2012 -0400 Committer: Andrew Grieve <agri...@chromium.org> Committed: Thu Aug 23 23:35:56 2012 -0400 ---------------------------------------------------------------------- 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/dd21b352/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/dd21b352/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() {