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

git commit: [CB-299] cordova.js should allow for registering plugins as required before deviceready fires. Use for CupcakeStorage on Android.

Updated Branches:
  refs/heads/master e71e51e88 -> efdcd7703


[CB-299] cordova.js should allow for registering plugins as required before deviceready fires.  Use for CupcakeStorage on Android.


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

Branch: refs/heads/master
Commit: efdcd770398b6ce79be90c809c240d6afccafaa8
Parents: e71e51e
Author: Bryce Curtis <cu...@gmail.com>
Authored: Wed Mar 14 18:01:59 2012 -0500
Committer: Bryce Curtis <cu...@gmail.com>
Committed: Wed Mar 14 18:01:59 2012 -0500

----------------------------------------------------------------------
 lib/bootstrap.js              |    8 +---
 lib/channel.js                |   65 +++++++++++++++++++++++++++++++++++-
 lib/plugin/android/storage.js |    4 ++
 3 files changed, 70 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/efdcd770/lib/bootstrap.js
----------------------------------------------------------------------
diff --git a/lib/bootstrap.js b/lib/bootstrap.js
index f90871b..dfc1a90 100755
--- a/lib/bootstrap.js
+++ b/lib/bootstrap.js
@@ -1,10 +1,5 @@
 (function (context) {
     var channel = require("cordova/channel"),
-        /**
-         * cordova Channels that must fire before "deviceready" is fired.
-         */ 
-        deviceReadyChannelsArray = [channel.onCordovaReady, channel.onCordovaInfoReady, channel.onCordovaConnectionReady],
-        deviceReadyChannelsMap = {},
         _self = {
             boot: function () {
                 //---------------
@@ -48,11 +43,12 @@
                         
                         // Fire the onresume event, since first one happens before JavaScript is loaded
                         channel.onResume.fire();
-                    }, deviceReadyChannelsArray);
+                    }, channel.deviceReadyChannelsArray);
                     
                 }, [ channel.onDOMContentLoaded, channel.onNativeReady ]);
             }
         };
+        
     // boot up once native side is ready
     channel.onNativeReady.subscribeOnce(_self.boot);
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/efdcd770/lib/channel.js
----------------------------------------------------------------------
diff --git a/lib/channel.js b/lib/channel.js
old mode 100644
new mode 100755
index 9beed93..baf08b6
--- a/lib/channel.js
+++ b/lib/channel.js
@@ -46,6 +46,45 @@ var Channel = function(type, opts) {
         create: function (type, opts) {
             channel[type] = new Channel(type, opts);
             return channel[type];
+        },
+
+        /**
+         * cordova Channels that must fire before "deviceready" is fired.
+         */ 
+        deviceReadyChannelsArray: [],
+        deviceReadyChannelsMap: {},
+        
+        /**
+         * Indicate that a feature needs to be initialized before it is ready to be used.
+         * This holds up Cordova's "deviceready" event until the feature has been initialized
+         * and Cordova.initComplete(feature) is called.
+         *
+         * @param feature {String}     The unique feature name
+         */
+        waitForInitialization: function(feature) {
+            if (feature) {
+                var c = null;
+                if (this[feature]) {
+                    c = this[feature];
+                }
+                else {
+                    c = this.create(feature);
+                }
+                this.deviceReadyChannelsMap[feature] = c;
+                this.deviceReadyChannelsArray.push(c);
+            }
+        },
+
+        /**
+         * Indicate that initialization code has completed and the feature is ready to be used.
+         *
+         * @param feature {String}     The unique feature name
+         */
+        initializationComplete: function(feature) {
+            var c = this.deviceReadyChannelsMap[feature];
+            if (c) {
+                c.fire();
+            }
         }
     },
     utils = require('cordova/utils');
@@ -131,14 +170,38 @@ Channel.prototype.fire = function(e) {
 };
 
 //HACK: defining them here so they are ready super fast!
+
+// DOM event that is received when the web page is loaded and parsed.
 channel.create('onDOMContentLoaded');
+
+// Event to indicate the Cordova native side is ready.
 channel.create('onNativeReady');
+
+// Event to indicate that all Cordova JavaScript objects have been created
+// and it's time to run plugin constructors.
 channel.create('onCordovaReady');
+
+// Event to indicate that device properties are available
 channel.create('onCordovaInfoReady');
+
+// Event to indicate that the connection property has been set.
 channel.create('onCordovaConnectionReady');
+
+// Event to indicate that Cordova is ready
+channel.create('onDeviceReady');
+
+// Event to indicate a resume lifecycle event
 channel.create('onResume');
+
+// Event to indicate a pause lifecycle event
 channel.create('onPause');
-channel.create('onDeviceReady');
+
+// Event to indicate a destroy lifecycle event
 channel.create('onDestroy');
 
+// Channels that must fire before "deviceready" is fired.
+channel.waitForInitialization('onCordovaReady');
+channel.waitForInitialization('onCordovaInfoReady');
+channel.waitForInitialization('onCordovaConnectionReady');
+
 module.exports = channel;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/efdcd770/lib/plugin/android/storage.js
----------------------------------------------------------------------
diff --git a/lib/plugin/android/storage.js b/lib/plugin/android/storage.js
index 11bef71..000612b 100644
--- a/lib/plugin/android/storage.js
+++ b/lib/plugin/android/storage.js
@@ -1,5 +1,6 @@
 var utils = require('cordova/utils'),
     exec = require('cordova/exec');
+    channel = require('cordova/channel');
 
 var queryQueue = {};
 
@@ -287,6 +288,8 @@ var DroidDB_openDatabase = function(name, version, display_name, size) {
  * @constructor
  */
 var CupcakeLocalStorage = function() {
+    channel.waitForInitialization("cupcakeStorage");
+
     try {
 
       this.db = openDatabase('localStorage', '1.0', 'localStorage', 2621440);
@@ -305,6 +308,7 @@ var CupcakeLocalStorage = function() {
               storage[result.rows.item(i)['id']] =  result.rows.item(i)['body'];
             }
             setLength(result.rows.length);
+            channel.initializationComplete("cupcakeStorage");
           });
 
         },


Re: git commit: [CB-299] cordova.js should allow for registering plugins as required before deviceready fires. Use for CupcakeStorage on Android.

Posted by Patrick Mueller <pm...@gmail.com>.
Ah.  That sounds good.

Looking at using some existing bits somewhere?  Or even some existing
'style'?

Node.js's events.js module seems to be 'pure' JS, modulo some console
method calls [1].

I'm kinda partial to Backbone's on/off/trigger brevity, along with the fact
that you can bind a receiver with your callback function (they call it a
'context'), which doesn't require an actual bind() [2].

I haven't used 'promises' in anger yet, some folks seem to think they're
the best thing evar [3].  I wouldn't want to foist that interface on our
users - would prefer we just expose a typical 'listener' approach
(node/backbone), but maybe there'd be some value of having promises as the
substrate on which we'd build an event-y model.  I'm not convinced yet.

We'll certainly need some additional prims over what you get from a simple
interface like node/backbone, in order to be able to do join() type calls
(wait for all the specified events to fire, THEN fire the cb specified in
the join() invocation).

[1] https://github.com/joyent/node/blob/master/lib/events.js
[2] https://github.com/documentcloud/backbone/blob/master/backbone.js#L71
[3] https://github.com/kriskowal/q

On Thu, Mar 15, 2012 at 12:51, <gt...@gmail.com> wrote:

> I know we have been throwing around the idea of replacing channel with an
> eventing module.
>
> I agree the channel code is rather large and complex and I am sure we can
> get all the functionality we need with an eventemitter interface and named
> events.
>
>
> Sent on the TELUS Mobility network with BlackBerry
>
> -----Original Message-----
> From: Patrick Mueller <pm...@gmail.com>
> Date: Thu, 15 Mar 2012 12:45:31
> To: <ca...@incubator.apache.org>
> Reply-To: callback-dev@incubator.apache.org
> Subject: Re: git commit: [CB-299] cordova.js should allow for registering
>  plugins as required before deviceready fires. Use for CupcakeStorage on
> Android.
>
> On Wed, Mar 14, 2012 at 19:02, <bc...@apache.org> wrote:
>
> > Updated Branches:
> >  refs/heads/master e71e51e88 -> efdcd7703
> >
> > [CB-299] cordova.js should allow for registering plugins as required
> > before deviceready fires.  Use for CupcakeStorage on Android.
> >
> > 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/efdcd770
> > Tree:
> >
> http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/efdcd770
> > Diff:
> >
> http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/efdcd770
> > ...
> >
> >
> >
> http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/efdcd770/lib/channel.js
> > ----------------------------------------------------------------------
> > diff --git a/lib/channel.js b/lib/channel.js
> > old mode 100644
> > new mode 100755
> > index 9beed93..baf08b6
> > --- a/lib/channel.js
> > +++ b/lib/channel.js
> > @@ -46,6 +46,45 @@ var Channel = function(type, opts) {
> >         create: function (type, opts) {
> >             channel[type] = new Channel(type, opts);
> >             return channel[type];
> > +        },
> > +
> > +        /**
> > +         * cordova Channels that must fire before "deviceready" is
> fired.
> > +         */
> > +        deviceReadyChannelsArray: [],
> > +        deviceReadyChannelsMap: {},
> >
>
> So, this is kind of a nasty pattern - keeping two data structures around
> when one will suffice, as there's the problem of these getting out of sync
> if code changes in the future.
>
> Then I looked at channel.join.  :-)
>
>       join: function (h, c) {
>            var i = c.length;
>            var len = i;
>            var f = function() {
>                if (!(--i)) h();
>            };
>            for (var j=0; j<len; j++) {
>                !c[j].fired?c[j].subscribeOnce(f):i--;
>            }
>            if (!i) h();
>        },
>
> Yikes!  Not terribly understandable, especially since it's not immediately
> obvious how a method like join() should work in the first place.  But the
> good news is, I think we could go to a single "map" and not use the array,
> since it doesn't need to be an array.  We can change the for loop to a for
> (var key in map) loop, and then reference c[key] in the body of the loop
> instead.
>
> Issues?
>
> --
> Patrick Mueller
> http://muellerware.org
>
>


-- 
Patrick Mueller
http://muellerware.org

Re: git commit: [CB-299] cordova.js should allow for registering plugins as required before deviceready fires. Use for CupcakeStorage on Android.

Posted by Filip Maj <fi...@adobe.com>.
+1

On 3/15/12 9:51 AM, "gtanner@gmail.com" <gt...@gmail.com> wrote:

>I know we have been throwing around the idea of replacing channel with an
>eventing module. 
>
>I agree the channel code is rather large and complex and I am sure we can
>get all the functionality we need with an eventemitter interface and
>named events.
>
>
>Sent on the TELUS Mobility network with BlackBerry
>
>-----Original Message-----
>From: Patrick Mueller <pm...@gmail.com>
>Date: Thu, 15 Mar 2012 12:45:31
>To: <ca...@incubator.apache.org>
>Reply-To: callback-dev@incubator.apache.org
>Subject: Re: git commit: [CB-299] cordova.js should allow for registering
> plugins as required before deviceready fires. Use for CupcakeStorage on
>Android.
>
>On Wed, Mar 14, 2012 at 19:02, <bc...@apache.org> wrote:
>
>> Updated Branches:
>>  refs/heads/master e71e51e88 -> efdcd7703
>>
>> [CB-299] cordova.js should allow for registering plugins as required
>> before deviceready fires.  Use for CupcakeStorage on Android.
>>
>> 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/efdcd7
>>70
>> Tree:
>> 
>>http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/efdcd770
>> Diff:
>> 
>>http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/efdcd770
>> ...
>>
>>
>> 
>>http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/efdcd770
>>/lib/channel.js
>> ----------------------------------------------------------------------
>> diff --git a/lib/channel.js b/lib/channel.js
>> old mode 100644
>> new mode 100755
>> index 9beed93..baf08b6
>> --- a/lib/channel.js
>> +++ b/lib/channel.js
>> @@ -46,6 +46,45 @@ var Channel = function(type, opts) {
>>         create: function (type, opts) {
>>             channel[type] = new Channel(type, opts);
>>             return channel[type];
>> +        },
>> +
>> +        /**
>> +         * cordova Channels that must fire before "deviceready" is
>>fired.
>> +         */
>> +        deviceReadyChannelsArray: [],
>> +        deviceReadyChannelsMap: {},
>>
>
>So, this is kind of a nasty pattern - keeping two data structures around
>when one will suffice, as there's the problem of these getting out of sync
>if code changes in the future.
>
>Then I looked at channel.join.  :-)
>
>       join: function (h, c) {
>            var i = c.length;
>            var len = i;
>            var f = function() {
>                if (!(--i)) h();
>            };
>            for (var j=0; j<len; j++) {
>                !c[j].fired?c[j].subscribeOnce(f):i--;
>            }
>            if (!i) h();
>        },
>
>Yikes!  Not terribly understandable, especially since it's not immediately
>obvious how a method like join() should work in the first place.  But the
>good news is, I think we could go to a single "map" and not use the array,
>since it doesn't need to be an array.  We can change the for loop to a for
>(var key in map) loop, and then reference c[key] in the body of the loop
>instead.
>
>Issues?
>
>-- 
>Patrick Mueller
>http://muellerware.org
>


Re: git commit: [CB-299] cordova.js should allow for registering plugins as required before deviceready fires. Use for CupcakeStorage on Android.

Posted by gt...@gmail.com.
I know we have been throwing around the idea of replacing channel with an eventing module. 

I agree the channel code is rather large and complex and I am sure we can get all the functionality we need with an eventemitter interface and named events.


Sent on the TELUS Mobility network with BlackBerry

-----Original Message-----
From: Patrick Mueller <pm...@gmail.com>
Date: Thu, 15 Mar 2012 12:45:31 
To: <ca...@incubator.apache.org>
Reply-To: callback-dev@incubator.apache.org
Subject: Re: git commit: [CB-299] cordova.js should allow for registering
 plugins as required before deviceready fires. Use for CupcakeStorage on Android.

On Wed, Mar 14, 2012 at 19:02, <bc...@apache.org> wrote:

> Updated Branches:
>  refs/heads/master e71e51e88 -> efdcd7703
>
> [CB-299] cordova.js should allow for registering plugins as required
> before deviceready fires.  Use for CupcakeStorage on Android.
>
> 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/efdcd770
> Tree:
> http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/efdcd770
> Diff:
> http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/efdcd770
> ...
>
>
> http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/efdcd770/lib/channel.js
> ----------------------------------------------------------------------
> diff --git a/lib/channel.js b/lib/channel.js
> old mode 100644
> new mode 100755
> index 9beed93..baf08b6
> --- a/lib/channel.js
> +++ b/lib/channel.js
> @@ -46,6 +46,45 @@ var Channel = function(type, opts) {
>         create: function (type, opts) {
>             channel[type] = new Channel(type, opts);
>             return channel[type];
> +        },
> +
> +        /**
> +         * cordova Channels that must fire before "deviceready" is fired.
> +         */
> +        deviceReadyChannelsArray: [],
> +        deviceReadyChannelsMap: {},
>

So, this is kind of a nasty pattern - keeping two data structures around
when one will suffice, as there's the problem of these getting out of sync
if code changes in the future.

Then I looked at channel.join.  :-)

       join: function (h, c) {
            var i = c.length;
            var len = i;
            var f = function() {
                if (!(--i)) h();
            };
            for (var j=0; j<len; j++) {
                !c[j].fired?c[j].subscribeOnce(f):i--;
            }
            if (!i) h();
        },

Yikes!  Not terribly understandable, especially since it's not immediately
obvious how a method like join() should work in the first place.  But the
good news is, I think we could go to a single "map" and not use the array,
since it doesn't need to be an array.  We can change the for loop to a for
(var key in map) loop, and then reference c[key] in the body of the loop
instead.

Issues?

-- 
Patrick Mueller
http://muellerware.org


Re: git commit: [CB-299] cordova.js should allow for registering plugins as required before deviceready fires. Use for CupcakeStorage on Android.

Posted by Patrick Mueller <pm...@gmail.com>.
On Wed, Mar 14, 2012 at 19:02, <bc...@apache.org> wrote:

> Updated Branches:
>  refs/heads/master e71e51e88 -> efdcd7703
>
> [CB-299] cordova.js should allow for registering plugins as required
> before deviceready fires.  Use for CupcakeStorage on Android.
>
> 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/efdcd770
> Tree:
> http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/efdcd770
> Diff:
> http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/efdcd770
> ...
>
>
> http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/efdcd770/lib/channel.js
> ----------------------------------------------------------------------
> diff --git a/lib/channel.js b/lib/channel.js
> old mode 100644
> new mode 100755
> index 9beed93..baf08b6
> --- a/lib/channel.js
> +++ b/lib/channel.js
> @@ -46,6 +46,45 @@ var Channel = function(type, opts) {
>         create: function (type, opts) {
>             channel[type] = new Channel(type, opts);
>             return channel[type];
> +        },
> +
> +        /**
> +         * cordova Channels that must fire before "deviceready" is fired.
> +         */
> +        deviceReadyChannelsArray: [],
> +        deviceReadyChannelsMap: {},
>

So, this is kind of a nasty pattern - keeping two data structures around
when one will suffice, as there's the problem of these getting out of sync
if code changes in the future.

Then I looked at channel.join.  :-)

       join: function (h, c) {
            var i = c.length;
            var len = i;
            var f = function() {
                if (!(--i)) h();
            };
            for (var j=0; j<len; j++) {
                !c[j].fired?c[j].subscribeOnce(f):i--;
            }
            if (!i) h();
        },

Yikes!  Not terribly understandable, especially since it's not immediately
obvious how a method like join() should work in the first place.  But the
good news is, I think we could go to a single "map" and not use the array,
since it doesn't need to be an array.  We can change the for loop to a for
(var key in map) loop, and then reference c[key] in the body of the loop
instead.

Issues?

-- 
Patrick Mueller
http://muellerware.org