Module: Dry::Events::Publisher::ClassMethods

Defined in:
lib/dry/events/publisher.rb

Overview

Class interface for publisher classes

Instance Method Summary collapse

Instance Method Details

#eventsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Global registry with events



155
156
157
# File 'lib/dry/events/publisher.rb', line 155

def events
  @__events__ ||= Concurrent::Map.new
end

#listenersObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Global registry with listeners



162
163
164
# File 'lib/dry/events/publisher.rb', line 162

def listeners
  @__listeners__ ||= LISTENERS_HASH.dup
end

#new_busBus

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets up event bus for publisher instances

Returns:



148
149
150
# File 'lib/dry/events/publisher.rb', line 148

def new_bus
  Bus.new(events: events.dup, listeners: listeners.dup)
end

#register_event(event_id, payload = EMPTY_HASH) ⇒ Object

Register an event

Parameters:

  • event_id (String)

    The event identifier

  • payload (Hash) (defaults to: EMPTY_HASH)

    Optional default payload



125
126
127
128
# File 'lib/dry/events/publisher.rb', line 125

def register_event(event_id, payload = EMPTY_HASH)
  events[event_id] = Event.new(event_id, payload)
  self
end

#subscribe(event_id, filter_hash = EMPTY_HASH, &block) ⇒ Class

Subscribe to an event

Parameters:

  • event_id (Symbol, String)

    The event identifier

  • filter_hash (Hash) (defaults to: EMPTY_HASH)

    An optional filter for conditional listeners

Returns:

  • (Class)

    publisher class



138
139
140
141
# File 'lib/dry/events/publisher.rb', line 138

def subscribe(event_id, filter_hash = EMPTY_HASH, &block)
  listeners[event_id] << [block, Filter.new(filter_hash)]
  self
end