English
Store: shared key-value
both directions
One key/value table shared by both sides. JS writes it and Python can read it; Python changes it and JS sees the change.
It is meant for state both sides need: the current user, the selected item, settings. Reads hit a local cache, only writes send a message, which is why reading is a synchronous get() with no round trip.
Python side
controller.py
python
from axon import class AxonStore(id: str, *web_names, auto_create: bool = True)AxonStore
WEB = "MainView"
# get a store (same name means the same store; creating it again is idempotent)
store = class AxonStore(id: str, *web_names, auto_create: bool = True)AxonStore("docs-store", WEB)
store.set("user", "travis") # write: synchronised to pages holding a store of the same name
print(store.get("user")) # read: local cache, returns immediately
# the property style works too
store.prop("theme").set("dark")
print(store.prop("theme").value)JS side
app.js
js
import { class AxonStoreAxonStore } from "axon-gui";
const const store: AxonStorestore = new new AxonStore(id: string, auto_create?: boolean): AxonStoreAxonStore("docs-store"); // the name must match the Python side
const store: AxonStorestore.AxonStore.set(prop: string, value: any): void写一个键: store.set("k", 1) 等价于 store.prop("k").set(1)。
set("user", "travis"); // write: synchronised to Python
const store: AxonStorestore.AxonStore.get<any>(prop: string): any读一个键的值(两端同名同义):const v = store.get<number>("k")。
get("user").get(); // read: local cache
// after Python writes, reading sees its value
const const theme: anytheme = const store: AxonStorestore.AxonStore.get<any>(prop: string): any读一个键的值(两端同名同义):const v = store.get<number>("k")。
get("theme").get();Who notifies whom
| Action | What happens |
|---|---|
store.set("k", v) | updates the local value, then sends setFromPeer("k", v) to the counterpart |
| counterpart write | applies setFromPeer locally and does not echo back (otherwise the two sides would ping-pong forever) |
So "both sides agree" comes from each side calling set, not from automatic two-way sync: whichever side wrote last wins for that key.
Key points
- Names must match:
AxonStore("docs-store", ...)andnew AxonStore("docs-store")are the same store; different names mean two independent stores. - Reads do not need
await(in JSstore.get("k")is synchronous); usestore.prop("k")when you need the property object (to write back). - Do not use it as a message queue: it gives no ordering guarantees for notifications — use events for that.
- Values must be JSON-serialisable:
dict/list/ primitives.