Distinguish one learner's UI state from explicitly shared application data.
Each browser session owns a graph. A Signal created inside the app callable belongs to that graph, so one learner's draft does not become another learner's draft. A module-level Signal instead belongs to the process and is shared.
Neither boundary is a business database. A process-local shared value does not automatically coordinate several workers, and an ordinary UI Signal does not promise saved account data after refresh.
Understand the moving parts
The two counters below are independent component instances inside one session. A second browser tab is a stronger isolation check: it constructs a different application graph. Increment one counter and edit the private draft, then compare a newly opened copy of this page.
Choose intentional sharing only when the product needs it, then define ownership, authorization and concurrency. Keeping private state local is simpler than trying to remove accidental sharing after several users already depend on it.
Walk through the code
from pysx import Fragment, pysx, signal
def counter(prefix: str ="") -> Fragment:
count = signal(0)
snapshot = count()
def increment(_: object) -> None:
count.update(lambda value: value +1)
return pysx(t"""
button(id={prefix + "increment"} onClick={increment}): "Increment"output(id={prefix + "live-count"}): {count}
p: "Snapshot: "output(id={prefix + "snapshot-count"}): {snapshot}
""")
def app() -> Fragment:
draft = signal("")
first = counter()
second = counter("second-")
return pysx(t"""
label(for="private-draft"): "Private session draft"input(id="private-draft"bindValue={draft})
output(id="private-echo"): {draft}
{first}
{second}
p: "Open this lesson in a new tab to construct another independent graph."
""")
app creates one private draft and calls counter twice. Each call owns its own count and initial snapshot. Another tab or a refreshed document constructs a fresh graph; it does not inherit either existing counter.
Try it live
Edit a private draft and increment an instance. Open this lesson in a new tab, then refresh it.
What to observe
The new tab and refreshed page start fresh; the existing open tab keeps its own current state.
Snapshot:
Snapshot:
Open this lesson in a new tab to construct another independent graph.
Take this with you
Make state boundaries a deliberate product decision and verify them with two sessions.