Created
August 11, 2017 18:41
-
-
Save haroldiedema/2a52bece67a79524ef4715ec9ec46ab1 to your computer and use it in GitHub Desktop.
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
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