Module: Hecks::Runtime::CommandRules::Emission

Included in:
Hecks::Runtime::CommandRules
Defined in:
lib/hecks/runtime/command_rules/emission.rb

Overview

What a command's emits becomes: an Event in the registry's log, and — where the store can hold one — a recorded event beside the data it describes.

Instance Method Summary collapse

Instance Method Details

#emit(command, domain, aggregate, instance, args, repository, correlation = nil) ⇒ Object

correlation arrives HERE rather than being merged onto the event afterwards. It is part of the transaction — known from dispatch's own argument before anything is emitted — and an event that is still being written to is not yet a record of what happened. Setting it at construction is what lets the event be frozen the moment it exists.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hecks/runtime/command_rules/emission.rb', line 16

def emit(command, domain, aggregate, instance, args, repository, correlation = nil)
  command.emits.map do |event_name|
    event = Event.new(
      name:        event_name,
      aggregate:   "#{domain}::#{aggregate.hecks_name}",
      id:          instance.id,
      payload:     args,
      occurred_at: Time.now.utc.iso8601,
      correlation: correlation
    )
    @registry.event_log << event.emit!
    repository.record_event(event) if repository.respond_to?(:record_event)
    event
  end
end