Module: Dry::Events::Publisher::InstanceMethods
- Defined in:
- lib/dry/events/publisher.rb
Overview
Instance interface for publishers
Instance Method Summary collapse
-
#__bus__ ⇒ Bus
private
Internal event bus.
-
#process(event_id, payload = EMPTY_HASH, &block) ⇒ Object
Utility method which yields event with each of its listeners.
-
#publish(event_id, payload = EMPTY_HASH) ⇒ Object
(also: #trigger)
Publish an event.
-
#register_event(event_id, payload = EMPTY_HASH) ⇒ self
Register a new event type at instance level.
-
#subscribe(object_or_event_id, filter_hash = EMPTY_HASH, &block) ⇒ Object
Subscribe to events.
-
#subscribed?(listener) ⇒ Boolean
Return true if a given listener has been subscribed to any event.
-
#unsubscribe(listener) ⇒ self
Unsubscribe a listener.
Instance Method Details
#__bus__ ⇒ 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.
Internal event bus
257 258 259 |
# File 'lib/dry/events/publisher.rb', line 257 def __bus__ @__bus__ ||= self.class.new_bus end |
#process(event_id, payload = EMPTY_HASH, &block) ⇒ Object
Utility method which yields event with each of its listeners
Listeners are already filtered out when filter was provided during subscription
param [Hash] payload An optional payload
248 249 250 |
# File 'lib/dry/events/publisher.rb', line 248 def process(event_id, payload = EMPTY_HASH, &block) __bus__.process(event_id, payload, &block) end |
#publish(event_id, payload = EMPTY_HASH) ⇒ Object Also known as: trigger
Publish an event
189 190 191 192 |
# File 'lib/dry/events/publisher.rb', line 189 def publish(event_id, payload = EMPTY_HASH) __bus__.publish(event_id, payload) self end |
#register_event(event_id, payload = EMPTY_HASH) ⇒ self
Register a new event type at instance level
179 180 181 182 |
# File 'lib/dry/events/publisher.rb', line 179 def register_event(event_id, payload = EMPTY_HASH) __bus__.events[event_id] = Event.new(event_id, payload) self end |
#subscribe(object_or_event_id, filter_hash = EMPTY_HASH, &block) ⇒ Object
Subscribe to events.
If the filter parameter is provided, filters events by payload.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/dry/events/publisher.rb', line 205 def subscribe(object_or_event_id, filter_hash = EMPTY_HASH, &block) if __bus__.can_handle?(object_or_event_id) filter = Filter.new(filter_hash) if block __bus__.subscribe(object_or_event_id, filter, &block) else __bus__.attach(object_or_event_id, filter) end self else raise InvalidSubscriberError, object_or_event_id end end |
#subscribed?(listener) ⇒ Boolean
Return true if a given listener has been subscribed to any event
235 236 237 |
# File 'lib/dry/events/publisher.rb', line 235 def subscribed?(listener) __bus__.subscribed?(listener) end |
#unsubscribe(listener) ⇒ self
Unsubscribe a listener
228 229 230 |
# File 'lib/dry/events/publisher.rb', line 228 def unsubscribe(listener) __bus__.detach(listener) end |