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

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

#callObject

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

Returns:

  • (Object)

    value returned by the invoked block of code



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.

Overloads:

  • #subscribe(handler, to: ) ⇒ self

    Parameters:

    • handler (Object, Class)

      handler passed as objects or classes

    • to (Array<Class>) (defaults to: )

      types of events to subscribe

    Returns:

    • (self)
  • #subscribe(to: , &handler) ⇒ self

    Parameters:

    • to (Array<Class>) (defaults to: )

      types of events to subscribe

    • handler (Proc)

      handler passed as proc

    Returns:

    • (self)

Raises:

  • (ArgumentError)


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.

Parameters:

  • handlers (Object, Class)

    handlers passed as objects or classes

  • handler2 (Proc)

    handler passed as proc

Returns:

  • (self)


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