Created
August 22, 2017 07:16
-
-
Save slugbyte/7adee7ddc9c80112b0cd31d5f1e632b2 to your computer and use it in GitHub Desktop.
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
class EE { | |
constructor(){ | |
this.listeners = {} | |
} | |
on(message, cb){ | |
if(this.listeners[message]){ | |
this.listeners[message].push(cb) | |
} else { | |
this.listeners[message] = [cb] | |
} | |
} | |
emit(message, ...args){ | |
if(this.listeners[message]){ | |
this.listeners[message].forEach(cb => cb(...args)) | |
return true | |
} | |
return false | |
} | |
} | |
e = new EE() | |
e.on('data', (data) => { | |
console.log('data', data) | |
}) | |
e.on('data', (data) => { | |
console.log('wat', data) | |
}) | |
e.emit('data', 'hello') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment