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) ⇒ 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

#callObject

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

Returns:

  • value returned by the invoked block of code



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.

Overloads:

  • #subscribe(handler, to: ) ⇒ self

    Parameters:

    • handler passed as objects or classes

    • (defaults to: )

      types of events to subscribe

    Returns:

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

    Parameters:

    • (defaults to: )

      types of events to subscribe

    • handler passed as proc

    Returns:

Raises:



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.

Parameters:

  • handlers passed as objects or classes

  • handler passed as proc

Returns:



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