DoubleDown.Contract.Dispatch.FakeHandler behaviour (double_down v0.47.2)

Copy Markdown View Source

Behaviour for stateful fake handler modules.

Implement this behaviour to make a stateful fake usable by module name in DoubleDown.Double.fake/2..4:

# Instead of:
Double.fake(Repo, &Repo.OpenInMemory.dispatch/4, Repo.OpenInMemory.new())

# Write:
Double.fake(Repo, Repo.OpenInMemory)

Callbacks

  • new/2 — build initial state from seed data and options
  • dispatch/4 — stateful handler (contract, operation, args, state) -> {result, new_state}
  • dispatch/5 — stateful handler with cross-contract state access

Implement either dispatch/4 or dispatch/5 (or both). When both are implemented, dispatch/5 takes priority.

Example

defmodule MyApp.InMemoryStore do
  @behaviour DoubleDown.Contract.Dispatch.FakeHandler

  @impl true
  def new(seed, _opts), do: seed

  @impl true
  def dispatch(_contract, :get, [id], state), do: {Map.get(state, id), state}
  def dispatch(_contract, :put, [id, val], state), do: {:ok, Map.put(state, id, val)}
end

Summary

Callbacks

Stateful dispatch handler.

Stateful dispatch handler with cross-contract state access.

Build initial state from seed data and options.

Callbacks

dispatch(contract, operation, args, state)

(optional)
@callback dispatch(
  contract :: module(),
  operation :: atom(),
  args :: [term()],
  state :: term()
) :: {term(), term()}

Stateful dispatch handler.

Receives the contract module, operation name, argument list, and current state. Returns {result, new_state}.

dispatch(contract, operation, args, state, all_states)

(optional)
@callback dispatch(
  contract :: module(),
  operation :: atom(),
  args :: [term()],
  state :: term(),
  all_states :: map()
) :: {term(), term()}

Stateful dispatch handler with cross-contract state access.

Same as dispatch/4 but receives a read-only snapshot of all contract states as the 5th argument.

new(seed, opts)

@callback new(seed :: term(), opts :: keyword()) :: term()

Build initial state from seed data and options.

Called by Double.fake/2..4 to construct the initial state for the stateful handler.

  • seed — seed data (e.g. %{User => %{1 => %User{}}} for Repo.OpenInMemory)
  • opts — additional options (e.g. fallback_fn: fn ... end)