Class: RubyEventStore::Client::Within

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(block, broker, resolver) ⇒ Within



213
214
215
216
217
218
219
# File 'lib/ruby_event_store/client.rb', line 213

def initialize(block, broker, resolver)
  @block = block
  @broker = broker
  @global_subscribers = []
  @subscribers = Hash.new { [] }
  @resolver = resolver
end

Instance Method Details

#callObject

Invokes the block of code provided to RubyEventStore::Client#within and then unsubscribes temporary handlers. Read more.



261
262
263
264
265
266
267
# File 'lib/ruby_event_store/client.rb', line 261

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.

Raises:

  • (ArgumentError)


250
251
252
253
254
# File 'lib/ruby_event_store/client.rb', line 250

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.



230
231
232
233
234
# File 'lib/ruby_event_store/client.rb', line 230

def subscribe_to_all_events(*handlers, &handler2)
  handlers << handler2 if handler2
  @global_subscribers += handlers
  self
end