Class: RubyEventStore::Client::Within
- Inherits:
-
Object
- Object
- RubyEventStore::Client::Within
- Defined in:
- lib/ruby_event_store/client.rb
Overview
Builder object for collecting temporary handlers (subscribers) which are active only during the invocation of the provided block of code.
Instance Method Summary collapse
-
#call ⇒ Object
Invokes the block of code provided to #within and then unsubscribes temporary handlers.
-
#initialize(block, broker) ⇒ Within
constructor
A new instance of Within.
-
#subscribe(handler = nil, to:, &handler2) ⇒ Object
Subscribes temporary handlers that will be called for published events of provided type.
-
#subscribe_to_all_events(*handlers, &handler2) ⇒ self
Subscribes temporary handlers that will be called for all published events.
Constructor Details
#initialize(block, broker) ⇒ Within
Returns a new instance of Within.
142 143 144 145 146 147 |
# File 'lib/ruby_event_store/client.rb', line 142 def initialize(block, broker) @block = block @broker = broker @global_subscribers = [] @subscribers = Hash.new {[]} end |
Instance Method Details
#call ⇒ Object
Invokes the block of code provided to RubyEventStore::Client#within and then unsubscribes temporary handlers. Read more.
189 190 191 192 193 194 195 |
# File 'lib/ruby_event_store/client.rb', line 189 def call unsubs = add_thread_global_subscribers unsubs += add_thread_subscribers @block.call ensure unsubs.each(&:call) if unsubs end |
#subscribe(handler, to: ) ⇒ self #subscribe(to: , &handler) ⇒ self
Subscribes temporary handlers that will be called for published events of provided type. The subscription is active only during the invocation of the block of code provided to RubyEventStore::Client#within. Read more.
178 179 180 181 182 |
# File 'lib/ruby_event_store/client.rb', line 178 def subscribe(handler=nil, to:, &handler2) raise ArgumentError if handler && handler2 @subscribers[handler || handler2] += Array(to) self end |
#subscribe_to_all_events(*handlers, &handler2) ⇒ self
Subscribes temporary handlers that will be called for all published events. The subscription is active only during the invocation of the block of code provided to RubyEventStore::Client#within. Read more.
158 159 160 161 162 |
# File 'lib/ruby_event_store/client.rb', line 158 def subscribe_to_all_events(*handlers, &handler2) handlers << handler2 if handler2 @global_subscribers += handlers self end |