Module: RocketChat::Realtime::EventEmitter

Included in:
Client
Defined in:
lib/rocket_chat/realtime/event_emitter.rb

Overview

Provide event listener support

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#emit(event, data) ⇒ Object

Publish event

Parameters:

  • event (String)

    event name

  • data (Object)

    event to publish

Since:

  • 0.1.0



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

Parameters:

  • event (String)

    event name

  • callback (Proc)

    callback to remove

Since:

  • 0.1.0



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

Parameters:

  • event (String)

    event name

  • callback (Proc)

    event handler

Since:

  • 0.1.0



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