-
-
Save eliot-akira/a15834820400c7f2752c to your computer and use it in GitHub Desktop.
Tiny browser/node EventEmitter implementation in coffeescript
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
class EventEmitter | |
constructor: -> | |
@events = {} | |
emit: (event, args...) -> | |
return false unless @events[event] | |
listener args... for listener in @events[event] | |
return true | |
addListener: (event, listener) -> | |
@emit 'newListener', event, listener | |
(@events[event]?=[]).push listener | |
return @ | |
on: @::addListener | |
once: (event, listener) -> | |
fn = => | |
@removeListener event, fn | |
listener arguments... | |
@on event, fn | |
return @ | |
removeListener: (event, listener) -> | |
return @ unless @events[event] | |
@events[event] = (l for l in @events[event] when l isnt listener) | |
return @ | |
removeAllListeners: (event) -> | |
delete @events[event] | |
return @ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment