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
277 278 279 |
# File 'lib/dry/events/publisher.rb', line 277 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
268 269 270 |
# File 'lib/dry/events/publisher.rb', line 268 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
205 206 207 208 209 210 211 212 |
# File 'lib/dry/events/publisher.rb', line 205 def publish(event_id, payload = EMPTY_HASH) if __bus__.can_handle?(event_id) __bus__.publish(event_id, payload) self else raise UnregisteredEventError, event_id end end |
#register_event(event_id, payload = EMPTY_HASH) ⇒ self
Register a new event type at instance level
194 195 196 197 |
# File 'lib/dry/events/publisher.rb', line 194 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.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/dry/events/publisher.rb', line 225 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
255 256 257 |
# File 'lib/dry/events/publisher.rb', line 255 def subscribed?(listener) __bus__.subscribed?(listener) end |
#unsubscribe(listener) ⇒ self
Unsubscribe a listener
248 249 250 |
# File 'lib/dry/events/publisher.rb', line 248 def unsubscribe(listener) __bus__.detach(listener) end |