The card file
State and sync
What a card remembers, and keeping it the same on every device.
State is what a card remembers: a count, a list of to-dos, the end time of a timer. Declare each key with a type and a default. State survives restarts, and actions change it with steps like set, push, increment, update and remove.
"state": {
"deploys": {
"type": "list",
"default": []
},
"lastText": {
"type": "text",
"default": ""
}
}
Declaring a key
| type | required |
| default | value of that type |
| sync | bool: true keeps this key the same on the app's copies on the user's other devices (SPEC §66) |
| Types | text, number, bool, list, object, any |
State persists across restarts. "sync": true on a key keeps it the same on every copy of the app on the user's devices that has Sync to My Devices on: a change on one device reaches the others, and the newest change wins (a list or object syncs as one value). Not for a source's key (sources run on each device), not with top-level "sync": "never", at most 20 keys. Store times as absolute (an end time, not seconds left), so a timer lines up everywhere. Top-level on_sync runs an action after synced values arrive (to schedule this device's own notifications). Sync only what makes sense on every device: not a search box or a picked row. A source's key needn't be declared (it becomes 'any'). Names can't be settings, meta, running, now, item, index, args or ui (nor params in an app with a detail card).
Keep raw values
Store numbers, Unix times and lists, and format them where they show: $total | currency, $ends_at | until, $todos | where:{done:false} | count. Never store a formatted string; the person’s locale decides how it reads.
Bindings and formatters
A source’s key needn’t be declared: it’s filled by the source. Each source also gets $meta.<key>.updated, .error and .status.
Synced state
Add "sync": true to a key and it stays the same on every copy of the card on your devices. A change on one device, by any step, reaches the others; the newest change wins; a copy added later starts with the latest value. No inbox or send step needed.
"state": {
"todos": { "type": "list", "default": [], "sync": true },
"ends_at": { "type": "number", "default": 0, "sync": true },
"query": { "type": "text", "default": "" }
}
- Sync only what belongs on every device. A search box, a picked row or results stay local, like query above.
- A list or object syncs as one value, so keep items that belong together in one key.
- Store times as absolute (
ends_at, not seconds left), so a timer lines up everywhere. - It works once the person turns on Sync to My Devices for the card on each device. No tool and no card can turn that on.
- Not for a source’s key (sources run on each device), and not with top-level
"sync": "never".
on_sync
SPEC §66: an action to run after synced state (state keys with "sync": true) arrives from another device, e.g. to schedule this device's own notifications for a timer started elsewhere. $args.keys (the keys that changed), $args.previous (each one's value before, e.g. to cancel the notification of a timer removed elsewhere), $args.from (the device's name), $args.replay (true while a copy just added catches up). In this run, send steps send nothing and writes to synced keys stay on this device
"on_sync": "reschedule",
"actions": {
"reschedule": { "steps": [
{ "notify": { "title": "Tea", "body": "Time’s up", "at": "$ends_at", "id": "tea" } }
] }
}
Here a timer started on the iPhone also rings on the Mac: when ends_at arrives from the phone, the Mac schedules its own notification for that time.
Shared value, or a message?
Synced state is for a value that should be the same everywhere: a counter, a list, a running timer. For a log of things that happened (a ping, a photo, a command to run on your computer) use send and an inbox. Between devices