motion/reactor/eventable.rb in bubble-wrap-1.7.0 vs motion/reactor/eventable.rb in bubble-wrap-1.7.1
- old
+ new
@@ -5,33 +5,42 @@
# When `event` is triggered the block will execute
# and be passed the arguments that are passed to
# `trigger`.
def on(event, method = nil, &blk)
+ events = _events_for_key(event)
method_or_block = method ? method : blk
- __events__[event].push method_or_block
+ events.push method_or_block
end
# When `event` is triggered, do not call the given
# block any more
def off(event, method = nil, &blk)
- method_or_block = method ? method : blk
- __events__[event].delete_if { |b| b == method_or_block }
+ events = _events_for_key(event)
+ if method
+ events.delete_if { |m| m.receiver == method.receiver and m.name == method.name }
+ else
+ events.delete_if { |b| b == blk }
+ end
blk
end
# Trigger an event
def trigger(event, *args)
- blks = __events__[event].clone
+ blks = _events_for_key(event).clone
blks.map do |blk|
blk.call(*args)
end
end
private
def __events__
- @__events__ ||= Hash.new { |h,k| h[k] = [] }
+ @__events__ ||= Hash.new
+ end
+
+ def _events_for_key(event)
+ __events__[event] ||= Array.new
end
end
end
end