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



170
171
172
# File 'lib/dry/events/publisher.rb', line 170

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



177
178
179
# File 'lib/dry/events/publisher.rb', line 177

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:



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

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



140
141
142
143
# File 'lib/dry/events/publisher.rb', line 140

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



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

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