Last active
December 20, 2015 15:39
-
-
Save nathggns/6155633 to your computer and use it in GitHub Desktop.
Nice little TickEventEmitter (EventEmitter extension with automatic tick support)
This file contains hidden or 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
define(['lib/EventEmitter', 'lib/heir'], function(EventEmitter) { | |
/** | |
* This is an extended version of EventEmitter with "tick" events | |
* built in. Extending this will also give you EventEmitter functionality. | |
*/ | |
var TickEventEmitter = function TickEventEmitter() { | |
}; | |
TickEventEmitter.inherit(EventEmitter); | |
/** | |
* This sets up TickEventEmitter | |
*/ | |
TickEventEmitter.prototype.initTick = function() { | |
this._initTick = true; | |
/** | |
* We'll create a binded version of the tick method, | |
* to speed stuff up | |
*/ | |
this.tick = this.tick.bind(this); | |
/** | |
* Create a version of trigger that only triggers | |
* the tick event, for setTimeout | |
*/ | |
this.trigger_tick = this.trigger.bind(this, 'tick'); | |
/** | |
* Start the tick loop | |
*/ | |
this.tick(); | |
}; | |
/** | |
* Have we initiated TickEventEmitter yet? | |
* @type {Boolean} | |
*/ | |
TickEventEmitter.prototype._initTick = false; | |
/** | |
* We'll overwrite addListener, so we can "pretend" autoinit. | |
*/ | |
TickEventEmitter.prototype.addListener = | |
TickEventEmitter.prototype.on = function() { | |
if (!this._initTick) { | |
this.initTick(); | |
} | |
return EventEmitter.prototype.addListener.apply(this, arguments); | |
}; | |
/** | |
* This variable controls the stopping of ticking. | |
* Set this to false to stop the ticking | |
* @type {Boolean} | |
*/ | |
TickEventEmitter.prototype.ticking = true; | |
/** | |
* This function is called for each tick. | |
*/ | |
TickEventEmitter.prototype.tick = function() { | |
if (!this.ticking) { | |
return; | |
} | |
setTimeout(this.trigger_tick); | |
setTimeout(this.tick); | |
}; | |
return TickEventEmitter; | |
}); |
This file contains hidden or 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
require(['TickEventEmitter'], function(TickEventEmitter) { | |
var Thing = function(){ | |
this.started = Date.getTime(); | |
this.elapsed = 0; | |
this.on('tick', this.updateElapsed); | |
}; | |
Thing.inherit(TickEventEmitter); | |
Thing.prototype.updateElapsed = function() { | |
this.elapsed = Date.getTime() - this.started; | |
if (this.elapsed % 60000 === 0) { | |
this.trigger('minute'); | |
} | |
if (this.elapsed % (60 * 60 * 1000) === 0) { | |
this.trigger('hour'); | |
} | |
// ... | |
}; | |
var thing = new Thing(); | |
thing.on('minute', function() { | |
alert('Minute has passed!'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment