Skip to content

Commit

Permalink
Only load JNEXT when it's actually needed, provides faster startup ti…
Browse files Browse the repository at this point in the history
…mes and lower memory usage in apps

Fixes blackberry#277

Reviewed By: Nukul Bhasin <nbhasin@rim.com>
Tested By: Tracy Li <tli@rim.com>
  • Loading branch information
Eric Pearson committed Nov 15, 2012
1 parent 0be029c commit 10add31
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 54 deletions.
8 changes: 4 additions & 4 deletions ext/bbm.platform/BBMEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ var bbm = require("./BBMJNEXT").bbm;
module.exports = {
addEventListener: function (event, trigger) {
if (event === "onaccesschanged") {
bbm.startEvents(trigger);
bbm.getInstance().startEvents(trigger);
} else if (event === "onupdate") {
bbm.startContactEvents(trigger);
bbm.getInstance().startContactEvents(trigger);
} else {
console.log("Ignore registration for unknown event: " + event);
}
},
removeEventListener: function (event) {
if (event === "onaccesschanged") {
bbm.stopEvents();
bbm.getInstance().stopEvents();
} else if (event === "onupdate") {
bbm.stopContactEvents();
bbm.getInstance().stopContactEvents();
} else {
console.log("Ignore un-registration for unknown event: " + event);
}
Expand Down
12 changes: 10 additions & 2 deletions ext/bbm.platform/BBMJNEXT.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ var bbm,

JNEXT.BBM = function ()
{
var _self = this;
var _self = this,
hasInstance = false;

_self.self = {};
_self.users = {};

Expand Down Expand Up @@ -128,7 +130,13 @@ JNEXT.BBM = function ()
_self.m_id = "";
_self.displayPictureEventId = "";

_self.init();
_self.getInstance = function () {
if (!hasInstance) {
_self.init();
hasInstance = true;
}
return _self;
};
};

bbm = new JNEXT.BBM();
Expand Down
28 changes: 14 additions & 14 deletions ext/bbm.platform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,49 +62,49 @@ module.exports = {
return;
}

bbm.register(args.options);
bbm.getInstance().register(args.options);
success();
}
},

self : {
appVersion : function (success, fail, args, env) {
success(bbm.self.getProfile(BBM_APP_VERSION));
success(bbm.getInstance().self.getProfile(BBM_APP_VERSION));
},

bbmsdkVersion : function (success, fail, args, env) {
success(parseInt(bbm.self.getProfile(BBM_SDK_VERSION), 10));
success(parseInt(bbm.getInstance().self.getProfile(BBM_SDK_VERSION), 10));
},

displayName : function (success, fail, args, env) {
success(bbm.self.getProfile(BBM_DISPLAY_NAME));
success(bbm.getInstance().self.getProfile(BBM_DISPLAY_NAME));
},

handle : function (success, fail, args, env) {
success(bbm.self.getProfile(BBM_HANDLE));
success(bbm.getInstance().self.getProfile(BBM_HANDLE));
},

personalMessage : function (success, fail, args, env) {
success(bbm.self.getProfile(BBM_PERSONAL_MESSAGE));
success(bbm.getInstance().self.getProfile(BBM_PERSONAL_MESSAGE));
},

ppid : function (success, fail, args, env) {
success(bbm.self.getProfile(BBM_PPID));
success(bbm.getInstance().self.getProfile(BBM_PPID));
},

status : function (success, fail, args, env) {
success(bbm.self.getProfile(BBM_STATUS));
success(bbm.getInstance().self.getProfile(BBM_STATUS));
},

statusMessage : function (success, fail, args, env) {
success(bbm.self.getProfile(BBM_STATUS_MESSAGE));
success(bbm.getInstance().self.getProfile(BBM_STATUS_MESSAGE));
},

getDisplayPicture : function (success, fail, args, env) {
if (args) {
args.eventId = JSON.parse(decodeURIComponent(args.eventId));

bbm.self.getDisplayPicture(args.eventId);
bbm.getInstance().self.getDisplayPicture(args.eventId);
success();
}
},
Expand All @@ -120,7 +120,7 @@ module.exports = {
}
}

bbm.self.setStatus(args);
bbm.getInstance().self.setStatus(args);
success();
},

Expand All @@ -134,7 +134,7 @@ module.exports = {
}
}

bbm.self.setPersonalMessage(args.personalMessage);
bbm.getInstance().self.setPersonalMessage(args.personalMessage);
success();
},

Expand All @@ -148,14 +148,14 @@ module.exports = {
}
}

bbm.self.setDisplayPicture(args.displayPicture);
bbm.getInstance().self.setDisplayPicture(args.displayPicture);
success();
}
},

users : {
inviteToDownload : function (success, fail, args) {
bbm.users.inviteToDownload();
bbm.getInstance().users.inviteToDownload();
success();
}
}
Expand Down
15 changes: 11 additions & 4 deletions ext/io.filetransfer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = {
}
}

filetransfer.upload(params);
filetransfer.getInstance().upload(params);
success();
},

Expand Down Expand Up @@ -110,7 +110,7 @@ module.exports = {

args.windowGroup = _webview.windowGroup();

filetransfer.download(args);
filetransfer.getInstance().download(args);
success();
}
};
Expand All @@ -121,7 +121,8 @@ module.exports = {
///////////////////////////////////////////////////////////////////

JNEXT.FileTransfer = function () {
var self = this;
var self = this,
hasInstance = false;

self.upload = function (args) {
return JNEXT.invoke(self.m_id, "upload " + JSON.stringify(args));
Expand Down Expand Up @@ -193,7 +194,13 @@ JNEXT.FileTransfer = function () {

self.m_id = "";

self.init();
self.getInstance = function () {
if (!hasInstance) {
self.init();
hasInstance = true;
}
return self;
};
};

filetransfer = new JNEXT.FileTransfer();
17 changes: 12 additions & 5 deletions ext/pim.contacts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = {
return;
}

pimContacts.find(findOptions);
pimContacts.getInstance().find(findOptions);

success();
},
Expand All @@ -81,7 +81,7 @@ module.exports = {
return;
}

pimContacts.save(attributes);
pimContacts.getInstance().save(attributes);
success();
},

Expand All @@ -93,7 +93,7 @@ module.exports = {
return;
}

pimContacts.remove(attributes);
pimContacts.getInstance().remove(attributes);
success();
}
};
Expand All @@ -104,7 +104,8 @@ module.exports = {

JNEXT.PimContacts = function ()
{
var self = this;
var self = this,
hasInstance = false;

self.find = function (args) {
JNEXT.invoke(self.m_id, "find " + JSON.stringify(args));
Expand Down Expand Up @@ -152,7 +153,13 @@ JNEXT.PimContacts = function ()

self.m_id = "";

self.init();
self.getInstance = function () {
if (!hasInstance) {
self.init();
hasInstance = true;
}
return self;
};
};

pimContacts = new JNEXT.PimContacts();
23 changes: 15 additions & 8 deletions ext/push/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,35 @@ module.exports = {
"appId" : JSON.parse(decodeURIComponent(args.appId)),
"ppgUrl" : JSON.parse(decodeURIComponent(args.ppgUrl)) };

_push.startService(pushOptions);
_push.getInstance().startService(pushOptions);
success();
},

createChannel: function (success, fail, args) {
_push.createChannel(args);
_push.getInstance().createChannel(args);
success();
},

destroyChannel: function (success, fail, args) {
_push.destroyChannel(args);
_push.getInstance().destroyChannel(args);
success();
},

extractPushPayload: function (success, fail, args) {
var invokeData = { "data" : JSON.parse(decodeURIComponent(args.data)) };
success(_push.extractPushPayload(invokeData));
success(_push.getInstance().extractPushPayload(invokeData));
},

launchApplicationOnPush: function (success, fail, args) {
_push.launchApplicationOnPush(JSON.parse(decodeURIComponent(args.shouldLaunch)));
_push.getInstance().launchApplicationOnPush(JSON.parse(decodeURIComponent(args.shouldLaunch)));
success();
},

acknowledge: function (success, fail, args) {
var acknowledgeData = { "id" : JSON.parse(decodeURIComponent(args.id)),
"shouldAcceptPush" : JSON.parse(decodeURIComponent(args.shouldAcceptPush)) };

_push.acknowledge(acknowledgeData);
_push.getInstance().acknowledge(acknowledgeData);
success();
}
};
Expand All @@ -62,7 +62,8 @@ module.exports = {
///////////////////////////////////////////////////////////////////

JNEXT.Push = function () {
var self = this;
var self = this,
hasInstance = false;

self.startService = function (args) {
JNEXT.invoke(self.m_id, "startService " + JSON.stringify(args));
Expand Down Expand Up @@ -145,7 +146,13 @@ JNEXT.Push = function () {

self.m_id = "";

self.init();
self.getInstance = function () {
if (!hasInstance) {
hasInstance = true;
self.init();
}
return self;
};
};

_push = new JNEXT.Push();
2 changes: 1 addition & 1 deletion ext/sensors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = {
if (args.options.sensor === "") {
fail(-1, "Must specify a sensor");
}
success(sensors.setOptions(args.options));
success(sensors.getInstance().setOptions(args.options));
} else {
fail(-1, "Need to specify arguments");
}
Expand Down
4 changes: 2 additions & 2 deletions ext/sensors/sensorsEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ var sensors = require("./sensorsJNEXT").sensors,
module.exports = {
addEventListener: function (event, trigger) {
if (events.indexOf(event) !== -1) {
sensors.startSensor(event, trigger);
sensors.getInstance().startSensor(event, trigger);
} else {
console.log("Ignore registration for unknown event: " + event);
}
},
removeEventListener: function (event) {
if (events.indexOf(event) !== -1) {
sensors.stopSensor(event);
sensors.getInstance().stopSensor(event);
} else {
console.log("Ignore un-registration for unknown event: " + event);
}
Expand Down
12 changes: 10 additions & 2 deletions ext/sensors/sensorsJNEXT.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var sensors,
///////////////////////////////////////////////////////////////////

JNEXT.Sensors = function () {
var self = this;
var self = this,
hasInstance = false;

self.startSensor = function (sensor, callback) {
callbackMap[sensor] = callback;
Expand Down Expand Up @@ -67,7 +68,14 @@ JNEXT.Sensors = function () {

self.m_id = "";

self.init();
self.getInstance = function () {
if (!hasInstance) {
hasInstance = true;
self.init();
}
return self;
};

};

sensors = new JNEXT.Sensors();
Expand Down
6 changes: 3 additions & 3 deletions test/unit/ext/bbm.platform/BBMEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ describe("bbm.platform BBMEvents", function () {
BBMEvents = null;
});

it("can access libbbm module in JNEXT", function () {
expect(JNEXT.require).toHaveBeenCalledWith("libbbm");
expect(JNEXT.createObject).toHaveBeenCalledWith("libbbm.BBM");
it("checks that JNEXT was not initialized on require", function () {
expect(JNEXT.require).not.toHaveBeenCalledWith("libbbm");
expect(JNEXT.createObject).not.toHaveBeenCalledWith("libbbm.BBM");
});

describe("onaccesschanged event", function () {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/ext/io.filetransfer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ describe("io.filetransfer index", function () {
index = null;
});

it("can access libfiletransfer module in JNEXT", function () {
expect(JNEXT.require).toHaveBeenCalledWith("libfiletransfer");
expect(JNEXT.createObject).toHaveBeenCalledWith("libfiletransfer.FileTransfer");
it("makes sure JNEXT was not initialized on require", function () {
expect(JNEXT.require).not.toHaveBeenCalledWith("libfiletransfer");
expect(JNEXT.createObject).not.toHaveBeenCalledWith("libfiletransfer.FileTransfer");
});

describe("filetransfer upload", function () {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/ext/pim.contacts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ describe("pim.contacts index", function () {
index = null;
});

it("JNEXT require/createObject/registerEvents are called upon requiring index", function () {
expect(JNEXT.require).toHaveBeenCalledWith("libpimcontacts");
expect(JNEXT.createObject).toHaveBeenCalledWith("libpimcontacts.PimContacts");
expect(JNEXT.registerEvents).toHaveBeenCalled();
it("JNEXT require/createObject/registerEvents are not called upon requiring index", function () {
expect(JNEXT.require).not.toHaveBeenCalledWith("libpimcontacts");
expect(JNEXT.createObject).not.toHaveBeenCalledWith("libpimcontacts.PimContacts");
expect(JNEXT.registerEvents).not.toHaveBeenCalled();
});

it("find - with correct permission specified", function () {
Expand Down
Loading

0 comments on commit 10add31

Please sign in to comment.