Module: WebSocket::Driver::EventEmitter

Included in:
WebSocket::Driver
Defined in:
lib/websocket/driver/event_emitter.rb

Instance Method Summary collapse

Instance Method Details

#add_listener(event, &listener) ⇒ Object



9
10
11
# File 'lib/websocket/driver/event_emitter.rb', line 9

def add_listener(event, &listener)
  @listeners[event.to_s] << listener
end

#emit(event, *args) ⇒ Object



29
30
31
32
33
# File 'lib/websocket/driver/event_emitter.rb', line 29

def emit(event, *args)
  @listeners[event.to_s].dup.each do |listener|
    listener.call(*args)
  end
end

#initializeObject



5
6
7
# File 'lib/websocket/driver/event_emitter.rb', line 5

def initialize
  @listeners = Hash.new { |h,k| h[k] = [] }
end

#listener_count(event) ⇒ Object



35
36
37
38
# File 'lib/websocket/driver/event_emitter.rb', line 35

def listener_count(event)
  return 0 unless @listeners.has_key?(event.to_s)
  @listeners[event.to_s].size
end

#listeners(event) ⇒ Object



40
41
42
# File 'lib/websocket/driver/event_emitter.rb', line 40

def listeners(event)
  @listeners[event.to_s]
end

#on(event, &listener) ⇒ Object



13
14
15
# File 'lib/websocket/driver/event_emitter.rb', line 13

def on(event, &listener)
  add_listener(event, &listener)
end

#remove_all_listeners(event = nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/websocket/driver/event_emitter.rb', line 21

def remove_all_listeners(event = nil)
  if event
    @listeners.delete(event.to_s)
  else
    @listeners.clear
  end
end

#remove_listener(event, &listener) ⇒ Object



17
18
19
# File 'lib/websocket/driver/event_emitter.rb', line 17

def remove_listener(event, &listener)
  @listeners[event.to_s].delete(listener)
end