A Contract resource asks its source again whenever an argument changes, and a reply can arrive after the user has moved on. Sources have no ambient clock and must finish each step inside the 100 ms turn budget. The root contract handles these constraints with a small set of patterns that every package repeats. Use them when adding a resource.
Epoch counters for pull to refresh
A state counter is passed as the last argument of a resource. Bumping it changes the arguments, so Exact asks again while the old answer stays on screen.
readEpoch feeds docs, directory and authors (lines 103-105). readRefresh adds one (line 310).
discussEpoch + readEpoch feeds both discuss resources, so refreshing a document also refreshes its comments.
On the data side, epochSeen compares the epoch with the last one seen for that server and drops the module caches when it differs. refresh <resource> in an action is the other form: it asks again with the same arguments (readRetry, meRetry).
Signed-time stamps
A request signed with a time cannot take performanceNow() as a resource argument: that would ask every frame. Instead one action writes a stamp, and the stamp is the argument. Activity's block comment states it (lines 126-131): the inbox arguments are "a stamp one action writes (activityAsk), never a clock read per frame". activityAsk copies server and currentAccount and sets activityAskAt to time.epochAtZero + performanceNow(), or the previous value plus one so a retry at the same instant still asks. activityAskAt == 0 parks the resource. Agents use the same idea with agentsRequestTime and agentsReadTime, added to time.epochAtZero inside the resource arguments. Vault passes time.epochAtZero through with.
Mutations take the time directly, as time.epochAtZero + performanceNow() evaluated in the sending action.
Owner and visit checks
Replies carry the identity of the request, and derives compare it with the present before anything is shown.
Derive | Check |
|---|---|
| |
The reply's | |
The reply's | |
The open-site sheet is on top and | |
A key export is displayed only on the identity it was requested for |
The visit id top(nav).id is unique per navigation entry, so passing it to a source and echoing it in the reply (as profilePublish does) ties a reply to one visit of one screen.
refreshes and then on mutations
mutation m as shape S refreshes a, b then after re-asks the named resources when the reply lands and then runs a parameterless action. Examples: discussPosted refreshes discussions, discussThreads then discussAfterPost; profileSaved refreshes meProfiles, authors, docs. The then action reads the reply with match m { case some(r) ... } and navigates or clears drafts. vaultEdited adds queue, which serialises sends: one request in flight, later ones in order.
Committed-snapshot handoff
When a write returns the new canonical state, the source keeps it for the refresh that follows, so the screen does not wait for a second request. In src/activity/sources.ts, activityApply stores its page in a module variable committed; the next activityLoad returns it once if account and server match, then clears it.
Optimistic sends
agentsSend copies the draft into agentsOptimistic and records the current row count. The derive agentsVisibleOptimistic shows that text as a pending bubble until a row with the same text and mine appears after that count. A failed send clears the bubble and keeps the draft. See Agents: screens.
Tasks
A task is a gated timer owned by the root.
Form | Example | Use |
|---|---|---|
| Runs for the app's life | |
|
| Poll or tick while a condition holds |
|
| Run once each time the key changes: initialise a form or stamp a visit |
|
| A delayed one-shot |
after(1, ...) keyed on top(nav).id is the standard way to act when a route opens.
The minute clock
readTick is set to performanceNow() every 60 seconds by readClock. Views compute time.epochAtZero + readTick for relative dates. The comment is explicit: "views read it, sources never do", so a tick redraws labels without refetching anything. Activity keeps its own activityTick, and agents a one second agentsLiveTick that runs only while a run is live.
Visibility-gated polling
Vault's poll and sync tasks include vaultPage.visibilityState == "visible" in their gate (lines 261-268), where vaultPage is the reserved exactPage() resource. Polling stops in the background, and vaultForeground syncs once on return. Tab gates do the same job for activity and agents: activityActive and agentsActive are nav.tab == "...", and each resource takes that flag as an enabled argument so its source returns an empty value without a request.
Related
The root contract and its declaration tables
Agents: streams: reconnect and backoff tasks
Read: loading pages, caches and embeds: what an epoch change drops
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime