Chronik v0.1.10 Chronik.Projection behaviour View Source

Chronik.Projection is a read-only model connected to Chronik.PubSub

Client code has to implement the Chronik.Projection.init/1 function and the state transition Chronik.Projection.handle_event/2.

Example

defmodule DomainEvents do
  defmodule CounterCreated do
    defstruct [:id]
  end

  defmodule CounterIncremented do
    defstruct [:id, :increment]
  end
end

defmodule CounterState do
  @behaviour Chronik.Projection

  alias DomainEvents.{CounterCreated, CounterIncremented}

  alias Chronik.Projection

  def start_link(opts), do: Projection.start_link(__MODULE__, opts)

  def init(_opts), do: {nil, []}

  def handle_event(%CounterCreated{}, nil), do: 0

  def handle_event(%CounterIncremented{increment: increment}, value) do
    value + increment
  end
end

Link to this section Summary

Types

The state represents the state of an projection

Functions

Returns a specification to start this module under a supervisor

Invoked when the server is started. start_link/3 or start/3 will block until it returns

Start a new projection

Return the current state for projection_id

Callbacks

The handle_event function is executed each time an event record is received on the Chronik.PubSub and is responsible of the projection state transition

The init function defines the intial state of an projection and some options

Link to this section Types

The state represents the state of an projection.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Invoked when the server is started. start_link/3 or start/3 will block until it returns.

args is the argument term (second argument) passed to start_link/3.

Returning {:ok, state} will cause start_link/3 to return {:ok, pid} and the process to enter its loop.

Returning {:ok, state, timeout} is similar to {:ok, state} except handle_info(:timeout, state) will be called after timeout milliseconds if no messages are received within the timeout.

Returning {:ok, state, :hibernate} is similar to {:ok, state} except the process is hibernated before entering the loop. See c:handle_call/3 for more information on hibernation.

Returning :ignore will cause start_link/3 to return :ignore and the process will exit normally without entering the loop or calling c:terminate/2. If used when part of a supervision tree the parent supervisor will not fail to start nor immediately try to restart the GenServer. The remainder of the supervision tree will be (re)started and so the GenServer should not be required by other processes. It can be started later with Supervisor.restart_child/2 as the child specification is saved in the parent supervisor. The main use cases for this are:

  • The GenServer is disabled by configuration but might be enabled later.
  • An error occurred and it will be handled by a different mechanism than the Supervisor. Likely this approach involves calling Supervisor.restart_child/2 after a delay to attempt a restart.

Returning {:stop, reason} will cause start_link/3 to return {:error, reason} and the process to exit with reason reason without entering the loop or calling c:terminate/2.

Callback implementation for GenServer.init/1.

Link to this function start_link(projection, opts) View Source

Start a new projection

Return the current state for projection_id

Link to this section Callbacks

Link to this callback handle_event(record, state) View Source
handle_event(record :: Chronik.EventRecord, state :: state()) :: state()

The handle_event function is executed each time an event record is received on the Chronik.PubSub and is responsible of the projection state transition.

The return value is a new state for the received record

The init function defines the intial state of an projection and some options.

The accepted options:

  • :version start replaying events from version and up. :all indicates that the replay should be from the begining of times while :current means that no re-play should be made and start feeding domain events from current version and on.

  • :consistency indicates how the projection should subscribe to the Chronik.PubSub. Possible values are :eventual (default) and :strict.