Module: Faye::WebSocket::API::EventTarget

Includes:
WebSocket::Driver::EventEmitter
Included in:
EventSource, Faye::WebSocket::API
Defined in:
lib/faye/websocket/api/event_target.rb

Instance Method Summary collapse

Instance Method Details

#add_event_listener(event_type, listener, use_capture = false) ⇒ Object


16
17
18
# File 'lib/faye/websocket/api/event_target.rb', line 16

def add_event_listener(event_type, listener, use_capture = false)
  add_listener(event_type, &listener)
end

#add_listener(event_type, callable = nil, &block) ⇒ Object


20
21
22
23
24
25
26
# File 'lib/faye/websocket/api/event_target.rb', line 20

def add_listener(event_type, callable = nil, &block)
  listener = callable || block
  EventMachine.next_tick do
    flush(event_type, listener)
    super(event_type, &listener)
  end
end

#dispatch_event(event) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/faye/websocket/api/event_target.rb', line 32

def dispatch_event(event)
  event.target = event.current_target = self
  event.event_phase = Event::AT_TARGET

  listener = instance_variable_get("@on#{ event.type }")
  count    = listener_count(event.type)

  unless listener or count > 0
    event_buffers[event.type].push(event)
  end

  listener.call(event) if listener
  emit(event.type, event)
end

#remove_event_listener(event_type, listener, use_capture = false) ⇒ Object


28
29
30
# File 'lib/faye/websocket/api/event_target.rb', line 28

def remove_event_listener(event_type, listener, use_capture = false)
  remove_listener(event_type, &listener)
end