Skip to content

store

wybthon.store

Reactive stores for nested state management, 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 the entire store.

Usage::

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},
    ],
})

# Reading (reactive):
store.count           # 0
store.user.name       # "Ada"
store.todos[0].text   # "Learn Wybthon"

# Writing via path-based setter:
set_store("count", 5)
set_store("user", "name", "Jane")
set_store("count", lambda c: c + 1)
set_store("todos", 0, "done", True)

# Writing via produce (batch mutations):
set_store(produce(lambda s: setattr(s, "count", s.count + 1)))

create_store(initial)

Create a reactive store from an initial value.

Returns (store, set_store) where store is a reactive proxy that tracks reads per-path, and set_store is a setter supporting path-based updates.

Example::

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

# Reactive reads:
store.count         # 0
store.user.name     # "Ada"

# Path-based writes:
set_store("count", 5)
set_store("user", "name", "Jane")
set_store("count", lambda c: c + 1)

See module docstring for full API documentation.

produce(fn)

Create a producer for batch-mutating store state.

The function fn receives a mutable draft of the store. Mutations on the draft are recorded and applied reactively when passed to set_store::

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

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

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.