Module: RocketChat::Realtime::EventEmitter
- Included in:
- Client
- Defined in:
- lib/rocket_chat/realtime/event_emitter.rb
Overview
Provide event listener support
Instance Method Summary collapse
-
#emit(event, data) ⇒ Object
Publish event.
-
#off(event, &callback) ⇒ Object
Deregister event listener.
-
#on(event, &callback) ⇒ Object
Register event listener.
Instance Method Details
#emit(event, data) ⇒ Object
Publish event
47 48 49 50 51 |
# File 'lib/rocket_chat/realtime/event_emitter.rb', line 47 def emit(event, data) listeners[event.to_s]&.each do |listener| listener.call(data) end end |
#off(event, &callback) ⇒ Object
Deregister event listener
31 32 33 34 35 36 37 38 39 |
# File 'lib/rocket_chat/realtime/event_emitter.rb', line 31 def off(event, &callback) listeners.compute(event.to_s) do |current| current ||= [] current.delete(callback) return nil if current.empty? current end end |
#on(event, &callback) ⇒ Object
Register event listener
17 18 19 20 21 22 23 |
# File 'lib/rocket_chat/realtime/event_emitter.rb', line 17 def on(event, &callback) listeners.compute(event.to_s) do |current| current ||= [] current.push(callback) current end end |