You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2012/08/24 05:36:23 UTC
[5/5] js commit: Make GUID counter shared for all channels.
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/b5c5184b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/b5c5184b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/b5c5184b
Branch: refs/heads/master
Commit: b5c5184bd597525ca87372cf90165b2aba60acd7
Parents: fed6a29
Author: Andrew Grieve <ag...@chromium.org>
Authored: Thu Aug 23 16:07:24 2012 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Thu Aug 23 23:32:45 2012 -0400
----------------------------------------------------------------------
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/b5c5184b/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/b5c5184b/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() {