Skip to content

Instantly share code, notes, and snippets.

@jjt
Forked from arboleya/microevent.coffee
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjt/ab9f0c22c680acff6045 to your computer and use it in GitHub Desktop.
Save jjt/ab9f0c22c680acff6045 to your computer and use it in GitHub Desktop.
###
Port of MicroEvent in Coffeescript with some naming modifications
and a new 'once' method.
Original project:
https://github.com/jeromeetienne/microevent.js
###
class MicroEventClass
_init:-> @_listn or @_listn = {}
_create:(e)-> @_init()[e] or @_init()[e] = []
on:(e, f)-> (@_create e).push f
off:(e, f)-> (t.splice (t.indexOf f), 1) if (t = @_init()[e])?
once:(e, f)-> @on e, (t = => (@off e, t) && f.apply @, arguments)
emit:(e, r...)-> l.apply @, r for l in t if (t = @_init()[e])?; 0
@mixin=(t)-> t::[p] = @::[p] for p of @::; 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment