class RubyEventStore::Client::Within
Builder object for collecting temporary handlers (subscribers) which are active only during the invocation of the provided block of code.
Attributes
Public Class Methods
# 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
Public Instance Methods
Invokes the block of code provided to {Client#within} and then unsubscribes temporary handlers. {railseventstore.org/docs/subscribe/#temporary-subscriptions Read more.}
@return [Object] value returned by the invoked block of code
# 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
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 {Client#within}. {railseventstore.org/docs/subscribe/#temporary-subscriptions Read more.}
@overload subscribe(handler, to:)
@param handler [Object, Class] handler passed as objects or classes @param to [Array<Class>] types of events to subscribe @return [self]
@overload subscribe(to:, &handler)
@param to [Array<Class>] types of events to subscribe @param handler [Proc] handler passed as proc @return [self]
# 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
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 {Client#within}. {railseventstore.org/docs/subscribe/#temporary-subscriptions Read more.}
@param handlers [Object, Class] handlers passed as objects or classes @param handler2 [Proc] handler passed as proc @return [self]
# 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
Private Instance Methods
# File lib/ruby_event_store/client.rb, line 240 def add_thread_global_subscribers @global_subscribers.map { |subscriber| @broker.add_thread_global_subscription(subscriber) } end
# File lib/ruby_event_store/client.rb, line 236 def add_thread_subscribers @subscribers.map { |subscriber, types| @broker.add_thread_subscription(subscriber, types) } end