Skip to content
Strata is pre-1.0. APIs, protocol, and storage format may change.

strata_client/page_store

Typed multi-document store for strata_client apps.

PageStore(page) keys every open document by id over the app's own page sum type (typically a small enum of strata_document.Document variants, one per schema tag). This module never inspects page itself — the app supplies merge and to_json, its own exhaustive functions over every page constructor, so adding a new document kind stays compiler-checked in the app rather than here.

PageStore does not own join state or reconnect behavior; Connection remains the single owner of that. open/close only track which ids this store currently holds a page for; the host pairs open with an explicit strata_client.Join command.

Errors returned by apply_document_event.

pub type ApplyError {
NotOpen(document_id: String)
MergeFailed(document_id: String, error: strata_document.DocumentError)
NotDocumentEvent
}

The event targets a document id that is not open in this store.

MergeFailed(document_id: String, error: strata_document.DocumentError)

Section titled “MergeFailed(document_id: String, error: strata_document.DocumentError)”

The event carries document state that the app's merge rejected.

The event is not a document event (Snapshot, RemoteDelta, or Deleted) and carries no document state to apply.

Every open document, keyed by id, over the app's own page type.

pub type PageStore(a)

Apply a strata_client.Event, returning an explicit error when the event cannot be applied.

Snapshot and RemoteDelta require the document id to be open and the state to merge successfully; otherwise NotOpen or MergeFailed is returned. Deleted requires the id to be open; otherwise NotOpen is returned. Every other event variant returns NotDocumentEvent.

This is the component-facing API. Use apply_event when backward compatibility with no-op-on-error behavior is required.

pub fn apply_document_event(
PageStore(a),
strata_client.Event
) -> Result(PageStore(a), ApplyError)

Apply a strata_client.Event.

This is a backward-compatible wrapper around apply_document_event. Any error — NotOpen, MergeFailed, or NotDocumentEvent — leaves the store unchanged rather than propagating. Use apply_document_event when the caller must distinguish error cases.

The non-document variants are matched explicitly via apply_document_event, so a future Event variant that should affect document state will force a revisit here rather than silently compiling into a no-op.

pub fn apply_event(
PageStore(a),
strata_client.Event
) -> PageStore(a)

Close a document. Later events for id are ignored until it is reopened.

pub fn close(
PageStore(a),
String
) -> PageStore(a)

Look up an open document's page.

pub fn get(
PageStore(a),
String
) -> Result(a, Nil)

The ids of every currently open document.

pub fn ids(PageStore(a)) -> List(String)

Build an empty store. merge and to_json must be the app's own exhaustive functions over every page constructor.

pub fn new(
fn(a, json.Json) -> Result(a, strata_document.DocumentError),
fn(a) -> json.Json
) -> PageStore(a)

Open a document, storing initial under id.

This does not send Join; the host pairs it with strata_client.outbound(connection, strata_client.Join(id)).

pub fn open(
PageStore(a),
String,
a
) -> PageStore(a)

Apply a local edit.

Returns Some(SendDelta(id, json)) for an open id, where json is the edited page encoded with the app's to_json. Returns None and an unchanged store when id is not open.

pub fn update(
PageStore(a),
String,
fn(a) -> a
) -> #(PageStore(a), option.Option(strata_client.Command))