Class: Brainguy::IdempotentEmitter
- Defined in:
- lib/brainguy/idempotent_emitter.rb
Overview
A type of Emitter that records and "plays back" events to new listeners. That way a listener will never miss an event, even if it subscribes late.
This class is probably best used in short-lived scopes, since the log of events will continually grow.
Constant Summary
Constants inherited from Emitter
Instance Method Summary collapse
-
#<<(subscription) ⇒ Object
(also: #add)
Add a new subscription, and immediately play back any missed events to it.
-
#emit(event_name, *extra_args) ⇒ Object
Emit an event and record it in the log.
-
#initialize ⇒ IdempotentEmitter
constructor
A new instance of IdempotentEmitter.
Methods inherited from Emitter
#attach, #detach, new_from_existing, #on, #subscriptions, #with_subscription_scope
Constructor Details
#initialize ⇒ IdempotentEmitter
Returns a new instance of IdempotentEmitter.
12 13 14 15 |
# File 'lib/brainguy/idempotent_emitter.rb', line 12 def initialize(*) super @event_log = [] end |
Instance Method Details
#<<(subscription) ⇒ Object Also known as: add
Add a new subscription, and immediately play back any missed events to it.
31 32 33 34 35 36 |
# File 'lib/brainguy/idempotent_emitter.rb', line 31 def <<(subscription) super @event_log.each do |event| subscription.handle(event) end end |