Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Tiny browser/node EventEmitter implementation in coffeescript

View EventEmitter.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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 @

@contra would this be MIT licensed?

Owner

It is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.