eventable.coffee | |
---|---|
Eventable is a lightweight asynchronous replacement for node's | class Eventable
listeners: {}
|
emitter.on(name, listener(args..., [callback]))Attach a listener to the " |
on: (name, listener) ->
@listeners[name] ||= []
@listeners[name].push(listener)
|
emitter.emit(name, args..., callback)Run all listeners attached under the " |
emit: (name, args..., callback = ->) ->
|
Create a callback to track the completion of async listeners. | remaining = 1
emitter = @
listenerCallback = (err) ->
if !--remaining || err
callback(err)
|
Add the callback to the end of | args.push(listenerCallback)
|
Get all of listeners for the event | listeners = @listeners[name]
|
Fire each listener. If the listener has the right number of args to be async, increment the number of expected async listeners. | if listeners
for listener in listeners
if (listener.length >= args.length)
remaining++
listener.apply(this, args)
|
If there were no async listeners (or any that fired their callback
syncronously) the final callback will never run. To circumvent this,
we fire | listenerCallback() |
Export this class | module.exports = Eventable
|