Skip to content

Instantly share code, notes, and snippets.

@meeech
Last active December 18, 2015 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meeech/568669ea435afe246a11 to your computer and use it in GitHub Desktop.
Save meeech/568669ea435afe246a11 to your computer and use it in GitHub Desktop.
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