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

Included in:
Faye::WebSocket::API
Defined in:
lib/faye/websocket/api/event_target.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#oncloseObject

Returns the value of attribute onclose.



4
5
6
# File 'lib/faye/websocket/api/event_target.rb', line 4

def onclose
  @onclose
end

#onerrorObject

Returns the value of attribute onerror.



4
5
6
# File 'lib/faye/websocket/api/event_target.rb', line 4

def onerror
  @onerror
end

#onmessageObject

Returns the value of attribute onmessage.



4
5
6
# File 'lib/faye/websocket/api/event_target.rb', line 4

def onmessage
  @onmessage
end

#onopenObject

Returns the value of attribute onopen.



4
5
6
# File 'lib/faye/websocket/api/event_target.rb', line 4

def onopen
  @onopen
end

Instance Method Details

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



6
7
8
9
10
# File 'lib/faye/websocket/api/event_target.rb', line 6

def add_event_listener(event_type, listener, use_capture = false)
  @listeners ||= {}
  list = @listeners[event_type] ||= []
  list << listener
end

#dispatch_event(event) ⇒ Object



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

def dispatch_event(event)
  event.target = event.current_target = self
  event.event_phase = Event::AT_TARGET
  
  callback = __send__("on#{ event.type }")
  callback.call(event) if callback
  
  return unless @listeners and @listeners[event.type]
  @listeners[event.type].each do |listener|
    listener.call(event)
  end
end

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



12
13
14
15
16
17
# File 'lib/faye/websocket/api/event_target.rb', line 12

def remove_event_listener(event_type, listener, use_capture = false)
  return unless @listeners and @listeners[event_type]
  return @listeners.delete(event_type) unless listener
  
  @listeners[event_type].delete_if(&listener.method(:==))
end