Skip to content

Lifecycle and state

Every run follows the same pipeline:

  1. Load: evaluate the script in a fresh context and check it.
  2. Begin: build ctx. previous is the state from the last successful run, or null on the first run.
  3. run(ctx) returns current. It must be JSON-serializable and at most 64 KB once serialized.
  4. onNotify(previous, current, ctx) runs with a 2 s limit. previous and current are fresh copies parsed from JSON, so mutations made in run() do not leak into them.
  5. summary(current, ctx) runs with a 2 s limit.
  6. Validate: notifications and the summary are normalized. Invalid parts are dropped and logged as warnings, and strings are truncated to their limits.
  7. 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 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
  • The first run should usually only record a baseline. Returning nothing from onNotify when previous === null is 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.

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.

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 limit error. 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.