A watch's call settles when the watcher's function has returned from the answer
Jonas Templestein, 2026-11-26 22:46 UTC · b942a1abc30420ed3b8ed191267c4f7b55ba696c · parent dc1cadf
3 files changed
- M core/itx/src/context/catalog-watch.ts
- M core/lib/src/api.ts
- M core/mind-worker/src/context/catalog-watch.test.ts
M core/itx/src/context/catalog-watch.ts
@@ -156,8 +156,11 @@ this.#waitUntil = deps.waitUntil; } - /** Asks the catalog to keep answering the ticket's query. Resolves when the first frame, the - * answer, has gone to `onFrame`; rejects with the catalog's reason when it refuses the query. */ + /** Asks the catalog to keep answering the ticket's query. Resolves when `onFrame` has RETURNED + * from the first frame, the answer: a watcher that awaits the call has the answer. Waiting for + * the function's return is what makes that true for a session that ends in another Worker, + * where the call's answer and a call of the function travel apart, in no order. Rejects with + * the catalog's reason when it refuses the query. */ async watch(ticket: CatalogWatchTicket, onFrame: FrameCallback): Promise<CatalogWatchHandle> { if (this.#over) throw codedError("UNAVAILABLE", "contexts.watch: the session ended"); const { query, cursor } = ticket.$itxCatalogWatch; @@ -359,7 +362,7 @@ if (!watch || watch.rev < 0) return; if (watch.gen !== message.gen || watch.rev !== from) return this.#askAgain(sub); watch.rev = rev; - this.#send(watch, { + void this.#send(watch, { type: "change", gen: message.gen, from, @@ -376,25 +379,33 @@ watch.gen = message.gen; watch.rev = message.rev; const head = { gen: message.gen, rev: message.rev, q: message.q, full: message.full }; - this.#send( + const handed = this.#send( watch, message.reset ? { type: "reset", ...head, rows: message.reset } : { type: "resume", ...head, ids: message.ids || [], rows: message.put || [] }, ); - if (!watch.answered) return; - clearTimeout(watch.answered.timer as never); - watch.answered.resolve(); - watch.answered = undefined; + const { answered } = watch; + if (!answered) return; + // the call that made the watch settles when the watcher's function has returned from this + // frame; until then the watch's end, or the deadline, still rejects it + void handed.then(() => { + if (watch.answered !== answered) return; + clearTimeout(answered.timer as never); + watch.answered = undefined; + answered.resolve(); + }); } - /** One frame to one watcher. Nothing waits for the watcher: the catalog's next frame goes when - * it comes, and a callback that throws is its own page's fault. */ - #send(watch: Watch, frame: CatalogFrame<unknown>): void { + /** One frame to one watcher: settles when the watcher's function has returned from it, or + * thrown. A change waits for nobody: the catalog's next frame goes when it comes, and a + * function that throws is its own page's fault. */ + #send(watch: Watch, frame: CatalogFrame<unknown>): Promise<void> { try { - void Promise.resolve(watch.onFrame(frame)).then(disposeRpcStub, () => {}); + return Promise.resolve(watch.onFrame(frame)).then(disposeRpcStub, () => {}); } catch { // a stub that broke: its `onRpcBroken` ends the watch + return Promise.resolve(); } } }
M core/lib/src/api.ts
@@ -2411,10 +2411,12 @@ * * `watch(query, onFrame, options?)` keeps answering a query: the catalog calls `onFrame` with * the answer, and then with each change to it, for as long as the handle lives - * (core/itx/src/context/reactive-catalog.ts). It is a session's call: the frames ride the - * session's own connection, and the session's end closes every watch. Who may `list` may - * `watch`. `options.cursor` says what the watcher holds already. `watches()` answers who - * watches what. */ + * (core/itx/src/context/reactive-catalog.ts). The call resolves when `onFrame` has returned + * from the answer, and rejects with the reason when the catalog refuses the query. It is a + * session's call: the frames ride the session's own connection, and the session's end closes + * every watch. The handle's `ended()` settles, with the reason, when the watch ends in any way + * but the watcher's own dispose. Who may `list` may `watch`. `options.cursor` says what the + * watcher holds already. `watches()` answers who watches what. */ contexts: { list(options?: ContextListOptions): Promise<ContextListing[]>; watch<Query extends CatalogQuery>( @@ -2423,7 +2425,7 @@ frame: CatalogFrame<Query extends { awake: true } ? CatalogAwakeRow : CatalogRow>, ) => void, options?: { cursor?: CatalogCursor }, - ): Promise<{ [Symbol.dispose](): void }>; + ): Promise<{ ended(): Promise<string>; [Symbol.dispose](): void }>; watches(options?: { verify?: boolean; plans?: boolean }): Promise<CatalogWatches>; }; processors: {
M core/mind-worker/src/context/catalog-watch.test.ts
@@ -32,10 +32,10 @@ await described(itx, "/tasks/two", { status: "closed", title: "Two" }); await listed(itx, { "/tasks/one": "open", "/tasks/two": "closed" }); - // 1. the answer: the open task, and no other context + // 1. the answer: the open task, and no other context. The call resolves when the watcher's + // function has returned from it, so the answer is here. const open = watcher(); using _watch = await itx.contexts.watch({ where: { status: "open" }, limit: 5 }, open.take); - expect(open.frames).toHaveLength(1); expect(open.frames[0]).toMatchObject({ type: "reset", full: false, @@ -57,27 +57,34 @@ eventTypes: { "events.iterate.com/itx/described": 1 }, }); - // 2. a context enters the answer: one frame, with the row + // 2. a context enters the answer: a frame with its row. (A context that works says a little + // more of itself when it goes quiet, so a frame for a row of the answer may follow any time.) await described(itx, "/tasks/two", { status: "open", title: "Two" }); await open.until((rows) => rows.length === 2); - expect(open.frames.at(-1)).toMatchObject({ - type: "change", - del: [], - rows: [{ path: "/tasks/two" }], - }); + expect(open.frames).toContainEqual( + expect.objectContaining({ + type: "change", + del: [], + rows: [expect.objectContaining({ path: "/tasks/two" })], + }), + ); // the newest activity first expect(open.rows().map((row) => row.path)).toEqual(["/tasks/two", "/tasks/one"]); - // 3. a context leaves it: one frame, with the path + // 3. a context leaves it: a frame with its path, and no row await described(itx, "/tasks/one", { status: "closed", title: "One" }); await open.until((rows) => rows.length === 1); - expect(open.frames.at(-1)).toMatchObject({ type: "change", del: ["/tasks/one"], rows: [] }); + expect(open.frames).toContainEqual( + expect.objectContaining({ type: "change", del: ["/tasks/one"], rows: [] }), + ); - // 4. a write that the query does not select sends this watcher nothing - const framesBefore = open.frames.length; + // 4. a context that the query does not select reaches this watcher in no frame await described(itx, "/tasks/three", { status: "closed" }); await listed(itx, { "/tasks/three": "closed" }); - expect(open.frames).toHaveLength(framesBefore); + await quiet(itx); + expect(open.frames.flatMap((frame) => frame.rows.map((row) => row.path))).not.toContain( + "/tasks/three", + ); // 5. every frame followed the one before it expect(open).toMatchObject({ gaps: [] }); @@ -115,13 +122,15 @@ { timeout: 60_000 }, async () => { await using runtime = await mind(); - const query: CatalogQuery = { under: "/tasks", order: "path", limit: 10 }; + const query: CatalogQuery = { childrenOf: "/tasks", order: "path", limit: 10 }; let cursor: CatalogCursor; { using itx = await session(runtime, "/"); for (const name of ["a", "b", "c"]) await described(itx, `/tasks/${name}`, { status: "open" }); await listed(itx, { "/tasks/a": "open", "/tasks/b": "open", "/tasks/c": "open" }); + // each context has said its last word: no row changes behind the cursor + await quiet(itx); const first = watcher(); using _watch = await itx.contexts.watch(query, first.take); const last = first.frames.at(-1)!; @@ -135,6 +144,7 @@ using itx = await session(runtime, "/"); await described(itx, "/tasks/b", { status: "closed" }); await listed(itx, { "/tasks/b": "closed" }); + await quiet(itx); const again = watcher(); using _watch = await itx.contexts.watch(query, again.take, { cursor }); expect(again.frames[0]).toMatchObject({ @@ -250,6 +260,13 @@ ([path, status]) => rows.get(path)?.status === status, ); }); +} + +/** Until no context under /tasks is awake: each has sent its last summary and closed its socket. */ +async function quiet(itx: Session) { + await until(async () => + (await itx.contexts.list({ under: "/tasks" })).every((row) => !row.awake), + ); } async function until(done: () => boolean | Promise<boolean>): Promise<void> {