lib/ably/modules/event_emitter.rb in ably-0.8.14 vs lib/ably/modules/event_emitter.rb in ably-0.8.15
- old
+ new
@@ -48,57 +48,50 @@
#
# @param [Array<String>] event_names event name
#
# @return [void]
def on(*event_names, &block)
- event_names.each do |event_name|
- callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block)
- end
+ add_callback event_names, proc_for_block(block)
end
# Equivalent of {#on} but any exception raised in a block will bubble up and cause this client library to fail.
# This method should only be used internally by the client library.
# @api private
def unsafe_on(*event_names, &block)
- event_names.each do |event_name|
- callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block, unsafe: true)
- end
+ add_callback event_names, proc_for_block(block, unsafe: true)
end
# On receiving an event maching the event_name, call the provided block only once and remove the registered callback
#
# @param [Array<String>] event_names event name
#
# @return [void]
def once(*event_names, &block)
- event_names.each do |event_name|
- callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block, delete_once_run: true)
- end
+ add_callback event_names, proc_for_block(block, delete_once_run: true)
end
# Equivalent of {#once} but any exception raised in a block will bubble up and cause this client library to fail.
# This method should only be used internally by the client library.
# @api private
def unsafe_once(*event_names, &block)
- event_names.each do |event_name|
- callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block, delete_once_run: true, unsafe: true)
- end
+ add_callback event_names, proc_for_block(block, delete_once_run: true, unsafe: true)
end
# Emit an event with event_name that will in turn call all matching callbacks setup with `on`
def emit(event_name, *args)
- callbacks[callbacks_event_coerced(event_name)].
- clone.
+ [callbacks_any, callbacks[callbacks_event_coerced(event_name)]].each do |callback_arr|
+ callback_arr.clone.
select do |proc_hash|
if proc_hash[:unsafe]
proc_hash[:emit_proc].call(*args)
else
safe_yield proc_hash[:emit_proc], *args
end
end.each do |callback|
- callbacks[callbacks_event_coerced(event_name)].delete callback
+ callback_arr.delete callback
end
+ end
end
# Remove all callbacks for event_name.
#
# If a block is provided, only callbacks matching that block signature will be removed.
@@ -119,17 +112,35 @@
callbacks[callbacks_event_coerced(event_name)].delete_if { |proc_hash| proc_hash[:block] == block }
else
callbacks[callbacks_event_coerced(event_name)].clear
end
end
+
+ if event_names.empty?
+ if block_given?
+ callbacks_any.delete_if { |proc_hash| proc_hash[:block] == block }
+ else
+ callbacks_any.clear
+ end
+ end
end
private
def self.included(klass)
klass.extend ClassMethods
end
+ def add_callback(event_names, proc_block)
+ if event_names.empty?
+ callbacks_any << proc_block
+ else
+ event_names.each do |event_name|
+ callbacks[callbacks_event_coerced(event_name)] << proc_block
+ end
+ end
+ end
+
# Create a Hash with a proc that calls the provided block and returns true if option :delete_once_run is set to true.
# #emit automatically deletes any blocks that return true thus allowing a block to be run once
def proc_for_block(block, options = {})
{
emit_proc: Proc.new do |*args|
@@ -141,9 +152,13 @@
}
end
def callbacks
@callbacks ||= Hash.new { |hash, key| hash[key] = [] }
+ end
+
+ def callbacks_any
+ @callbacks_any ||= []
end
def callbacks_event_coerced(event_name)
if self.class.event_emitter_coerce_proc
self.class.event_emitter_coerce_proc.call(event_name)