Class: Dry::Events::Bus Private
- Inherits:
-
Object
- Object
- Dry::Events::Bus
- Defined in:
- lib/dry/events/bus.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Event bus
An event bus stores listeners (callbacks) and events
Instance Attribute Summary collapse
- #events ⇒ Object readonly private
- #listeners ⇒ Object readonly private
Instance Method Summary collapse
- #attach(listener, filter) ⇒ Object private
- #can_handle?(object_or_event_id) ⇒ Boolean private
- #detach(listener) ⇒ Object private
-
#initialize(events: EMPTY_HASH, listeners: LISTENERS_HASH.dup) ⇒ Bus
constructor
private
Initialize a new event bus.
- #process(event_id, payload) ⇒ Object private
- #publish(event_id, payload) ⇒ Object private
- #subscribe(event_id, filter, &block) ⇒ Object private
- #subscribed?(listener) ⇒ Boolean private
Constructor Details
#initialize(events: EMPTY_HASH, listeners: LISTENERS_HASH.dup) ⇒ Bus
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.
Initialize a new event bus
25 26 27 28 |
# File 'lib/dry/events/bus.rb', line 25 def initialize(events: EMPTY_HASH, listeners: LISTENERS_HASH.dup) @listeners = listeners @events = events end |
Instance Attribute Details
#events ⇒ Object (readonly)
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.
13 14 15 |
# File 'lib/dry/events/bus.rb', line 13 def events @events end |
#listeners ⇒ Object (readonly)
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.
17 18 19 |
# File 'lib/dry/events/bus.rb', line 17 def listeners @listeners end |
Instance Method Details
#attach(listener, filter) ⇒ Object
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.
49 50 51 52 53 54 55 56 57 |
# File 'lib/dry/events/bus.rb', line 49 def attach(listener, filter) events.each do |id, event| meth = event.listener_method if listener.respond_to?(meth) listeners[id] << [listener.method(meth), filter] end end end |
#can_handle?(object_or_event_id) ⇒ Boolean
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.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dry/events/bus.rb', line 81 def can_handle?(object_or_event_id) case object_or_event_id when String, Symbol events.key?(object_or_event_id) else events .values .map(&:listener_method) .any?(&object_or_event_id.method(:respond_to?)) end end |
#detach(listener) ⇒ Object
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.
60 61 62 63 64 65 66 67 |
# File 'lib/dry/events/bus.rb', line 60 def detach(listener) listeners.each do |id, memo| memo.each do |tuple| current_listener, _ = tuple listeners[id].delete(tuple) if current_listener.receiver.equal?(listener) end end end |
#process(event_id, payload) ⇒ Object
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.
31 32 33 34 35 36 37 38 39 |
# File 'lib/dry/events/bus.rb', line 31 def process(event_id, payload) listeners[event_id].each do |listener, filter| event = events[event_id].payload(payload) if filter.(payload) yield(event, listener) end end end |
#publish(event_id, payload) ⇒ Object
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.
42 43 44 45 46 |
# File 'lib/dry/events/bus.rb', line 42 def publish(event_id, payload) process(event_id, payload) do |event, listener| listener.(event) end end |
#subscribe(event_id, filter, &block) ⇒ Object
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.
70 71 72 73 |
# File 'lib/dry/events/bus.rb', line 70 def subscribe(event_id, filter, &block) listeners[event_id] << [block, filter] self end |
#subscribed?(listener) ⇒ Boolean
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.
76 77 78 |
# File 'lib/dry/events/bus.rb', line 76 def subscribed?(listener) listeners.values.any? { |value| value.any? { |block, _| block.equal?(listener) } } end |