packages/ui of iterate/private at 262a86d8745055d818c58c448ca226b407e0fc2a
ui release builder, 2026-11-27 00:06 UTC · 203f7da6d0f81d840c6d78b2681d26bb3f13c6ff · parent 411c456
3 files changed
- M src/components/context-catalog/catalog-collection.test.ts
- M src/components/context-catalog/catalog-collection.ts
- M src/components/context-catalog/context-catalog-live.test.tsx
M src/components/context-catalog/catalog-collection.test.ts
@@ -212,7 +212,7 @@ catalog.put("/a", 10); catalog.put("/b", 20); catalog.put("/c", 30); - const watcher = catalogWatcher(catalog.itx); + const watcher = catalogWatcher(catalog.itx, { commitEveryMs: 0 }); const changes: { del: string[]; put: string[] }[] = []; const watch = watcher.watch({ order: "active", limit: 10 }, CatalogRow, ({ del, put }) => changes.push({ del, put: put.map((row) => row.path) }), @@ -239,7 +239,7 @@ test("a change that comes with a gap starts the watch again", async () => { const catalog = fakeCatalog(); catalog.put("/a", 10); - const watcher = catalogWatcher(catalog.itx); + const watcher = catalogWatcher(catalog.itx, { commitEveryMs: 0 }); const watch = watcher.watch({ order: "active", limit: 10 }, CatalogRow, () => {}); await watch.ready; const [live] = catalog.watching; @@ -255,7 +255,7 @@ const itx: CatalogItx = { contexts: { watch: () => Promise.reject(new TypeError("watch is not a function")) }, }; - const watcher = catalogWatcher(itx); + const watcher = catalogWatcher(itx, { commitEveryMs: 0 }); const said: string[] = []; watcher.subscribe(() => said.push(watcher.support())); const watch = watcher.watch({ awake: true }, CatalogAwakeRow, () => {}); @@ -346,7 +346,7 @@ test("a refusal that is not the query's makes the catalog no source, a limit of watches does not, and a later probe makes it live", async () => { const catalog = fakeCatalog(); - const watcher = catalogWatcher(catalog.itx); + const watcher = catalogWatcher(catalog.itx, { commitEveryMs: 0 }); catalog.refuseNext("UNAVAILABLE"); watcher.probe(); await tick(); @@ -366,10 +366,29 @@ expect(watcher.support()).toBe("none"); }); +test("the catalog's changes are committed at most once in the time given, and an answer at once", async () => { + const catalog = fakeCatalog(); + catalog.put("/a", 10); + const watcher = catalogWatcher(catalog.itx, { commitEveryMs: 120 }); + let commits = 0; + watcher.onBatch(() => (commits += 1)); + const watch = watcher.watch({ order: "active", limit: 10 }, CatalogRow, () => {}); + await watch.ready; + expect(commits).toBe(1); + for (const at of [20, 30, 40, 50]) catalog.put("/a", at); + // the frames are in the watch at once; their commit waits + expect(watch).toMatchObject({ frames: 4 }); + expect(commits).toBe(1); + expect(watcher.batchOpen()).toBe(true); + await new Promise((resolve) => setTimeout(resolve, 160)); + expect(commits).toBe(2); + expect(watcher.batchOpen()).toBe(false); +}); + test("a batch whose last frame never comes is ended by the page itself", async () => { const catalog = fakeCatalog(); catalog.put("/a", 10); - const watcher = catalogWatcher(catalog.itx); + const watcher = catalogWatcher(catalog.itx, { commitEveryMs: 0 }); const watch = watcher.watch({ order: "active", limit: 10 }, CatalogRow, () => {}); await watch.ready; let ends = 0; @@ -409,7 +428,7 @@ }, }, }; - const watcher = catalogWatcher(itx); + const watcher = catalogWatcher(itx, { commitEveryMs: 0 }); const watch = watcher.watch({ awake: true }, CatalogAwakeRow, () => {}); say({ epoch: 1, state: "opening" }); say({ epoch: 1, state: "open" }); @@ -433,7 +452,7 @@ }, }, }; - const watcher = catalogWatcher(itx); + const watcher = catalogWatcher(itx, { commitEveryMs: 0 }); const watch = watcher.watch({ awake: true }, CatalogAwakeRow, () => {}); await watch.ready; for (const _ of [1, 2, 3]) { @@ -449,7 +468,7 @@ test("a query that the catalog refuses ends its watch alone", async () => { const catalog = fakeCatalog(); - const watcher = catalogWatcher(catalog.itx); + const watcher = catalogWatcher(catalog.itx, { commitEveryMs: 0 }); const awake = watcher.watch({ awake: true }, CatalogAwakeRow, () => {}); await awake.ready; const refused = watcher.watch({ where: { refused: true } }, CatalogRow, () => {}); @@ -461,7 +480,7 @@ const tick = () => new Promise((resolve) => setTimeout(resolve, 0)); function page(catalog: ReturnType<typeof fakeCatalog>) { - const watcher = catalogWatcher(catalog.itx); + const watcher = catalogWatcher(catalog.itx, { commitEveryMs: 0 }); // with indexes, as the Dash makes it (context-catalog-live.ts) const contexts = createCollection( contextsCollectionOptions({
M src/components/context-catalog/catalog-collection.ts
@@ -78,8 +78,9 @@ /** One context as a watch sends it (iterate/api `CatalogRow`): a listing without `awake` and * `activities`, with the two numbers that the orders sort by and the catalog's revision at which * the row last changed. `activeMs` is when it was last active (`lastActivityAt || lastEventAt`) - * and `createdMs` when its first event was written, in milliseconds. A row changes at each - * summary that its context sends: at most one in 15 s while it works. */ + * and `createdMs` when its first event was written, in milliseconds. A row changes when + * its context's summary changed (its events, its last activity, its size): for a context whose + * log grows, as often as once in 100 ms. */ export const CatalogRow = ContextListing.omit({ awake: true, activities: true }).extend({ activeMs: z.number(), createdMs: z.number(), @@ -224,7 +225,8 @@ /** `end()` runs after the last frame of each batch. One write of the catalog is one batch over * all of this page's queries, so a holder applies it as one transaction. */ onBatch(end: () => void): () => void; - /** True between a frame that says `more` and the end of its batch. */ + /** True while changes wait for their commit: between a frame that says `more` and the end of + * its batch, and until a commit that is held back (`commitEveryMs`). */ batchOpen(): boolean; support(): CatalogSupport; /** Asks, with one watch of the awake list that ends with its answer, whether the catalog is a @@ -264,7 +266,7 @@ error: string | undefined; /** The start that this watch listens to: a frame of an earlier start is of no watch. */ start: object | undefined; - handle: PromiseLike<CatalogWatchHandle> | undefined; + handle: Promise<CatalogWatchHandle> | undefined; ready: Promise<void>; resolveReady(): void; rejectReady(error: Error): void; @@ -274,7 +276,17 @@ /** THE CATALOG'S WATCHES OF ONE PAGE. A watch is of one session and ends with it: when a new * session opens, each watch starts again with its cursor, and the catalog sends every path of * the answer and only the rows that this page lacks. */ -export function catalogWatcher(itx: CatalogItx): CatalogWatcher { +export function catalogWatcher( + itx: CatalogItx, + { + commitEveryMs = 250, + }: { + /** A page's collections commit the catalog's changes at most this often: a busy mind sends + * ten frames a second and more, and each commit is a run of every live query and a draw of + * the page. An answer to a watch is committed at once. */ + commitEveryMs?: number; + } = {}, +): CatalogWatcher { const session = itxSessionOf(itx); const watches = new Set<Held<{ path: string }>>(); const batchEnds = new Set<() => void>(); @@ -306,11 +318,24 @@ // this page's own, since the catalog cuts a very long message in two and a frame can be lost // with its session: a transaction must not stay open. let batchTimer: ReturnType<typeof setTimeout> | undefined; - const endBatch = () => { + /** The commit that waits for its time (`commitEveryMs`), and when the last one was. */ + let commitTimer: ReturnType<typeof setTimeout> | undefined; + let committedAt = 0; + const commitNow = () => { + clearTimeout(commitTimer); + commitTimer = undefined; + committedAt = Date.now(); + for (const end of batchEnds) end(); + }; + /** The end of a batch. `answered`: the batch held an answer, which a load waits for. */ + const endBatch = (answered: boolean) => { clearTimeout(batchTimer); batchTimer = undefined; - for (const end of batchEnds) end(); + const wait = committedAt + commitEveryMs - Date.now(); + if (answered || wait <= 0) commitNow(); + else commitTimer ||= setTimeout(commitNow, wait); }; + let answeredInBatch = false; function refuse(held: Held<{ path: string }>, error: string) { held.error = error; @@ -325,8 +350,10 @@ held.start = undefined; held.handle = undefined; // a call that failed left nothing to end - void Promise.resolve(handle).then( - (answered) => answered?.[Symbol.dispose](), + void handle?.then( + (answered) => { + answered[Symbol.dispose](); + }, () => {}, ); } @@ -349,8 +376,11 @@ } catch (caught) { handle = Promise.reject(caught); } - held.handle = handle; - void Promise.resolve(handle).then(undefined, (caught: unknown) => { + // ONE resolution of the call, for every reader: each `then` on a capnweb promise can answer a + // stub of its own, and a watch ends only when every stub of its handle is disposed. + const answered = Promise.resolve(handle); + held.handle = answered; + void answered.then(undefined, (caught: unknown) => { if (held.start !== started) return; // the session went away under the call: its next epoch starts the watch again if (sessionNow() !== asked || session.get().state !== "open") return; @@ -362,9 +392,9 @@ }); // The catalog ended the watch and this page did not: watch again. A session that went away // rejects here, and its next epoch starts the watch. - void Promise.resolve(handle) + void answered // a platform whose handle has no `ended` never says that a watch ended - .then((answered) => (answered.ended ? answered.ended() : new Promise<never>(() => {}))) + .then((stub) => (stub.ended ? stub.ended() : new Promise<never>(() => {}))) .then( () => { if (held.start === started && session.get().state === "open") @@ -405,10 +435,16 @@ } } // The batch ends with its last frame, whoever that frame was for. + if (parsed.success && parsed.data.type !== "change") answeredInBatch = true; + const end = () => { + const answered = answeredInBatch; + answeredInBatch = false; + endBatch(answered); + }; if (parsed.success && parsed.data.more) { clearTimeout(batchTimer); - batchTimer = setTimeout(endBatch, 50); - } else endBatch(); + batchTimer = setTimeout(end, 50); + } else end(); } function apply(held: Held<{ path: string }>, frame: z.infer<typeof Frame>) { @@ -524,7 +560,7 @@ batchEnds.add(end); return () => batchEnds.delete(end); }, - batchOpen: () => Boolean(batchTimer), + batchOpen: () => Boolean(batchTimer || commitTimer), support: () => support, probe() { if (probing) return;
M src/components/context-catalog/context-catalog-live.test.tsx
@@ -26,7 +26,8 @@ await act(async () => { catalog.putAwake("/agents/a", 30); - await settled(); + // the page commits the catalog's changes at most once in 250 ms + await new Promise((resolve) => setTimeout(resolve, 300)); }); expect(page.last()?.rows?.map((row) => [row.path, row.awake?.since])).toEqual([ ["/agents/a", 30],