You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2012/05/09 00:04:19 UTC

js commit: [CB-633] Fix for channel, regarding unsubscribing and how a handler count is decremented. Also added tests to catch this.

Updated Branches:
  refs/heads/master 8b369515d -> 6cb6e6cfb


[CB-633] Fix for channel, regarding unsubscribing and how a handler count is decremented. Also added tests to catch this.


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/6cb6e6cf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/6cb6e6cf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/6cb6e6cf

Branch: refs/heads/master
Commit: 6cb6e6cfb5d85017d2b2f24203d9078f1be775a8
Parents: 8b36951
Author: Fil Maj <ma...@gmail.com>
Authored: Tue May 8 15:07:18 2012 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Tue May 8 15:07:18 2012 -0700

----------------------------------------------------------------------
 lib/common/channel.js |   11 +++++++----
 test/test.channel.js  |   32 ++++++++++++++++++++++++++------
 2 files changed, 33 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/6cb6e6cf/lib/common/channel.js
----------------------------------------------------------------------
diff --git a/lib/common/channel.js b/lib/common/channel.js
index 329efda..455892a 100755
--- a/lib/common/channel.js
+++ b/lib/common/channel.js
@@ -181,10 +181,13 @@ Channel.prototype.unsubscribe = function(g) {
     if (g === null || g === undefined) { throw "You must pass _something_ into Channel.unsubscribe"; }
 
     if (typeof g == 'function') { g = g.observer_guid; }
-    this.handlers[g] = null;
-    delete this.handlers[g];
-    this.numHandlers--;
-    if (this.events.onUnsubscribe) this.events.onUnsubscribe.call(this);
+    var handler = this.handlers[g];
+    if (handler) {
+        this.handlers[g] = null;
+        delete this.handlers[g];
+        this.numHandlers--;
+        if (this.events.onUnsubscribe) this.events.onUnsubscribe.call(this);
+    }
 };
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/6cb6e6cf/test/test.channel.js
----------------------------------------------------------------------
diff --git a/test/test.channel.js b/test/test.channel.js
index c8bb43d..d998da9 100644
--- a/test/test.channel.js
+++ b/test/test.channel.js
@@ -1,9 +1,14 @@
 describe("channel", function () {
-    var channel = require('cordova/channel');
+    var channel = require('cordova/channel'),
+        c;
 
-    describe("when subscribing", function() {
+    beforeEach(function() {
+        c = null;
+        c = channel.create('masterexploder');
+    });
+
+    describe("subscribe method", function() {
         it("should throw an exception if no function is provided", function() {
-            var c = channel.create('test');
             expect(function() {
                 c.subscribe();
             }).toThrow();
@@ -21,7 +26,6 @@ describe("channel", function () {
             }).toThrow();
         });
         it("should not change number of handlers if no function is provided", function() {
-            var c = channel.create('heydawg');
             var initialLength = c.numHandlers;
 
             try {
@@ -38,9 +42,25 @@ describe("channel", function () {
         });
     });
 
-    describe("when unsubscribing", function() {
+    describe("unsubscribe method", function() {
+        it("should throw an exception if passed in null or undefined", function() {
+            expect(function() {
+                c.unsubscribe();
+            }).toThrow();
+            expect(function() {
+                c.unsubscribe(null);
+            }).toThrow();
+        });
+        it("should not decrement numHandlers if unsubscribing something that does not exist", function() {
+            var initialLength = c.numHandlers;
+            c.unsubscribe('blah');
+            expect(c.numHandlers).toEqual(initialLength);
+            c.unsubscribe(2);
+            expect(c.numHandlers).toEqual(initialLength);
+            c.unsubscribe({balls:false});
+            expect(c.numHandlers).toEqual(initialLength);
+        });
         it("should change the handlers length appropriately", function() {
-            var c = channel.create('test');
             var firstHandler = function() {};
             var secondHandler = function() {};
             var thirdHandler = function() {};