mirror of
https://github.com/fazo96/ipfs-boards
synced 2025-01-10 12:24:20 +01:00
wrote a very small custom event emitter
This commit is contained in:
parent
9344b360d7
commit
31ddef6338
25
lib/EventEmitter.js
Normal file
25
lib/EventEmitter.js
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user