Files
native/src/widgets/serial.ts
T
Linus Rath 26d0cf3ccc fix: keep a widget archive when a second tap's refresh lands before it #52
Every widget tap runs as its own headless task in the same JS runtime,
and the tasks interleave at every await. Each one loaded the stored
snapshot, edited it and saved it back, then refreshed from the server.
That went wrong in three ways:

- two taps loaded the same snapshot and the second save dropped the
  first change;
- the refresh of the first tap read the server before the second tap's
  archive landed and was stored anyway, so the message came back (seen
  on the emulator with a slow `Email/set`);
- a redraw of an older snapshot could land after a newer one, leaving
  buttons that act on a message the widget no longer shows.

The snapshot now only holds what the server last said. A button's
change is kept as a pending op next to it and laid over it when a
widget draws; it stays until a refresh that started after the server
took it has been stored (calendar and task changes only count as caught
up by a refresh that loaded calendar and tasks). A call that fails
drops its op, which also undoes an archive made offline: before, the
failed refresh kept the edited snapshot and the message stayed hidden
although it was never archived. Reads, writes and redraws of the
widget data run one at a time, and every redraw draws the latest view.
2026-10-02 11:32:38 +02:00

17 lines
737 B
TypeScript

// One lane for everything that reads and writes the stored widget data or
// redraws the widgets. Each widget event (every tap, every periodic update)
// runs as its own headless task in the same JS runtime, and they interleave
// at every await; without this, two taps load the same snapshot and the
// second save drops the first change, and a redraw of an older snapshot can
// land after a newer one, leaving buttons that act on a message the widget
// no longer shows.
let tail: Promise<unknown> = Promise.resolve();
/** Run `fn` after everything queued before it has finished. */
export function serial<T>(fn: () => Promise<T>): Promise<T> {
const run = tail.then(fn, fn);
tail = run.catch(() => undefined);
return run;
}