lib/faye/websocket/api/event_target.rb in faye-websocket-0.9.2 vs lib/faye/websocket/api/event_target.rb in faye-websocket-0.10.0
- old
+ new
@@ -5,23 +5,24 @@
events = %w[open message error close]
events.each do |event_type|
define_method "on#{event_type}=" do |handler|
EventMachine.next_tick do
- flush(event_type, &handler)
+ flush(event_type, handler)
instance_variable_set("@on#{event_type}", handler)
end
end
end
def add_event_listener(event_type, listener, use_capture = false)
add_listener(event_type, &listener)
end
- def add_listener(event_type, &listener)
+ def add_listener(event_type, callable = nil, &block)
+ listener = callable || block
EventMachine.next_tick do
- flush(event_type, &listener)
+ flush(event_type, listener)
super(event_type, &listener)
end
end
def remove_event_listener(event_type, listener, use_capture = false)
@@ -30,26 +31,26 @@
def dispatch_event(event)
event.target = event.current_target = self
event.event_phase = Event::AT_TARGET
- callback = instance_variable_get("@on#{ event.type }")
+ listener = instance_variable_get("@on#{ event.type }")
count = listener_count(event.type)
- unless callback or count > 0
+ unless listener or count > 0
@buffers ||= Hash.new { |k,v| k[v] = [] }
@buffers[event.type].push(event)
end
- callback.call(event) if callback
+ listener.call(event) if listener
emit(event.type, event)
end
private
- def flush(event_type, &callback)
+ def flush(event_type, listener)
if buffer = @buffers && @buffers.delete(event_type.to_s)
- buffer.each { |event| callback.call(event) }
+ buffer.each { |event| listener.call(event) }
end
end
end
end