Lifecycle and state
The pipeline
Section titled “The pipeline”Every run follows the same pipeline:
- Load: evaluate the script in a fresh context and check it.
- Begin: build
ctx.previousis the state from the last successful run, ornullon the first run. run(ctx)returnscurrent. It must be JSON-serializable and at most 64 KB once serialized.onNotify(previous, current, ctx)runs with a 2 s limit.previousandcurrentare fresh copies parsed from JSON, so mutations made inrun()do not leak into them.summary(current, ctx)runs with a 2 s limit.- Validate: notifications and the summary are normalized. Invalid parts are dropped and logged as warnings, and strings are truncated to their limits.
- Commit: state, store and summary are saved atomically, and only then are notifications delivered. If any phase throws or times out, nothing is committed: the item keeps its previous state, store and summary, and shows an error.
Phase rules
Section titled “Phase rules”| Phase | Network (ctx.http, ctx.net) |
Store | Time limit |
|---|---|---|---|
| Top-level code | no | — | 5 s |
run |
yes | read and write | meta.timeout (default 20 s) |
onNotify |
no | read and write | 2 s |
summary |
no | read only | 2 s |
validate |
yes | read and write | 20 s |
Consequences
Section titled “Consequences”- The first run should usually only record a baseline. Returning nothing from
onNotifywhenprevious === nullis the norm. - The app never runs scripts just to draw the feed; it shows the summary saved by the last run.
Keep everything
summary()needs in the state or the store. - Dates survive the round trip as ISO strings, because
JSON.stringify(new Date())produces one. - A run that fails is invisible to the next one: it sees the last successful state and store.
Triggers
Section titled “Triggers”ctx.trigger says why a run happened:
| Trigger | |
|---|---|
foreground |
The app became active and the item was due. |
pull |
Pull to refresh, Update All (also from the watch or the Mac’s menu bar item), or the Refresh All Items Shortcut. |
background |
A background wake on iOS, or the Mac’s schedule. |
manual |
Update Now (also from the watch), the Refresh Item Shortcut, a reload from the Actions folder, or a run started over the server’s API. |
server |
A scheduled run on the server. |
test |
Test Run in the item’s settings. |
Where scripts run
Section titled “Where scripts run”The same script runs in the apps (JavaScriptCore) and on the companion server (Node). The user
chooses per item where it runs; an item that moves takes its inputs, secrets, state and store
along, so previous and the store carry on as if nothing happened. Both hosts evaluate the same
prelude, give scripts the same globals (only ECMAScript built-ins and what the prelude defines: no
require, fetch, URL or atob) and apply the same rules and limits. The shared conformance
cases in conformance/ run on both hosts.
Differences:
- On the server, each run also has a memory limit (128 MB). A script that uses more fails with a
limiterror. JavaScriptCore offers no such limit. - On the server, an endless loop costs nothing beyond the run: the worker running it is stopped at the deadline. In the app, the thread stays blocked until the loop ends (see Errors and limits).
- Engines word their own errors differently, for example for a syntax error or
undefined.x. Errors from the prelude and the hosts (permissions, limits, timeouts) read the same everywhere.