Cleaning Up App.EventListeners on restart of App

109 views
Skip to first unread message

Jeff Bonnes

unread,
Sep 6, 2013, 5:54:14 PM9/6/13
to tish...@googlegroups.com
I'm working with an App in TiShadow that has some Global event listeners. [Ti.App.addEventListeners].  I've created a 'wrapping' function to try to remove all the listeners on App startup if using TiShadow, but I'm not having much luck. They keep getting 're-regsitered' every time the App reruns and never removed.   Hence, they are firing multiple times. Is there a best practice for this?

listeners.js:
var globalListeners = [];

exports.registerGlobalListener = function(evt, _function) {
globalListeners.push({
evt : evt,
func : _function
});
Ti.App.addEventListener(evt, _function);
};

exports.clearAllListeners = function() {
Ti.API.info('clearing all global listeners');  // <- This never runs...
_.each(globalListeners, function(listener) {
Ti.App.removeEventListener(listener.evt, listener.func);
});
globalListeners = [];
};

//Code

listeners.registerGlobalListener('resume', function() {

 // Do Something

});

app.js
// Shim for TiShadow
try {
Ti.API.info("Running in TiShadow");
exports.close = function() {
listeners.clearAllListeners();
tabGroup.close();
};
} catch (e) {
Ti.API.info("Running stand-alone");
}

David Bankier

unread,
Sep 9, 2013, 11:42:23 PM9/9/13
to tish...@googlegroups.com
Hi Jeff,

(I know I sent this to you directly, but I'm posting here again on the group for others.)

The latest version 1.3.2 does all this for you. It tracks and clears all app wide listeners between launches.
Here is the commit that added it: https://github.com/dbankier/TiShadow/commit/e2f76a2bceeec3a054a3b0c0e260681d8b409aa9

David

Jeff Bonnes

unread,
Sep 10, 2013, 6:46:56 PM9/10/13
to tish...@googlegroups.com
Thanks David.  I was still running 1.2.1 - I'll test this on 1.3.2.

Alberto Marcone

unread,
Oct 9, 2013, 6:33:59 AM10/9/13
to tish...@googlegroups.com
This was very helpful, thank you.

Ian Duggan

unread,
Nov 18, 2013, 9:02:04 PM11/18/13
to tish...@googlegroups.com
Is there something similar for modules? I'm seeing login/logout handlers on the Facebook module stick around in 2.1.1. I presume this is because the listeners are attached to binary module and thus not seen by this code? Is there any way to register listeners with tishadow that should be unregistered on a new launch?

--Ian

David Bankier

unread,
Nov 20, 2013, 9:09:47 PM11/20/13
to tish...@googlegroups.com
Hi Ian,

If you need to cleanup anything else add a "close" listener to your base window and do the clean up there. All windows are closed between runs to the "close" event will be fired.

David
Reply all
Reply to author
Forward
0 new messages