Skip to content

Instantly share code, notes, and snippets.

@haroldiedema
Created August 11, 2017 18:41
Show Gist options
  • Save haroldiedema/2a52bece67a79524ef4715ec9ec46ab1 to your computer and use it in GitHub Desktop.
Save haroldiedema/2a52bece67a79524ef4715ec9ec46ab1 to your computer and use it in GitHub Desktop.
module.exports = Class('Lente.System.EventEmitter', {
'private string name' : '',
'private object listeners' : {},
'private object last_event' : {},
/**
* @constructor
* @param {String} name
*/
construct: function (name)
{
this.name = name || 'Untitled Event Emitter';
this.listeners = [];
},
subscribe: function (callback, priority, run_last)
{
priority = parseInt(priority || 100);
callback = callback || function () {};
this.listeners.push({
func: callback,
prio: priority
});
this.listeners.sort((a, b) => {
return a.prio > b.prio ? 1 : (a.prio < b.prio ? -1 : 0);
});
if (run_last === true) {
callback(this.last_event);
}
},
/**
* Executes subscribed listeners. Any arguments passed to this function are placed in
*
* @param {Object} e_args
*/
emit: function (e_args)
{
let e = new Lente.System.Event(this.name, e_args),
c = [];
this.last_event = e;
this.listeners.forEach((listener) => {
listener.func(e);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment