/
microevent.coffee
36 lines (29 loc) · 1.02 KB
/
microevent.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
32
33
34
35
# This is a simple port of microevent.js to Coffeescript. I've changed the
# function names to be consistent with node.js EventEmitter.
#
# microevent.js is copyright Jerome Etienne, and licensed under the MIT license:
# https://github.com/jeromeetienne/microevent.js
class MicroEvent
on: (event, fct) ->
@_events ||= {}
@_events[event] ||= []
@_events[event].push(fct)
this
removeListener: (event, fct) ->
@_events ||= {}
idx = @_events[event]?.indexOf fct
@_events[event].splice(idx, 1) if idx? and idx >= 0
this
emit: (event, args...) ->
return this unless @_events?[event]
fn.apply this, args for fn in @_events[event]
this
# mixin will delegate all MicroEvent.js function in the destination object
MicroEvent.mixin = (obj) ->
proto = obj.prototype || obj
# Damn closure compiler :/
proto.on = proto['on'] = MicroEvent.prototype.on
proto.removeListener = proto['removeListener'] = MicroEvent.prototype.removeListener
proto.emit = MicroEvent.prototype.emit
obj
module.exports = MicroEvent if module?.exports