Class: ROM::Notifications::EventBus

Inherits:
Object
  • Object
show all
Includes:
Publisher
Defined in:
lib/rom/support/notifications.rb

Overview

Event bus

An event bus stores listeners (callbacks) and events

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, events: EMPTY_HASH) ⇒ EventBus

Initialize a new event bus

Parameters:

  • id (Symbol)

    The bus identifier

  • events (Hash) (defaults to: EMPTY_HASH)

    A hash with events

  • listeners (Hash)

    A hash with listeners



231
232
233
234
235
# File 'lib/rom/support/notifications.rb', line 231

def initialize(id, events: EMPTY_HASH)
  @id = id
  @listeners = LISTENERS_HASH.dup
  @events = events
end

Instance Attribute Details

#eventsObject (readonly)



218
219
220
# File 'lib/rom/support/notifications.rb', line 218

def events
  @events
end

#idObject (readonly)



214
215
216
# File 'lib/rom/support/notifications.rb', line 214

def id
  @id
end

#listenersObject (readonly)



222
223
224
# File 'lib/rom/support/notifications.rb', line 222

def listeners
  @listeners
end

Instance Method Details

#subscribe(event_id, query = EMPTY_HASH) {|block| ... } ⇒ Object Originally defined in module Publisher

Subscribe to events. If the query parameter is provided, filters events by payload.

Parameters:

  • event_id (String)

    The event key

  • query (Hash) (defaults to: EMPTY_HASH)

    An optional event filter

Yields:

  • (block)

    The callback

Returns:

  • (Object)

    self

#trigger(event_id, payload = EMPTY_HASH) ⇒ Object Originally defined in module Publisher

Trigger an event

Parameters:

  • event_id (String)

    The event key

  • payload (Hash) (defaults to: EMPTY_HASH)

    An optional payload