You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2012/08/24 23:57:38 UTC
[7/50] [abbrv] 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/32ddfa69
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/32ddfa69
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/32ddfa69
Branch: refs/heads/master
Commit: 32ddfa699d37e282645f2f0a53f5d95863a82eb9
Parents: 0911e19
Author: Andrew Grieve <ag...@chromium.org>
Authored: Thu Aug 23 23:05:39 2012 -0400
Committer: Anis Kadri <an...@gmail.com>
Committed: Fri Aug 24 13:50:04 2012 -0700
----------------------------------------------------------------------
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/32ddfa69/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/32ddfa69/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);
+ });
+ });
});