Skip to content

Stores

A store wraps a dataclass instance, a dict, or a list, and create_store types it as that same data. A dataclass store (StoreObject) reads its fields as tracked attributes; dict and list stores (Store and StoreList) implement Python's read-only Mapping and Sequence protocols, and their synchronous scoped drafts implement the mutable counterparts. See the store guide for versions, identity, snapshots, shallow stores, projections, and optimistic rebasing.

store

Reactive stores: nested state you read like plain data and edit through drafts.

A store wraps a dict, a list, or a dataclass instance. Reading it in a tracking scope subscribes to exactly the fields read. A setter opens a scoped mutable draft; its edits are staged together and become visible at the next flush, or are all discarded if the callback raises.

Containers are wrapped lazily: creating a store is constant time, and a nested dict, list, or dataclass gets its reactive bookkeeping the first time something reads it. Every container is copied on its first edit in a setter, so a version that a pending transition still shows stays intact.

Classes:

Name Description
DraftExpiredError

A draft was used after its setter or deriving function finished.

Store

The read-only reactive view of a dict store.

StoreList

The read-only reactive view of a list store. Entity proxies survive reorders.

StoreObject

The read-only reactive view of a dataclass store.

StoreSetter

Apply an atomic draft edit. Exceptions discard all changes from the call.

Functions:

Name Description
create_store

Create a reactive store and its atomic draft setter.

create_projection

Create a read-only derived store with memo readiness and refresh semantics.

create_optimistic_store

Layer optimistic draft edits over a reactive authoritative source.

reconcile

Prepare replacement data, preserving matched list entity identities.

snapshot

Return a detached, untracked copy of the currently visible state.

deep

Return a detached snapshot, tracking changes only within this subtree.

DraftExpiredError

Bases: RuntimeError

A draft was used after its setter or deriving function finished.

ListChange dataclass

ListChange(kind: str, index: int = 0, removed: tuple[Any, ...] = (), added: tuple[Any, ...] = ())

Internal sequence edit. Indices refer to the state before this edit.

Store

Store(node: _Node, session: _Session | None = None)

Bases: _Proxy, Mapping[Any, Any]

The read-only reactive view of a dict store.

Read it like a dict: store["key"], store.get("key"), in, len, and iteration are all tracked. Missing keys raise KeyError.

Methods:

Name Description
get

Return the value for key, or default when absent (tracked either way).

get

get(key: Any, default: Any = None) -> Any

Return the value for key, or default when absent (tracked either way).

A tracked read of an absent key subscribes to it, so a computation reading store.get(row_id) re-runs when that key appears.

StoreList

StoreList(node: _Node, session: _Session | None = None)

Bases: _Proxy, Sequence[T]

The read-only reactive view of a list store. Entity proxies survive reorders.

StoreObject

StoreObject(node: _Node, session: _Session | None = None)

Bases: _Proxy

The read-only reactive view of a dataclass store.

Fields read as tracked attributes. Methods and properties of the dataclass work too, reading through the view, and isinstance sees the dataclass, so a store typed as your class behaves like one.

StoreSetter

StoreSetter(node: _Node)

Apply an atomic draft edit. Exceptions discard all changes from the call.

Methods:

Name Description
edit

Edit the store in a with block: the draft's changes stage together when it ends.

edit

edit() -> Iterator[T]

Edit the store in a with block: the draft's changes stage together when it ends.

The block is the setter's callback written inline, so it's atomic in the same way: an exception discards every change it made.

with set_state.edit() as draft:
    draft.todos[0].title = title
    draft.todos[0].done = False

create_store

create_store(initial: Callable[[T], object], seed: T, *, shallow: bool = False) -> tuple[T, StoreSetter[T]]
create_store(initial: Callable[[], object], seed: None = None, *, shallow: bool = False) -> tuple[Any, StoreSetter[Any]]
create_store(initial: T, seed: None = None, *, shallow: bool = False) -> tuple[T, StoreSetter[T]]
create_store(initial: Any, seed: Any = None, *, shallow: bool = False) -> tuple[Any, StoreSetter[Any]]

Create a reactive store and its atomic draft setter.

initial is a dict, a list, or a dataclass instance; the store takes ownership of it, so don't keep mutating it yourself. The returned view is typed as initial's own type: create_store(State(...)) gives a State whose fields are tracked attributes, and the setter takes Callable[[State], object].

With a function, the store is derived: the function either mutates the draft it's given or returns new data (reconciled by "id"), synchronously or asynchronously. Its reads are tracked and its writes are staged together. seed is the starting data.

With shallow=True, only the top level is reactive: nested values are stored and returned as they are, and replacing one is the only way to change it.

create_projection

create_projection(fn: Callable[[T], object], initial: T) -> T
create_projection(fn: Callable[[], object], initial: None = None) -> Any
create_projection(fn: Callable[[], object], initial: T) -> T
create_projection(fn: Callable[..., Any], initial: Any = None) -> Any

Create a read-only derived store with memo readiness and refresh semantics.

create_optimistic_store

create_optimistic_store(source: Callable[[], T], initial: Any = None) -> tuple[T, Callable[[Callable[[T], object] | T], None]]
create_optimistic_store(source: T, initial: None = None) -> tuple[T, Callable[[Callable[[T], object] | T], None]]
create_optimistic_store(source: Any, initial: Any = None) -> tuple[Any, Callable[[Any], None]]

Layer optimistic draft edits over a reactive authoritative source.

Active edits replay in submission order when the source changes. They are removed when their submitting action settles; other actions keep their edits. Draft callbacks must be deterministic: replay mustn't perform network or other side effects.

reconcile

reconcile(data: T, key: str | Callable[[Any], Any] | None = 'id') -> _Reconcile[T]

Prepare replacement data, preserving matched list entity identities.

key names the field that identifies list entities ("id" by default), or is a function computing the identity from an entity. key=None replaces list entities positionally. Duplicate keys are matched in occurrence order, without assigning the same entity to multiple rows.

snapshot

snapshot(value: T) -> T

Return a detached, untracked copy of the currently visible state.

deep

deep(value: T) -> T

Return a detached snapshot, tracking changes only within this subtree.