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
¶
Internal sequence edit. Indices refer to the state before this edit.
Store
¶
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 |
StoreList
¶
Bases: _Proxy, Sequence[T]
The read-only reactive view of a list store. Entity proxies survive reorders.
StoreObject
¶
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
¶
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_optimistic_store
¶
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
¶
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
¶
Return a detached, untracked copy of the currently visible state.