Class: Brainguy::IdempotentEmitter

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

Emitter::DEFAULT_NOTIFIER

Instance Method Summary collapse

Methods inherited from Emitter

#attach, #detach, new_from_existing, #on, #subscriptions, #with_subscription_scope

Constructor Details

#initializeIdempotentEmitter

Returns a new instance of IdempotentEmitter.

Parameters:

  • event_source (Object)

    the event-originating object

  • options (Hash)

    a customizable set of options



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

#emit(event_name, *extra_args) ⇒ Object

Emit an event and record it in the log.

Returns:

  • the notifier's result value



21
22
23
24
# File 'lib/brainguy/idempotent_emitter.rb', line 21

def emit(event_name, *extra_args)
  @event_log.push(Event.new(event_name, @event_source, extra_args))
  super
end