ECOSYSTEM / STORE

janelkit-store

The shared data of an application in one place: a class with reactive fields, actions that group and record each change, controlled writes, one instance per application, and persistence.

Ecosystem package janelkit-store Shared application data: stores with actions, controlled writes and change tracking, one instance per application, and persistence. pip install janelkit-store
STORE · PAGE 1 / 8~2 min read
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

What it adds

Most applications have data that several views or windows read and change: the signed-in user, the open documents, a cart. A store keeps that data in one class. Its fields are reactive and are read without .value, its methods are actions that group their changes and record them, and use(Class) gives every view and window the same instance, without provide or props. A strict store can only be changed by its own methods, and a store can save itself to disk.

What you keepWhere
What only one component uses (a draft, whether a panel is open)state in its <script>
What several views or windows share (the user, the documents, the cart)A store
An object with state of its own that exists several times (a camera, a connection)A class with state attributes, or a store with key=
A single preference that is rememberedpersisted

In one minute

Install it next to janelkit with pip install janelkit-store. Declare the store as a class decorated with @store, usually in src/stores. Every annotated attribute is a field; every method is an action. Any component gets the application’s instance with use(Cart).

src/stores/cart.py
from janelkit import computed
from janelkit_store import field, store

@store(strict=True)
class Cart:
    items: list[dict] = []
    note: str = field("", bind=True)    # in a strict store, only marked fields accept bind:

    @computed
    def total(self) -> float:           # derived and cached
        return sum(item["price"] for item in self.items)

    def add(self, name, price):         # every method is an action
        self.items.append({"name": name, "price": price})

    def clear(self):
        self.items.clear()
Copy
src/components/CartBadge.janel
<script lang="python">
from janelkit_store import use
from stores.cart import Cart

cart = use(Cart)
</script>

<text>{len(cart.items)} item(s) · {cart.total:.2f} €</text>
Copy
src/App.janel
<script lang="python">
from janelkit_store import use
from stores.cart import Cart
from components.CartBadge import CartBadge

cart = use(Cart)
</script>

<window title="Shop" width={420} height={260}>
  <column padding={24} gap={12}>
    <CartBadge />
    <row gap={8}>
      <button on:click={cart.add("Apple", 0.5)}>Add an apple</button>
      <button on:click={cart.clear()}>Empty the cart</button>
    </row>
    <textfield bind:value={cart.note} label="Note" placeholder="A note for the order" />
  </column>
</window>
Copy

Complete example in 3 files · Save each one at the path shown above it.

How it works

  1. use(Cart) returns the same instance in App and in CartBadge, so both see every change.
  2. Fields and derived values are read without .value, in Python and in the template: cart.items, cart.total.
  3. cart.add(...) is an action: its changes are applied together and recorded as one Mutation.
  4. note is marked bind=True, so bind:value can write it even though the store is strict.

What you should see

The badge shows 0 item(s) · 0.00 €. Each Add an apple adds one item and 0.50 €; Empty the cart returns to 0 item(s) · 0.00 €.

Built on the public API

janelkit-store uses only the public API of the core: janelkit.ext.session() for one instance per application and its data folder, on_write and own to know which field each write belongs to (and to enforce strict mode), Scope so that what a store creates belongs to it, @computed on methods, and bind: through descriptors (__jk_bind__).

Learn moreBuild your own package ↗

Versions and changes

janelkit-store shares the series with the core: janelkit-store 0.2.x.x works with janelkit 0.2.x.x and Python 3.10 or later. The last two numbers are its own.

0.2.0.0 is its first version: store, field, use, subscribe, snapshot, reset and mutations. persisted used to be part of the core: change from janelkit import persisted to from janelkit_store import persisted. janel migrate makes the change for you.

Learn moreChanges and migration ↗

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.