lib/submodules/ably-ruby/lib/ably/modules/event_emitter.rb in ably-rest-0.8.15 vs lib/submodules/ably-ruby/lib/ably/modules/event_emitter.rb in ably-rest-0.9.0
- old
+ new
@@ -48,50 +48,57 @@
#
# @param [Array<String>] event_names event name
#
# @return [void]
def on(*event_names, &block)
- add_callback event_names, proc_for_block(block)
+ event_names.each do |event_name|
+ callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block)
+ end
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)
- add_callback event_names, proc_for_block(block, unsafe: true)
+ event_names.each do |event_name|
+ callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block, unsafe: true)
+ end
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)
- add_callback event_names, proc_for_block(block, delete_once_run: true)
+ event_names.each do |event_name|
+ callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block, delete_once_run: true)
+ end
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)
- add_callback event_names, proc_for_block(block, delete_once_run: true, unsafe: true)
+ event_names.each do |event_name|
+ callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block, delete_once_run: true, unsafe: true)
+ end
end
# Emit an event with event_name that will in turn call all matching callbacks setup with `on`
def emit(event_name, *args)
- [callbacks_any, callbacks[callbacks_event_coerced(event_name)]].each do |callback_arr|
- callback_arr.clone.
+ callbacks[callbacks_event_coerced(event_name)].
+ 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|
- callback_arr.delete callback
+ callbacks[callbacks_event_coerced(event_name)].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.
@@ -112,35 +119,17 @@
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|
@@ -152,13 +141,9 @@
}
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)