Module: ROM::Notifications::Publisher

Included in:
ROM::Notifications, EventBus
Defined in:
lib/rom/support/notifications.rb

Overview

Extension used for classes that can trigger events

Instance Method Summary collapse

Instance Method Details

#subscribe(event_id, query = EMPTY_HASH) {|block| ... } ⇒ Object

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


56
57
58
59
# File 'lib/rom/support/notifications.rb', line 56

def subscribe(event_id, query = EMPTY_HASH, &block)
  listeners[event_id] << [block, query]
  self
end

#trigger(event_id, payload = EMPTY_HASH) ⇒ Object

Trigger an event

Parameters:

  • event_id (String)

    The event key

  • payload (Hash) (defaults to: EMPTY_HASH)

    An optional payload


67
68
69
70
71
72
73
# File 'lib/rom/support/notifications.rb', line 67

def trigger(event_id, payload = EMPTY_HASH)
  event = events[event_id]

  listeners[event.id].each do |(listener, query)|
    event.payload(payload).trigger(listener, query)
  end
end