Sha256: 3e748fcc3f2d5f79f08e052c06238528d7e60b140f7e47b6254abd201a8160a5
Contents?: true
Size: 1.46 KB
Versions: 2
Compression:
Stored size: 1.46 KB
Contents
module Commute # Internal: A Simple EventEmitter modelled after node.js' EventEmitter. # More info: http://nodejs.org/api/events.html # # Include this module in any model that wants to send events to its listeners. # # Listen to events using `on(:event)`. In the model, notify listeners # by calling `emit(:event, ...)` with some arguments. module EventEmitter # Internal: Initializes an empty handler hash. def initialize *args super *args @handlers = Hash.new { |h, k| h[k] = [] } end # Internal: Subscribe a handler to an event. # # event - The name of the event to listen for. # handler - The proc to call when this event occurs. # # Returns Nothing. def on event, &handler @handlers[event] << handler nil end # Internal: Subscribe a handler to an event and # unsuscribe it when the event fired once. # def once event, &handler on event do |*args, &block| handler.call *args, &block remove event, handler end end # Internal: Remove an event handler. def remove event, handler @handlers[event].delete handler end protected # Internal: Emit an event to all handlers. # # event - The name of the event. # args - Arguments to send to the listeners. # # Returns the notified handlers. def emit event, *args @handlers[event].each do |handler| handler.call *args end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
commute-0.3.0.pre.2 | lib/commute/core/util/event_emitter.rb |
commute-0.3.0.pre | lib/commute/core/util/event_emitter.rb |