Skip to content

Stores

wybthon.store

store

Reactive stores for nested state, inspired by SolidJS createStore.

Stores provide fine-grained reactive access to nested objects and lists. Each path through the store is backed by its own Signal, so reading store.user.name only subscribes the current computation to that specific leaf, not to the entire store.

Public surface:

  • create_store: build a store from any initial value.
  • produce: batch mutations through a mutable draft.
Example
from wybthon import create_store, produce

store, set_store = create_store({
    "count": 0,
    "user": {"name": "Ada", "age": 30},
    "todos": [
        {"id": 1, "text": "Learn Wybthon", "done": False},
    ],
})

store.count           # 0
store.user.name       # "Ada"
store.todos[0].text   # "Learn Wybthon"

set_store("count", 5)
set_store("user", "name", "Jane")
set_store("count", lambda c: c + 1)
set_store("todos", 0, "done", True)

set_store(produce(lambda s: setattr(s, "count", s.count + 1)))
See Also

Functions:

Name Description
create_store

Create a reactive store from an initial value.

produce

Create a producer for batch-mutating store state.

create_store

create_store(initial: Any) -> Tuple[Any, _StoreSetter]

Create a reactive store from an initial value.

Parameters:

Name Type Description Default
initial Any

Initial state. Dicts and lists are wrapped in reactive proxies; other values are returned unchanged.

required

Returns:

Type Description
Any

A tuple (store, set_store) where store is a reactive

_StoreSetter

proxy that tracks reads per-path, and set_store is a setter

Tuple[Any, _StoreSetter]

supporting path-based updates and

Tuple[Any, _StoreSetter]

produce batches.

Example
store, set_store = create_store({"count": 0, "user": {"name": "Ada"}})

store.count         # 0
store.user.name     # "Ada"

set_store("count", 5)
set_store("user", "name", "Jane")
set_store("count", lambda c: c + 1)

See the module docstring for the full set of supported calling conventions.

produce

produce(fn: Callable[..., None]) -> _ProduceResult

Create a producer for batch-mutating store state.

fn receives a mutable draft of the store. Mutations are recorded and applied reactively when the producer is passed to a store setter created by create_store.

The draft supports attribute access, item access, and append / pop for lists.

Parameters:

Name Type Description Default
fn Callable[..., None]

A function that mutates the supplied draft. The draft is consumed immediately when applied; do not keep a reference past the call.

required

Returns:

Type Description
_ProduceResult

A marker object recognized by the store setter.

Example
set_store(produce(lambda s: setattr(s, "count", s.count + 1)))

set_store(produce(lambda s: s.todos.append(
    {"text": "New", "done": False}
)))

Public API

Store creation
  • create_store(initial) -> (store, set_store). Create a reactive store from a dict or list.
Store setter

The set_store setter supports several calling conventions:

  • set_store("key", value). Set a top-level key.
  • set_store("key", fn). Functional update (fn receives current value).
  • set_store("a", "b", value). Nested path.
  • set_store("a", 0, "done", True). Path with list index.
  • set_store(produce(fn)). Batch mutations via produce.
  • set_store({"a": 1, "b": 2}). Update multiple top-level keys.
  • set_store(fn). Functional update on the entire store (fn receives raw data).
Produce
  • produce(fn). Create a producer for batch-mutating store state; pass to set_store.

Type hints are provided for all public functions and classes.