ECOSYSTEM / STORE

Tracking changes

Follow every change with subscribe, copy the fields with snapshot, go back to the defaults with reset, and log every action.

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 5 / 8~1 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.

Every action produces a Mutation

Each action that changes something produces a Mutation: store (the class name), key, action (the method, "bind:field" or None) and changes, one Change per field with field, kind ("set" if it was assigned, "mutate" if it was changed in place), old and new. The copies in old and new are only made while someone observes, with subscribe or JANELKIT_STORE_LOG; otherwise they are None.

subscribe(instance, fn) calls fn(mutation) after each action that changes something and returns a function that cancels it. snapshot(instance) is a copy of the public fields, and reset(instance) returns them to their defaults as an action named reset.

python
# Ordinary Python: follow every change of a store.
from janelkit_store import reset, snapshot, store, subscribe

@store
class Counter:
    count: int = 0

    def add(self):
        self.count += 1

counter = Counter()
seen = []
cancel = subscribe(counter, seen.append)

counter.add()
change = seen[0].changes[0]
assert seen[0].action == "add"
assert (change.field, change.kind, change.old, change.new) == ("count", "set", 0, 1)
assert snapshot(counter) == {"count": 1}

reset(counter)
assert counter.count == 0
cancel()
Copy

How it works

  1. subscribe observes the instance, so the Change carries the old and new values.
  2. snapshot returns plain data, safe to save or compare.
  3. reset is an action too, so subscribers see it.

Log every action

With the environment variable JANELKIT_STORE_LOG=1, every Mutation is logged to the janelkit_store logger. mutations() returns the mutations of the running application, from the oldest to the newest; in tests, app.mutations() does the same for the mounted application.

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