Module: WebSocket::Driver::EventEmitter
- Included in:
- WebSocket::Driver, Proxy
- Defined in:
- lib/websocket/driver/event_emitter.rb
Instance Method Summary collapse
- #add_listener(event, callable = nil, &block) ⇒ Object
- #emit(event, *args) ⇒ Object
- #initialize ⇒ Object
- #listener_count(event) ⇒ Object
- #listeners(event) ⇒ Object
- #on(event, callable = nil, &block) ⇒ Object
- #remove_all_listeners(event = nil) ⇒ Object
- #remove_listener(event, callable = nil, &block) ⇒ Object
Instance Method Details
#add_listener(event, callable = nil, &block) ⇒ Object
9 10 11 12 13 |
# File 'lib/websocket/driver/event_emitter.rb', line 9 def add_listener(event, callable = nil, &block) listener = callable || block @listeners[event.to_s] << listener listener end |
#emit(event, *args) ⇒ Object
37 38 39 40 41 |
# File 'lib/websocket/driver/event_emitter.rb', line 37 def emit(event, *args) @listeners[event.to_s].dup.each do |listener| listener.call(*args) end end |
#initialize ⇒ Object
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
43 44 45 46 |
# File 'lib/websocket/driver/event_emitter.rb', line 43 def listener_count(event) return 0 unless @listeners.has_key?(event.to_s) @listeners[event.to_s].size end |
#listeners(event) ⇒ Object
48 49 50 |
# File 'lib/websocket/driver/event_emitter.rb', line 48 def listeners(event) @listeners[event.to_s] end |
#on(event, callable = nil, &block) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/websocket/driver/event_emitter.rb', line 15 def on(event, callable = nil, &block) if callable add_listener(event, callable) else add_listener(event, &block) end end |
#remove_all_listeners(event = nil) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/websocket/driver/event_emitter.rb', line 29 def remove_all_listeners(event = nil) if event @listeners.delete(event.to_s) else @listeners.clear end end |
#remove_listener(event, callable = nil, &block) ⇒ Object
23 24 25 26 27 |
# File 'lib/websocket/driver/event_emitter.rb', line 23 def remove_listener(event, callable = nil, &block) listener = callable || block @listeners[event.to_s].delete(listener) listener end |