Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Event emitter example
var win = Ti.UI.createWindow({backgroundColor: '#fff'})
, Emitter = require('emitter') //https://github.com/component/emitter
, event = new Emitter()
, limit = 10000
, i
, startTime
;
var buttonTi = Ti.UI.createButton({title: 'start-Ti', left: 10, enabled: true})
, buttonJs = Ti.UI.createButton({title: 'start-js', right: 10, enabled: true})
;
function toggle_buttons() {
buttonTi.enabled = !buttonTi.enabled;
buttonJs.enabled = !buttonJs.enabled;
}
win.add(buttonTi);
win.add(buttonJs);
win.open();
var start = function(){
toggle_buttons();
i = 0;
startTime = new Date();
};
var stop = function() {
var now = new Date();
console.log(now - startTime, 'ms');
toggle_buttons();
};
var eventData = {data: "to pass", to: "listener" };
buttonTi.addEventListener('click', function() {
console.log('->Starting Ti');
start();
Ti.App.fireEvent('app:custom', eventData);
});
buttonJs.addEventListener('click', function() {
console.log('->Starting JS');
start();
event.emit('app:custom', eventData );
});
Ti.App.addEventListener('app:custom', function(e) {
i++;
if(i < limit) {
Ti.App.fireEvent('app:custom', eventData);
return;
}
console.log('->Stopping Ti');
stop();
});
event.on('app:custom', function(e) {
i++;
if(i < limit) {
event.emit('app:custom', eventData);
return;
}
console.log('->Stopping Js');
stop();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment