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, resolver) ⇒ 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, resolver) ⇒ Within
Returns a new instance of Within.
177 178 179 180 181 182 183 |
# File 'lib/ruby_event_store/client.rb', line 177 def initialize(block, broker, resolver) @block = block @broker = broker @global_subscribers = [] @subscribers = Hash.new { [] } @resolver = resolver end |
Instance Method Details
#call ⇒ Object
Invokes the block of code provided to RubyEventStore::Client#within and then unsubscribes temporary handlers. Read more.
225 226 227 228 229 230 231 |
# File 'lib/ruby_event_store/client.rb', line 225 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.
214 215 216 217 218 |
# File 'lib/ruby_event_store/client.rb', line 214 def subscribe(handler = nil, to:, &handler2) raise ArgumentError if handler && handler2 @subscribers[handler || handler2] += Array(to).map { |event_klass| resolver.call(event_klass) } 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.
194 195 196 197 198 |
# File 'lib/ruby_event_store/client.rb', line 194 def subscribe_to_all_events(*handlers, &handler2) handlers << handler2 if handler2 @global_subscribers += handlers self end |