lib/ably/modules/event_emitter.rb in ably-0.8.15 vs lib/ably/modules/event_emitter.rb in ably-1.0.0
- old
+ new
@@ -52,11 +52,11 @@
def on(*event_names, &block)
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.
+ # This method is designed to 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)
end
@@ -68,11 +68,11 @@
def once(*event_names, &block)
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.
+ # This method is designed to 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)
end
@@ -99,34 +99,49 @@
#
# @param [Array<String>] event_names event name
#
# @return [void]
def off(*event_names, &block)
+ off_internal(false, *event_names, &block)
+ end
+
+ # Equivalent of {#off} but only unsafe listeners are removed.
+ # This method is designed to be used internally by the client library.
+ # @api private
+ def unsafe_off(*event_names, &block)
+ off_internal(true, *event_names, &block)
+ end
+
+ private
+ def off_internal(unsafe, *event_names, &block)
keys = if event_names.empty?
callbacks.keys
else
event_names
end
- keys.each do |event_name|
- if block_given?
- callbacks[callbacks_event_coerced(event_name)].delete_if { |proc_hash| proc_hash[:block] == block }
- else
- callbacks[callbacks_event_coerced(event_name)].clear
+ if event_names.empty?
+ callbacks_any.delete_if do |proc_hash|
+ if block_given?
+ (proc_hash[:unsafe] == unsafe) && (proc_hash[:block] == block)
+ else
+ proc_hash[:unsafe] == unsafe
+ end
end
end
- if event_names.empty?
- if block_given?
- callbacks_any.delete_if { |proc_hash| proc_hash[:block] == block }
- else
- callbacks_any.clear
+ keys.each do |event_name|
+ callbacks[callbacks_event_coerced(event_name)].delete_if do |proc_hash|
+ if block_given?
+ (proc_hash[:unsafe] == unsafe) && (proc_hash[:block] == block)
+ else
+ proc_hash[:unsafe] == unsafe
+ end
end
end
end
- private
def self.included(klass)
klass.extend ClassMethods
end
def add_callback(event_names, proc_block)
@@ -146,10 +161,10 @@
emit_proc: Proc.new do |*args|
block.call(*args)
true if options[:delete_once_run]
end,
block: block,
- unsafe: options[:unsafe]
+ unsafe: options[:unsafe] || false
}
end
def callbacks
@callbacks ||= Hash.new { |hash, key| hash[key] = [] }