Tracking changes
Follow every change with subscribe, copy the fields with snapshot, go back to the defaults with reset, and log every action.
janelkit-store Shared application data: stores with actions, controlled writes and change tracking, one instance per application, and persistence. pip install janelkit-storeEvery 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.
# 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()CopyHow it works
- subscribe observes the instance, so the Change carries the old and new values.
- snapshot returns plain data, safe to save or compare.
- 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 ↗.