fix(native): a tick on the watch, the phone or another member's device reaches every watch at once

The watch's snapshot was rebuilt every fifteen minutes, so a tick in the app or one arriving over the WebSocket did not reach the watch until then. It is now rebuilt as soon as the packing lists or the to-dos change in the offline tables. A tick on the watch goes as a message while the phone is in reach, which wakes the phone app, and a phone app in the background writes it to the server itself, so the trip's other devices hear of it at once instead of when the phone app is next opened.
This commit is contained in:
Maurice
2026-11-08 10:15:20 +01:00
parent 5f4fb5dae7
commit afaacec61a
5 changed files with 233 additions and 4 deletions
@@ -0,0 +1,71 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { offlineDb } from '../db/offlineDb';
import type { PackingItem, TodoItem } from '../types';
import { onWatchListsChange } from './watchListChanges';
// FE-NATIVE-WATCHSYNC-001 onwards: the watch hears of a list change at once, not on the next refresh.
const packing = (id: number, checked: number) =>
({ id, trip_id: 1, name: `Item ${id}`, checked }) as unknown as PackingItem;
const todo = (id: number, checked: number) => ({ id, trip_id: 1, name: `Task ${id}`, checked }) as unknown as TodoItem;
/** Long enough for the live query to answer and the settle window to pass. */
const settled = () => new Promise((resolve) => setTimeout(resolve, 120));
describe('onWatchListsChange', () => {
let stop: (() => void) | null = null;
beforeEach(async () => {
await offlineDb.packingItems.clear();
await offlineDb.todoItems.clear();
await offlineDb.packingItems.put(packing(1, 0));
await offlineDb.todoItems.put(todo(2, 0));
});
afterEach(() => {
stop?.();
stop = null;
});
it('FE-NATIVE-WATCHSYNC-001: subscribing is no change, a tick on the packing list is', async () => {
const run = vi.fn();
stop = onWatchListsChange(run, 20);
await settled();
expect(run).not.toHaveBeenCalled();
await offlineDb.packingItems.put(packing(1, 1));
await settled();
expect(run).toHaveBeenCalledTimes(1);
});
it('FE-NATIVE-WATCHSYNC-002: a to-do ticked, here or by another member, counts as well', async () => {
const run = vi.fn();
stop = onWatchListsChange(run, 20);
await settled();
await offlineDb.todoItems.put(todo(2, 1));
await settled();
expect(run).toHaveBeenCalledTimes(1);
});
it('FE-NATIVE-WATCHSYNC-003: a run of ticks settles into one refresh', async () => {
const run = vi.fn();
stop = onWatchListsChange(run, 60);
await settled();
await offlineDb.packingItems.put(packing(1, 1));
await offlineDb.packingItems.put(packing(3, 0));
await offlineDb.todoItems.put(todo(2, 1));
await new Promise((resolve) => setTimeout(resolve, 200));
expect(run).toHaveBeenCalledTimes(1);
});
it('FE-NATIVE-WATCHSYNC-004: after the unsubscribe nothing is called any more', async () => {
const run = vi.fn();
stop = onWatchListsChange(run, 20);
await settled();
stop();
stop = null;
await offlineDb.packingItems.put(packing(1, 1));
await settled();
expect(run).not.toHaveBeenCalled();
});
});
+42
View File
@@ -0,0 +1,42 @@
import { liveQuery } from 'dexie';
import { offlineDb } from '../db/offlineDb';
/** Long enough to take a run of ticks (or a synced batch) as one change, short enough to read as instant. */
const SETTLE_MS = 300;
/**
* Calls `run` once the packing lists or the to-dos this device keeps offline
* have changed, after they settled: a tick in the app, a tick from the watch
* written through, or another member's tick arriving over the WebSocket (the
* remote event handler writes it to the same tables). The watch's snapshot is
* built from those tables, so this is what keeps the watch as current as the
* phone instead of waiting for the next quarter-hour refresh.
*
* The first answer of the query is the state at subscription, not a change, and
* is skipped. Returns the unsubscribe.
*/
export function onWatchListsChange(run: () => void, settleMs = SETTLE_MS): () => void {
let first = true;
let timer: ReturnType<typeof setTimeout> | null = null;
const subscription = liveQuery(() =>
Promise.all([offlineDb.packingItems.toArray(), offlineDb.todoItems.toArray()])
).subscribe({
next: () => {
if (first) {
first = false;
return;
}
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
timer = null;
run();
}, settleMs);
},
// A failed read changes nothing on the watch; the next write tries again.
error: () => undefined,
});
return () => {
if (timer) clearTimeout(timer);
subscription.unsubscribe();
};
}