retrace_frontend/capture

Optional capture coordination. Observe only application-approved transitions. Map the returned effects/messages into your application. They are not events to record. Stop the owned client when the application unmounts.

Types

pub type Info {
  Info(
    session: option.Option(wire.Session),
    captured: Int,
    saved: Int,
    delivery: client.Status,
    failure: option.Option(String),
  )
}

Constructors

pub opaque type Message
pub opaque type State

Values

pub fn flush(state: State) -> effect.Effect(Message)

Save the open batch now instead of waiting for its window. A browser page that becomes hidden or navigates away already does this before the hand-over. Does nothing when no observation is held.

pub fn info(state: State) -> Info
pub fn observe(
  state: State,
  before: model,
  message: message,
  encode_model: fn(model) -> String,
  encode_message: fn(message) -> wire.Event,
) -> #(State, effect.Effect(Message))

Observe after accepting a transition, passing its state BEFORE reduction. This helper never runs application effects. Record their resulting messages through this same hook. A failure stops capture; your update still proceeds. Before the session is open nothing is recorded, so the recording starts at a checkpoint instead of leaving a gap.

pub fn observe_projected(
  state: State,
  projection: Result(#(model, message), error),
  encode_model: fn(model) -> String,
  encode_message: fn(message) -> wire.Event,
) -> #(State, effect.Effect(Message))

Observe an application’s fallible safe projection of an accepted transition. The pair contains the safe state BEFORE the transition and its safe event. Only successful projections reach the codecs. Failure stops capture without recording the application’s error, which may itself contain private values. This does not classify data or prove the projection/codecs safe or pure.

pub fn retry(state: State) -> effect.Effect(Message)
pub fn start(
  metadata: wire.Metadata,
  config: client.Config,
  storage: client.Storage,
  transport: client.Transport,
  flush_after_ms: Int,
) -> Result(#(State, effect.Effect(Message)), wire.Error)

Start capture for a new session. The connection effect starts the client and asks the collector to open a session for metadata; the collector chooses the session id and issues the write token every batch must carry. Recording begins with the first transition observed after the session is open; the session’s first checkpoint is the state before that transition.

Observations are uploaded in batches. A batch opens at an observation and closes flush_after_ms later, at the wire format’s 128 entries, before an observation that would take it past half of the wire byte limit, on flush and on stop. It carries one checkpoint, the state before its first observation, so the model is encoded and uploaded once per batch, and replay ranges start at batch boundaries. With a window of zero every observation is saved at once as its own batch. Held observations count as captured and not as saved. In a browser the open batch also closes when the page becomes hidden or navigates away, just before the client hands its unacknowledged batches over, so the last observations leave with the page.

pub fn start_with_clock(
  metadata: wire.Metadata,
  config: client.Config,
  storage: client.Storage,
  transport: client.Transport,
  flush_after_ms: Int,
  clock: fn() -> Int,
) -> Result(#(State, effect.Effect(Message)), wire.Error)

The clock returns elapsed milliseconds since this session began.

pub fn stop(state: State) -> effect.Effect(Message)

Run before discarding the application state. The open batch is saved first. Persisted batches remain queued.

pub fn update(state: State, message: Message) -> State
✨ Search Document