Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
class EventEmitter
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] if @events?
return @
listeners: (event)->
if @events?[event] then @events[event] else []
@mixin: (target)->
target[name]= fn for name,fn of @::
return @
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment