Skip to content

Instantly share code, notes, and snippets.

@jaeschrich
Created June 14, 2013 02:25
Show Gist options
  • Save jaeschrich/5779036 to your computer and use it in GitHub Desktop.
Save jaeschrich/5779036 to your computer and use it in GitHub Desktop.
CoffeScript event emitter
class Emitter
constructor: ->
@events = {}
on: (e, cb)->
@events[e] = [] unless @events[e]
@events[e].push cb
this
off: (e, cb)->
unless cb
@events[e] = []
else
index = @events[e].indexOf cb
@events[e].splice index, 1
emit: ->
args = Array.prototype.slice.call arguments
e = args.shift()
self = this
@events[e].forEach (cb)->
cb.apply self, args
this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment