Class: Synapse::Domain::EventContainer Private

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

Instance Method Summary collapse

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

Parameters:

  • aggregate_id (Object)

    The identifier of the aggregate being tracked



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_idObject (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.

Returns:

  • (Object)

    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_numberInteger (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.

Returns:

  • (Integer)

    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.

Parameters:

  • listener (Proc)

Returns:

  • (undefined)


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

Parameters:

  • last_known (Integer)

Returns:

  • (undefined)

Raises:

  • (RuntimeError)

    If events have already been registered to 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_numberInteger

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

Returns:

  • (Integer)


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_committedundefined

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

Returns:

  • (undefined)


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.

Parameters:

  • payload (Object)

    Payload of the message; the actual event object

  • metadata (Hash)

    Metadata associated with the event

Returns:



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

#sizeInteger

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

Returns:

  • (Integer)


114
115
116
# File 'lib/synapse/domain/event_container.rb', line 114

def size
  @events.size
end

#to_streamDomainEventStream

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

Returns:



108
109
110
# File 'lib/synapse/domain/event_container.rb', line 108

def to_stream
  SimpleDomainEventStream.new @events
end