# `Chronicle.Events.EventContext`
[🔗](https://github.com/Cratis/Chronicle.Elixir/blob/main/lib/chronicle/events/event_context.ex#L4)

The context of an event handed to reactors and reducers.

It carries the same information as the .NET client's `EventContext`, so a handler can see
who caused an event, how it is correlated and where it sits in the event sequence.

A struct is a map: handlers can keep pattern matching on keys
(`def handle(event, %{event_source_id: id})`) or use `context.caused_by`.

  * `:event_type` — `%{id: String.t(), generation: non_neg_integer()}`
  * `:event_source_type`, `:event_source_id`, `:event_source` (the registered event source
    name, or `nil`), `:event_stream_type`, `:event_stream_id`
  * `:sequence_number`, `:occurred`
  * `:event_store`, `:namespace`, `:event_sequence_id` (when known)
  * `:correlation_id` — string
  * `:caused_by` — `Chronicle.Identity` (including `on_behalf_of`), or `nil`
  * `:causation` — list of `Chronicle.Auditing.CausationEntry`
  * `:tags` — list of tag strings; `:named_tags` — map of name to value
  * `:hash`, `:subject`
  * `:appended_generation` — the generation that was actually appended, regardless of
    which generation the handler is written against. `0` means unknown: an older kernel
    that doesn't send this field, or an event appended outside a registered event source.
  * `:observation_state` — the observation state; `:replaying?` is `true` while the observer is replaying
  * `:delivery_id` — reactors only; see `delivery_id/1`

# `t`

```elixir
@type t() :: %Chronicle.Events.EventContext{
  appended_generation: term(),
  causation: term(),
  caused_by: term(),
  correlation_id: term(),
  delivery_id: term(),
  event_sequence_id: term(),
  event_source: term(),
  event_source_id: term(),
  event_source_type: term(),
  event_store: term(),
  event_stream_id: term(),
  event_stream_type: term(),
  event_type: term(),
  hash: term(),
  named_tags: term(),
  namespace: term(),
  observation_state: term(),
  occurred: term(),
  replaying?: term(),
  sequence_number: term(),
  subject: term(),
  tags: term()
}
```

# `delivery_id`

```elixir
@spec delivery_id(t(), String.t()) :: String.t()
```

The stable identity of one delivery of an event to a reactor.

Derived exactly like the .NET `ReactorDelivery.Id`: the reactor id, event store, namespace,
event sequence id, event source id and sequence number joined with `#`. Use it as the key of
whatever records that a side effect completed.

# `from_wire`

```elixir
@spec from_wire(map(), keyword()) :: t()
```

Builds an event context from the wire `EventContext` of a reactor or reducer observation.

Options: `:event_sequence_id` and `:reactor_id` (when given, a `:delivery_id` is derived).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
