Class: Synapse::Domain::EventContainer Private
- Inherits:
-
Object
- Object
- Synapse::Domain::EventContainer
- Defined in:
- lib/synapse/domain/event_container.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Container that tracks uncommitted events published by an aggregate root and its child entities
Instance Attribute Summary collapse
-
#aggregate_id ⇒ Object
readonly
private
The identifier of the aggregate being tracked.
-
#last_committed_sequence_number ⇒ Integer
readonly
private
The sequence number of the last committed event.
Instance Method Summary collapse
-
#add_registration_listener(listener) ⇒ undefined
private
Adds an event registration listener to this container.
-
#initialize(aggregate_id) ⇒ undefined
constructor
private
Initializes this event container.
-
#initialize_sequence_number(last_known) ⇒ undefined
private
Sets the last committed sequence number for the container.
-
#last_sequence_number ⇒ Integer
private
Returns the sequence number of the last event known by this container.
-
#mark_committed ⇒ undefined
private
Updates the last committed sequence number and clears any uncommitted events and any event registration listeners.
-
#register_event(payload, metadata) ⇒ DomainEventMessage
private
Registers an event published by the aggregate to this container.
-
#size ⇒ Integer
private
Returns the number of uncommitted events in this container.
-
#to_stream ⇒ DomainEventStream
private
Returns an event stream containing the uncommitted events in this container.
Constructor Details
#initialize(aggregate_id) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes this event container
18 19 20 21 22 |
# File 'lib/synapse/domain/event_container.rb', line 18 def initialize(aggregate_id) @aggregate_id = aggregate_id @events = Array.new @listeners = Array.new end |
Instance Attribute Details
#aggregate_id ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The identifier of the aggregate being tracked.
7 8 9 |
# File 'lib/synapse/domain/event_container.rb', line 7 def aggregate_id @aggregate_id end |
#last_committed_sequence_number ⇒ Integer (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The sequence number of the last committed event.
10 11 12 |
# File 'lib/synapse/domain/event_container.rb', line 10 def last_committed_sequence_number @last_committed_sequence_number end |
Instance Method Details
#add_registration_listener(listener) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds an event registration listener to this container
If the listener is added after events have already registered with the container, it will be called with a backlog of events to process.
61 62 63 64 65 66 67 |
# File 'lib/synapse/domain/event_container.rb', line 61 def add_registration_listener(listener) @listeners.push listener @events.map! do |event| listener.call event end end |
#initialize_sequence_number(last_known) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets the last committed sequence number for the container
74 75 76 77 78 79 80 |
# File 'lib/synapse/domain/event_container.rb', line 74 def initialize_sequence_number(last_known) unless @events.empty? raise 'Sequence number must be set before events are registered' end @last_committed_sequence_number = last_known end |
#last_sequence_number ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the sequence number of the last event known by this container
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/synapse/domain/event_container.rb', line 84 def last_sequence_number if @events.empty? return @last_committed_sequence_number end unless @last_sequence_number @last_sequence_number = @events.last.sequence_number end @last_sequence_number end |
#mark_committed ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Updates the last committed sequence number and clears any uncommitted events and any event registration listeners
100 101 102 103 104 |
# File 'lib/synapse/domain/event_container.rb', line 100 def mark_committed @last_committed_sequence_number = @last_sequence_number @events.clear @listeners.clear end |
#register_event(payload, metadata) ⇒ DomainEventMessage
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Registers an event published by the aggregate to this container
During this process, a domain event message is created. Event registration listeners can choose to modify or replace the message before it is committed.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/synapse/domain/event_container.rb', line 36 def register_event(payload, ) event = DomainEventMessage.build do |builder| builder.aggregate_id = @aggregate_id builder.sequence_number = next_sequence_number builder. = builder.payload = payload end @listeners.each do |listener| event = listener.call event end @last_sequence_number = event.sequence_number @events.push event event end |
#size ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the number of uncommitted events in this container
114 115 116 |
# File 'lib/synapse/domain/event_container.rb', line 114 def size @events.size end |
#to_stream ⇒ DomainEventStream
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an event stream containing the uncommitted events in this container
108 109 110 |
# File 'lib/synapse/domain/event_container.rb', line 108 def to_stream SimpleDomainEventStream.new @events end |