-
-
Save meeech/568669ea435afe246a11 to your computer and use it in GitHub Desktop.
Event emitter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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