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:22 UTC
[4/5] js commit: Fix unsubscribe of a function listening on a channel
using subscribeOnce.
Fix unsubscribe of a function listening on a channel using subscribeOnce.
Plus added a couple other subscribeOnce tests
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/3b3e2de3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/3b3e2de3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/3b3e2de3
Branch: refs/heads/master
Commit: 3b3e2de39ed6138225a59d565e79a17594abfb10
Parents: dd21b35
Author: Andrew Grieve <ag...@chromium.org>
Authored: Thu Aug 23 23:05:39 2012 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Thu Aug 23 23:35:56 2012 -0400
----------------------------------------------------------------------
lib/common/channel.js | 13 ++++++-------
test/test.channel.js | 28 ++++++++++++++++++++++++++++
2 files changed, 34 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/3b3e2de3/lib/common/channel.js
----------------------------------------------------------------------
diff --git a/lib/common/channel.js b/lib/common/channel.js
index 446f105..c1cd702 100755
--- a/lib/common/channel.js
+++ b/lib/common/channel.js
@@ -169,15 +169,14 @@ Channel.prototype.subscribeOnce = function(f, c) {
var g = null;
var _this = this;
- var m = function() {
- f.apply(c || null, arguments);
- _this.unsubscribe(g);
- };
if (this.fired) {
- if (typeof c == "object") { f = utils.close(c, f); }
- f.apply(this, this.fireArgs);
+ f.apply(c || null, this.fireArgs);
} else {
- g = this.subscribe(m);
+ g = this.subscribe(function() {
+ _this.unsubscribe(g);
+ f.apply(c || null, arguments);
+ });
+ f.observer_guid = g;
}
return g;
};
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/3b3e2de3/test/test.channel.js
----------------------------------------------------------------------
diff --git a/test/test.channel.js b/test/test.channel.js
index 8e97685..9968901 100644
--- a/test/test.channel.js
+++ b/test/test.channel.js
@@ -129,6 +129,16 @@ describe("channel", function () {
expect(c.numHandlers).toEqual(1);
expect(c2.numHandlers).toEqual(1);
});
+ it("should be able to unsubscribe a subscribeOnce.", function() {
+ var handler = function(){};
+ c.subscribeOnce(handler);
+
+ expect(c.numHandlers).toEqual(1);
+
+ c.unsubscribe(handler);
+
+ expect(c.numHandlers).toEqual(0);
+ });
});
describe("fire method", function() {
@@ -197,4 +207,22 @@ describe("channel", function () {
expect(after).toHaveBeenCalled();
});
});
+ describe("subscribeOnce method", function() {
+ it("should be unregistered after being fired.", function() {
+ var count = 0;
+ var handler = jasmine.createSpy().andCallFake(function() { count++; });
+ c.subscribeOnce(handler);
+ c.fire();
+ c.fire();
+ expect(count).toEqual(1);
+ });
+ it("should be safe to add listeners from within callback.", function() {
+ var count = 0;
+ var handler = jasmine.createSpy().andCallFake(function() { count++; c.subscribeOnce(handler2); });
+ var handler2 = jasmine.createSpy().andCallFake(function() { count++; });
+ c.subscribeOnce(handler);
+ c.fire();
+ expect(count).toEqual(2);
+ });
+ });
});