From 31ddef6338991542fe0d6302e1e5f4545ac1f7ef Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Mon, 16 Nov 2015 13:21:44 +0100 Subject: [PATCH] wrote a very small custom event emitter --- lib/EventEmitter.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lib/EventEmitter.js diff --git a/lib/EventEmitter.js b/lib/EventEmitter.js new file mode 100644 index 0000000..3716a5f --- /dev/null +++ b/lib/EventEmitter.js @@ -0,0 +1,25 @@ +// Custom event emitter for boards-api + +var EventEmitter = function(){ + this.listeners = {} +} + +EventEmitter.prototype.on = function(name,handler){ + if(this.listeners[name] === undefined) + this.listeners[name] = [] + else if(handler && handler.apply && this.listeners[name].indexOf(handler) < 0) + this.listeners[name].push(handler) +} + +EventEmitter.prototype.emit = function(name,arg){ + var args = [] + for(var a in arguments) if(a > 0) args.push(arguments[a]) + if(this.listeners[name]){ + for(var i in this.listeners[name]){ + var listener = this.listeners[name][i] + if(listener && listener.apply) listener.apply(this,args) + } + } +} + +module.exports = EventEmitter