lib/faye/websocket/api/event_target.rb in faye-websocket-0.4.7-java vs lib/faye/websocket/api/event_target.rb in faye-websocket-0.5.0
- old
+ new
@@ -1,32 +1,55 @@
module Faye::WebSocket::API
module EventTarget
- attr_accessor :onopen, :onmessage, :onerror, :onclose
+ include ::WebSocket::Driver::EventEmitter
+ 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)
+ instance_variable_set("@on#{event_type}", handler)
+ end
+ end
+ end
+
def add_event_listener(event_type, listener, use_capture = false)
- @listeners ||= {}
- list = @listeners[event_type] ||= []
- list << listener
+ add_listener(event_type, &listener)
end
- def remove_event_listener(event_type, listener, use_capture = false)
- return unless @listeners and @listeners[event_type]
- return @listeners.delete(event_type) unless listener
+ def add_listener(event_type, &listener)
+ EventMachine.next_tick do
+ flush(event_type, &listener)
+ super(event_type, &listener)
+ end
+ end
- @listeners[event_type].delete_if(&listener.method(:==))
+ def remove_event_listener(event_type, listener, use_capture = false)
+ remove_listener(event_type, &listener)
end
def dispatch_event(event)
event.target = event.current_target = self
event.event_phase = Event::AT_TARGET
- callback = __send__("on#{ event.type }")
+ callback = instance_variable_get("@on#{ event.type }")
+ count = listener_count(event.type)
+
+ unless callback or count > 0
+ @buffers ||= Hash.new { |k,v| k[v] = [] }
+ @buffers[event.type].push(event)
+ end
+
callback.call(event) if callback
+ emit(event.type, event)
+ end
- return unless @listeners and @listeners[event.type]
- @listeners[event.type].each do |listener|
- listener.call(event)
+ private
+
+ def flush(event_type, &callback)
+ if buffer = @buffers && @buffers.delete(event_type.to_s)
+ buffer.each { |event| callback.call(event) }
end
end
end
end