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/02/29 02:17:46 UTC

[2/3] git commit: cleaning up a bug with registering/unregistering handlers in channel. added more tests for that. switched over use of handlers.length to new numHandlers variable in android and blackberry platform defs.

cleaning up a bug with registering/unregistering handlers in channel. added more tests for that. switched over use of handlers.length to new numHandlers variable in android and blackberry platform defs.


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

Branch: refs/heads/master
Commit: 94d7bb55b09c6ea3364abe92965cc9d03b3940ad
Parents: 9a4b200
Author: Fil Maj <ma...@gmail.com>
Authored: Tue Feb 28 17:01:56 2012 -0800
Committer: Fil Maj <ma...@gmail.com>
Committed: Tue Feb 28 17:17:13 2012 -0800

----------------------------------------------------------------------
 lib/channel.js             |    5 ++++-
 lib/platform/android.js    |    4 ++--
 lib/platform/blackberry.js |   10 +++++-----
 lib/plugin/battery.js      |   24 ++++++++++--------------
 test/test.battery.js       |    7 +++++--
 test/test.channel.js       |   22 ++++++++++++++++++++++
 6 files changed, 48 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/94d7bb55/lib/channel.js
----------------------------------------------------------------------
diff --git a/lib/channel.js b/lib/channel.js
index fca7671..c8d83ce 100644
--- a/lib/channel.js
+++ b/lib/channel.js
@@ -13,7 +13,8 @@
  */
 var Channel = function(type, opts) {
         this.type = type;
-        this.handlers = [];
+        this.handlers = {};
+        this.numHandlers = 0;
         this.guid = 0;
         this.fired = false;
         this.enabled = true;
@@ -67,6 +68,7 @@ Channel.prototype.subscribe = function(f, c, g) {
     func.observer_guid = g;
     f.observer_guid = g;
     this.handlers[g] = func;
+    this.numHandlers++;
     if (this.events.onSubscribe) this.events.onSubscribe.call(this);
     return g;
 };
@@ -98,6 +100,7 @@ Channel.prototype.unsubscribe = function(g) {
     if (g instanceof Function) { g = g.observer_guid; }
     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/94d7bb55/lib/platform/android.js
----------------------------------------------------------------------
diff --git a/lib/platform/android.js b/lib/platform/android.js
index 4da115d..b9a457f 100644
--- a/lib/platform/android.js
+++ b/lib/platform/android.js
@@ -35,13 +35,13 @@ module.exports = {
     var backButtonChannel = cordova.addDocumentEventHandler('backbutton', {
       onSubscribe:function() {
         // If we just attached the first handler, let native know we need to override the back button.
-        if (this.handlers.length === 1) {
+        if (this.numHandlers === 1) {
           exec(null, null, "App", "overrideBackbutton", [true]);
         }
       },
       onUnsubscribe:function() {
         // If we just detached the last handler, let native know we no longer override the back button.
-        if (this.handlers.length === 0) {
+        if (this.numHandlers === 0) {
           exec(null, null, "App", "overrideBackbutton", [false]);
         }
       }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/94d7bb55/lib/platform/blackberry.js
----------------------------------------------------------------------
diff --git a/lib/platform/blackberry.js b/lib/platform/blackberry.js
index dd25f07..dd366a6 100644
--- a/lib/platform/blackberry.js
+++ b/lib/platform/blackberry.js
@@ -62,7 +62,7 @@ module.exports = {
             return { onSubscribe : function() {
                 // If we just attached the first handler, let native know we
                 // need to override the back button.
-                if (this.handlers.length === 1) {
+                if (this.numHandlers === 1) {
                     blackberry.system.event.onHardwareKey(
                             buttonMapping[event], fireEvent(event));
                 }
@@ -70,7 +70,7 @@ module.exports = {
             onUnsubscribe : function() {
                 // If we just detached the last handler, let native know we
                 // no longer override the back button.
-                if (this.handlers.length === 0) {
+                if (this.numHandlers === 0) {
                     blackberry.system.event.onHardwareKey(
                             buttonMapping[event], null);
                 }
@@ -101,7 +101,7 @@ module.exports = {
         // Unsubscribe handler - turns off native backlight change
         // listener
         var onUnsubscribe = function() {
-            if (channel.onResume.handlers.length === 0 && channel.onPause.handlers.length === 0) {
+            if (channel.onResume.numHandlers === 0 && channel.onPause.numHandlers === 0) {
                 exec(null, null, 'App', 'ignoreBacklight', []);
             }
         };
@@ -125,7 +125,7 @@ module.exports = {
                 // If we just attached the first handler and there are
                 // no pause handlers, start the backlight system
                 // listener on the native side.
-                if (channel.onResume.handlers.length === 1 && channel.onPause.handlers.length === 0) {
+                if (channel.onResume.numHandlers === 1 && channel.onPause.numHandlers === 0) {
                     exec(backlightWin, backlightFail, "App", "detectBacklight", []);
                 }
             },
@@ -136,7 +136,7 @@ module.exports = {
                 // If we just attached the first handler and there are
                 // no resume handlers, start the backlight system
                 // listener on the native side.
-                if (channel.onResume.handlers.length === 0 && channel.onPause.handlers.length === 1) {
+                if (channel.onResume.numHandlers === 0 && channel.onPause.numHandlers === 1) {
                     exec(backlightWin, backlightFail, "App", "detectBacklight", []);
                 }
             },

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/94d7bb55/lib/plugin/battery.js
----------------------------------------------------------------------
diff --git a/lib/plugin/battery.js b/lib/plugin/battery.js
index e89771b..5649356 100644
--- a/lib/plugin/battery.js
+++ b/lib/plugin/battery.js
@@ -6,13 +6,9 @@ var cordova = require('cordova'),
     exec = require('cordova/exec');
 
 function handlers() {
-  var count = function (a) {
-          return a.filter(function (v) {return !!v;}).length;
-      }; 
-
-  return count(module.exports.channels.batterystatus.handlers) + 
-         count(module.exports.channels.batterylow.handlers) +
-         count(module.exports.channels.batterycritical.handlers);
+  return battery.channels.batterystatus.numHandlers + 
+         battery.channels.batterylow.numHandlers +
+         battery.channels.batterycritical.numHandlers;
 }
 
 var Battery = function() {
@@ -35,7 +31,7 @@ var Battery = function() {
  * appropriately (and hopefully save on battery life!).
  */
 Battery.prototype.onSubscribe = function() {
-  var me = module.exports; // TODO: i dont like this reference
+  var me = battery;
   // If we just registered the first handler, make sure native listener is started.
   if (handlers() === 1) {
     exec(me._status, me._error, "Battery", "start", []);
@@ -43,10 +39,8 @@ Battery.prototype.onSubscribe = function() {
 };
 
 Battery.prototype.onUnsubscribe = function() {
-  var me = module.exports,
-      empty = function (a) {
-          return a.filter(function (v, i) {return v && !!i;});
-      }; 
+  var me = battery;
+
   // If we just unregistered the last handler, make sure native listener is stopped.
   if (handlers() === 0) {
       exec(null, null, "Battery", "stop", []);
@@ -60,7 +54,7 @@ Battery.prototype.onUnsubscribe = function() {
  */
 Battery.prototype._status = function(info) {
 	if (info) {
-		var me = module.exports;//TODO: can we eliminate this global ref?
+		var me = battery;
     var level = info.level;
 		if (me._level !== level || me._isPlugged !== info.isPlugged) {
 			// Fire batterystatus event
@@ -88,4 +82,6 @@ Battery.prototype._error = function(e) {
     console.log("Error initializing Battery: " + e);
 };
 
-module.exports = new Battery();
+var battery = new Battery();
+
+module.exports = battery;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/94d7bb55/test/test.battery.js
----------------------------------------------------------------------
diff --git a/test/test.battery.js b/test/test.battery.js
index 5167fac..8ac87bd 100644
--- a/test/test.battery.js
+++ b/test/test.battery.js
@@ -10,7 +10,7 @@ describe("battery", function () {
             window.removeEventListener("batterystatus", cb);
         });
         
-        it("only calls exec once", function () {
+        it("only calls exec with start once", function () {
             var cb1 = jasmine.createSpy("cb1"),
                 cb2 = jasmine.createSpy("cb2");
 
@@ -28,12 +28,15 @@ describe("battery", function () {
                 cb2 = jasmine.createSpy("cb2");
 
             window.addEventListener("batterystatus", cb1);
+            
+            exec.reset();
+            
             window.removeEventListener("batterystatus", cb1);
 
             expect(exec).toHaveBeenCalledWith(null, null, "Battery", "stop", []);
         });
 
-        it("only calls stop once", function () {
+        it("only calls exec with stop once", function () {
             var cb1 = jasmine.createSpy("cb1"),
                 cb2 = jasmine.createSpy("cb2");
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/94d7bb55/test/test.channel.js
----------------------------------------------------------------------
diff --git a/test/test.channel.js b/test/test.channel.js
index 2d3dc47..96e07eb 100644
--- a/test/test.channel.js
+++ b/test/test.channel.js
@@ -1,3 +1,25 @@
 describe("channel", function () {
     var channel = require('cordova/channel');
+
+    describe("when subscribing", function() {
+    });
+
+    describe("when unsubscribing", function() {
+        it("should change the handlers length appropriately", function() {
+            var c = channel.create('test');
+            var firstHandler = function() {};
+            var secondHandler = function() {};
+            var thirdHandler = function() {};
+
+            c.subscribe(firstHandler);
+            c.subscribe(secondHandler);
+            c.subscribe(thirdHandler);
+
+            var initialLength = c.numHandlers;
+
+            c.unsubscribe(thirdHandler);
+
+            expect(c.numHandlers).toEqual(initialLength - 1);
+        });
+    });
 });