diff --git a/demo/web/src/index.ts b/demo/web/src/index.ts index e946de64fbe..ea8d0de5e71 100644 --- a/demo/web/src/index.ts +++ b/demo/web/src/index.ts @@ -436,9 +436,9 @@ ui.run((effect) => { effect.spawn(async () => { try { for (;;) { - const value = await effect.race(consumer.next()); - if (value === undefined) break; - metaSignal.set(value); + const state = await effect.race(consumer.latest()); + if (state === undefined) break; + metaSignal.set(state.value); } } catch (err) { console.warn("error reading metadata", err); diff --git a/doc/lib/js/flate.md b/doc/lib/js/flate.md index e8380b93413..24e9aacad35 100644 --- a/doc/lib/js/flate.md +++ b/doc/lib/js/flate.md @@ -9,7 +9,7 @@ description: Opaque MoQ tracks, optionally compressed with group-scoped DEFLATE Opaque payloads over [`@moq/net`](/lib/js/net) tracks, in two modes: -- **Snapshot**: lossy latest-value, one payload per group. +- **Snapshot**: lossy latest-value, one payload per group. `next()` yields every value in order; `latest()` skips to the newest. - **Stream**: lossless append-log in a single group. The bytes are never inspected. Compression is opt-in: `"none"` (the default) @@ -26,9 +26,11 @@ producer.update({ value: payload, at: Time.Timestamp.now() }); ``` Producers take a `Timed` value, `{ value, at }`, where `at` is the capture -time written as the frame timestamp. Nothing fills in now: a timed track, such -as `createTrack(name, { timescale: Time.Timescale.MILLI })`, needs `at` on -every write, and an untimed track (no `timescale`) takes none. +time written as the frame timestamp, and consumers return the same shape. +Nothing fills in now: a timed track, such as +`createTrack(name, { timescale: Time.Timescale.MILLI })`, needs `at` on every +write, and an untimed track (no `timescale`) takes none and reads back without +one. A Stream rides one group, so the whole log shares `@moq/net`'s group budget: 32 MiB of payload and 8192 payloads. An `append` that might not fit throws diff --git a/doc/lib/js/json.md b/doc/lib/js/json.md index a3d42106d74..e9aa63a4426 100644 --- a/doc/lib/js/json.md +++ b/doc/lib/js/json.md @@ -12,14 +12,31 @@ are the catalog modes on the [hang](/concept/hang#data-tracks) page. Window is a third framing for a raw track: both ends opt into it, and a generic catalog reader will not pick it up. -- **Snapshot**: lossy latest value, with RFC 7396 merge-patch deltas. A delta before any snapshot is an error. +- **Snapshot**: lossy latest value, with RFC 7396 merge-patch deltas. A delta before any snapshot is an error. `next()` yields every state in order; `latest()` skips to the newest. - **Stream**: lossless append-log in a single group. A reader that falls behind fails the read rather than resuming mid-log. - **Window**: a bounded run of records a reader can join at any point. A new group restates what it keeps explicitly, so a reader that was keeping up is not handed a record twice. Both sides choose the same compression, `"none"` or `"deflate"`. Producers take a `Timed` value, `{ value, at }`, where `at` is the capture time written as -the frame timestamp. Nothing fills in now: a timed track needs `at` on every -write, and an untimed track (no `timescale`) takes none. +the frame timestamp, and the snapshot and stream consumers return the same +shape. Nothing fills in now: a timed track needs `at` on every write, and +an untimed track (no `timescale`) takes none and reads back without one. + +```ts +import { Snapshot } from "@moq/json"; + +// Hold each state back until the video playhead reaches it. +const consumer = new Snapshot.Consumer({ track }); +for await (const { value, at } of consumer) { + schedule(value, at); +} +``` + +`next()` buffers every state it has not yielded. On a timed track it skips a +group once the subscription's `maxDelay` proves it stale, and the default of +zero keeps only the newest group, so subscribe with a `maxDelay` covering how +far the playhead trails the live edge. An untimed track skips nothing; a reader +that cannot keep up calls `latest()` instead. A stream rides one group, so the whole log shares `@moq/net`'s group budget: 32 MiB of payload and 8192 records. An append that might not fit is refused diff --git a/doc/lib/js/watch.md b/doc/lib/js/watch.md index a6451e35755..28570dd1671 100644 --- a/doc/lib/js/watch.md +++ b/doc/lib/js/watch.md @@ -103,9 +103,9 @@ const dispose = el.signals.run((effect) => { const consumer = new Json.Snapshot.Consumer({ track }); effect.spawn(async () => { for (;;) { - const value = await effect.race(consumer.next()); - if (value === undefined) break; - console.log("metadata", value); + const state = await effect.race(consumer.latest()); + if (state === undefined) break; + console.log("metadata", state.value); } }); }); diff --git a/doc/setup/upgrade.md b/doc/setup/upgrade.md index f1088eeec8d..edd3c8b0a47 100644 --- a/doc/setup/upgrade.md +++ b/doc/setup/upgrade.md @@ -299,6 +299,17 @@ These land with the next breaking release, not the 2026-09-23 train. `TrackStream` accept a CMAF rendition's track at that timescale; a track you create yourself declares it with `track::Info::with_timescale`. Both decoders refuse a `trun` whose `data_offset` doesn't start at the next sample in the `mdat`. +- **@moq/json and @moq/flate consumers return `Timed` values.** The snapshot + and stream consumers' `next()` and async iterator yield `{ value, at }`, + where `at` is the frame's timestamp and absent on an untimed track; read + `.value` for what they used to return. A snapshot consumer's `next()` now + yields every state in order. Call `latest()` for the old behavior, which + skips to the newest state. A caller that passes the result on as `any` or + `unknown` (logging, `JSON.stringify`, a generic setter) still compiles but + now gets the wrapper and every intermediate state, so search for each + `next()` and `for await` over a snapshot consumer rather than relying on + type errors. On an untimed track nothing marks a group stale, so a `next()` + reader that falls behind replays the whole backlog where it used to skip it. ## Wire diff --git a/js/flate/README.md b/js/flate/README.md index 02642752f9a..4625243096f 100644 --- a/js/flate/README.md +++ b/js/flate/README.md @@ -9,7 +9,7 @@ Opaque byte tracks over MoQ, optionally compressed with group-scoped DEFLATE, in two modes: -- `Snapshot`: **lossy**. One value updated over time; a consumer only gets the most recent one. +- `Snapshot`: **lossy**. One value updated over time; a consumer reads every value it receives (`next()`) or skips to the most recent (`latest()`). - `Stream`: **lossless**. An ordered append-log of self-contained payloads, nothing superseded. The bytes are opaque: the tracks frame them onto a [`@moq/net`](../net) track and optionally compress them, and never look inside. For JSON documents reach for [`@moq/json`](../json) instead, which adds RFC 7396 merge-patch deltas on top of the same two modes and codec. Interoperable with the Rust [`moq-flate`](https://crates.io/crates/moq-flate) crate. @@ -22,15 +22,14 @@ const thumbnail = new Snapshot.Producer({ track, compression: "deflate" }); thumbnail.update({ value: jpeg, at: Time.Timestamp.now() }); const log = new Stream.Consumer({ track: subscriber, compression: "deflate" }); -for (;;) { - const payload = await log.next(); - if (payload === undefined) break; +for await (const { value, at } of log) { + console.log(value.byteLength, at); } ``` Compression is opt-in per track (`compression: "none" | "deflate"`, default `"none"`); a consumer must set the same value as the producer. -Producers take `{ value, at }`, where `at` is the capture time written as the frame timestamp. Nothing fills in now: a timed track needs `at` on every write, and an untimed track takes none. +Producers take `{ value, at }`, where `at` is the capture time written as the frame timestamp, and consumers return the same shape. Nothing fills in now: a timed track needs `at` on every write, and an untimed track takes none and reads back without one. ## Codec diff --git a/js/flate/src/index.ts b/js/flate/src/index.ts index 057ebe02ff6..41b5223e177 100644 --- a/js/flate/src/index.ts +++ b/js/flate/src/index.ts @@ -1,8 +1,8 @@ /** * Opaque byte tracks over MoQ, optionally compressed with group-scoped DEFLATE, in two modes: * - * - {@link Snapshot}: **lossy**. One value updated over time; a consumer only gets the most recent - * one. Older values are superseded and dropped. + * - {@link Snapshot}: **lossy**. One value updated over time; a newer value supersedes the older + * ones, and a consumer can read every value it receives or skip to the most recent. * - {@link Stream}: **lossless**. An ordered append-log of self-contained payloads, delivered in * order with nothing superseded. Bounded by the group budget: see {@link Stream} for what happens * once it is spent. diff --git a/js/flate/src/snapshot/consumer.ts b/js/flate/src/snapshot/consumer.ts index 9aa68a34295..cfa31bf7af6 100644 --- a/js/flate/src/snapshot/consumer.ts +++ b/js/flate/src/snapshot/consumer.ts @@ -1,3 +1,4 @@ +import type { Timed } from "@moq/net"; import * as Moq from "@moq/net"; import { Decoder as Flate } from "../codec.ts"; @@ -5,11 +6,12 @@ import { isDeflate } from "../compression.ts"; import type { Config as CodecConfig } from "./producer.ts"; /** - * Consumes an opaque value from a track, yielding the newest one. + * Consumes an opaque value from a track, yielding each with the timestamp of its frame. * - * Jumps to the newest group and reads the value out of it, so a late joiner starts at the current - * value rather than replaying superseded ones. Interoperable with the Rust - * `moq_flate::snapshot::Consumer`, which collapses the same backlog. + * Two reads: {@link next} yields every value in group order, for a caller that picks the value + * matching a playhead, and {@link latest} jumps to the newest group, for a caller that only wants + * the current value. What {@link latest} skips is gone, so a later {@link next} resumes after + * it. Interoperable with the Rust `moq_flate::snapshot::Consumer`. */ export class Consumer { #track: Moq.Track.Ordered; @@ -27,20 +29,37 @@ export class Consumer { } /** - * Get the next value, or `undefined` once the track ends. + * Get the next value in group order, or `undefined` once the track ends. + * + * Buffers every value the reader has not reached yet. On a timed track, a group the + * subscription's `maxDelay` proves too old is skipped: the default of zero keeps only the + * newest group, so a playhead reader sets `maxDelay` to how far it trails the live edge. An + * untimed track never proves a group stale, so a reader that falls behind replays the whole + * backlog; use {@link latest} to skip it. A group lost to a gap resyncs from the next one. + */ + next(): Promise | undefined> { + return this.#read(false); + } + + /** + * Get the newest value, or `undefined` once the track ends. * * Every group is a complete value, so any older group is already superseded. Raising the read * floor to the newest sequence before each read does two things: it discards a backlog instead * of decoding every superseded value in turn, and it abandons a group a newer one has - * superseded rather than waiting out its close. A snapshot reader's latency therefore never - * grows with the queue, and never depends on a stale group's FIN arriving. - * - * This consumer owns its subscriber's read cursor, which is what lets it discard the backlog. + * superseded rather than waiting out its close. A reader's latency therefore never grows with + * the queue, and never depends on a stale group's FIN arriving. */ - async next(): Promise { + latest(): Promise | undefined> { + return this.#read(true); + } + + async #read(skip: boolean): Promise | undefined> { for (;;) { - const latest = this.#track.latest(); - if (latest !== undefined) this.#track.setGroups({ start: { included: latest } }); + if (skip) { + const latest = this.#track.latest(); + if (latest !== undefined) this.#track.setGroups({ start: { included: latest } }); + } let next: Awaited>; try { @@ -63,12 +82,12 @@ export class Consumer { this.#flate = this.#decompress ? new Flate() : undefined; } - return this.#flate ? this.#flate.frame(next.payload) : next.payload; + return { value: this.#flate ? this.#flate.frame(next.payload) : next.payload, at: next.timestamp }; } } - /** Iterate over values until the track ends, each the newest at the time it is yielded. */ - async *[Symbol.asyncIterator](): AsyncIterator { + /** Iterate over every value in group order, as {@link next} yields them, until the track ends. */ + async *[Symbol.asyncIterator](): AsyncIterator> { for (;;) { const value = await this.next(); if (value === undefined) return; diff --git a/js/flate/src/snapshot/index.ts b/js/flate/src/snapshot/index.ts index 027e96f8112..fc99c3b5fed 100644 --- a/js/flate/src/snapshot/index.ts +++ b/js/flate/src/snapshot/index.ts @@ -1,11 +1,12 @@ /** * Lossy latest-value opaque publishing over MoQ tracks. * - * One opaque value updated over time, for consumers that only care about the current state (a - * poster image, a serialized state blob). This mode is **lossy** by design: a consumer yields only - * the most recent value. A late joiner (or a consumer that falls behind) jumps straight to the - * newest group, and older groups are dropped entirely. For an ordered log where every payload is - * preserved, use the `Stream` module instead. + * One opaque value updated over time, for consumers that care about the state rather than every + * payload (a poster image, a serialized state blob). This mode is **lossy** by design: a new group + * supersedes the older ones, which are dropped. {@link Consumer.next} yields every value it still + * receives, in group order, each with its frame's timestamp, and {@link Consumer.latest} jumps + * straight to the newest group. For an ordered log where every payload is preserved, use the + * `Stream` module instead. * * On the wire each value is one group holding one frame, so a group is self-contained and a * consumer never needs an older one. With {@link Config.compression} `"deflate"`, that frame is its diff --git a/js/flate/src/snapshot/snapshot.test.ts b/js/flate/src/snapshot/snapshot.test.ts index 2969cdf18dd..c68b03116e2 100644 --- a/js/flate/src/snapshot/snapshot.test.ts +++ b/js/flate/src/snapshot/snapshot.test.ts @@ -8,15 +8,19 @@ const bytes = (...values: number[]) => new Uint8Array(values); // instead of the transport's live-edge default, which skips every superseded group. const REPLAY_LATENCY = Time.Milli(30_000); -// Drain every value currently available from a fresh consumer over the (finished) track. +// Drain every value `next()` yields from a fresh consumer over the (finished) track. async function drain(track: Track.Subscriber, compression: boolean): Promise { const consumer = new Consumer({ track, compression: compression ? "deflate" : "none" }); const out: Uint8Array[] = []; - for (;;) { - const value = await consumer.next(); - if (value === undefined) break; - out.push(value); - } + for await (const { value } of consumer) out.push(value); + return out; +} + +// Drain every value `latest()` yields from a fresh consumer over the (finished) track. +async function drainLatest(track: Track.Subscriber): Promise { + const consumer = new Consumer({ track }); + const out: Uint8Array[] = []; + for (let next = await consumer.latest(); next; next = await consumer.latest()) out.push(next.value); return out; } @@ -27,9 +31,10 @@ test("one single-frame group per update", async () => { producer.update({ value: bytes(2) }); producer.finish(); - // Two updates => two self-contained groups, so a consumer never needs an older one: a reader - // that arrives after both discards the superseded first group instead of replaying it. - expect(await drain(track.subscribe(), false)).toEqual([bytes(2)]); + // Two updates => two self-contained groups, so a consumer never needs an older one: `next()` reads + // both in order, while `latest()` discards the superseded first group instead of replaying it. + expect(await drain(track.subscribe({ maxDelay: REPLAY_LATENCY }), false)).toEqual([bytes(1), bytes(2)]); + expect(await drainLatest(track.subscribe({ maxDelay: REPLAY_LATENCY }))).toEqual([bytes(2)]); const subscriber = track.subscribe({ maxDelay: REPLAY_LATENCY }).ordered(); const counts: number[] = []; @@ -50,7 +55,7 @@ test("a live consumer sees each update", async () => { for (let n = 0; n < 3; n++) { producer.update({ value: bytes(n) }); - expect(await consumer.next()).toEqual(bytes(n)); + expect((await consumer.next())?.value).toEqual(bytes(n)); } producer.finish(); }); @@ -86,13 +91,13 @@ test("a finished track ends the consumer", async () => { producer.finish(); const consumer = new Consumer({ track: track.subscribe() }); - expect(await consumer.next()).toEqual(bytes(7)); + expect((await consumer.next())?.value).toEqual(bytes(7)); expect(await consumer.next()).toBeUndefined(); }); -test("a backlog collapses to the newest value", async () => { - // A consumer that fell behind (or joined late) must not replay every superseded value: its - // latency would grow with the backlog, and each older group is already superseded by design. +test("latest collapses a backlog to the newest value", async () => { + // A reader that fell behind (or joined late) and only wants the current value must not replay + // every superseded one: its latency would grow with the backlog. const track = new Track.Producer("test"); const producer = new Producer({ track }); const consumer = new Consumer({ track: track.subscribe() }); @@ -100,27 +105,40 @@ test("a backlog collapses to the newest value", async () => { for (let n = 0; n < 10; n++) producer.update({ value: bytes(n) }); producer.finish(); - expect(await consumer.next()).toEqual(bytes(9)); + expect((await consumer.latest())?.value).toEqual(bytes(9)); + expect(await consumer.latest()).toBeUndefined(); +}); + +test("next reads a backlog in group order", async () => { + // A reader syncing to a playhead needs every value it can still get, not just the head. + const track = new Track.Producer("test"); + const producer = new Producer({ track }); + const consumer = new Consumer({ track: track.subscribe({ maxDelay: REPLAY_LATENCY }) }); + + for (let n = 0; n < 10; n++) producer.update({ value: bytes(n) }); + producer.finish(); + + for (let n = 0; n < 10; n++) expect((await consumer.next())?.value).toEqual(bytes(n)); expect(await consumer.next()).toBeUndefined(); }); -test("a newer group preempts an open one", async () => { +test("latest lets a newer group preempt an open one", async () => { // A group whose close is delayed must not park the reader while a newer value is available. - // Snapshot mode exists to deliver the current value, not to wait out a stale group's FIN, and + // `latest()` exists to deliver the current value, not to wait out a stale group's FIN, and // groups ride independent QUIC streams so a newer one can land first. const track = new Track.Producer("test"); const consumer = new Consumer({ track: track.subscribe() }); const stale = track.appendGroup(); stale.writeFrame({ payload: bytes(1), timestamp: Time.Timestamp.now() }); - expect(await consumer.next()).toEqual(bytes(1)); + expect((await consumer.latest())?.value).toEqual(bytes(1)); // A complete newer value arrives while the previous group is still open. const fresh = track.appendGroup(); fresh.writeFrame({ payload: bytes(2), timestamp: Time.Timestamp.now() }); fresh.close(); - expect(await consumer.next()).toEqual(bytes(2)); + expect((await consumer.latest())?.value).toEqual(bytes(2)); stale.close(); track.close(); @@ -159,3 +177,33 @@ test("a capture timestamp is written as the frame timestamp", async () => { const frame = await (await track.subscribe().ordered().nextGroup())?.readFrame(); expect(frame?.timestamp?.as(Time.Timescale.MILLI)).toBe(1_234); }); + +test("an untimed value reads back untimed", async () => { + const track = new Track.Producer("test").accept({}); + const producer = new Producer({ track }); + producer.update({ value: bytes(1) }); + producer.finish(); + + expect(await new Consumer({ track: track.subscribe() }).next()).toEqual({ value: bytes(1), at: undefined }); +}); + +test("each value reads back with its frame's timestamp", async () => { + const track = new Track.Producer("test").accept({ timescale: Time.Timescale.MILLI }); + const producer = new Producer({ track, compression: "deflate" }); + for (let n = 1; n <= 3; n++) producer.update({ value: bytes(n), at: Time.Timestamp.fromMillis(n * 1_000) }); + producer.finish(); + + const subscription = { maxDelay: REPLAY_LATENCY }; + const consumer = new Consumer({ track: track.subscribe(subscription), compression: "deflate" }); + const values: [number, number | undefined][] = []; + for await (const { value, at } of consumer) values.push([value[0], at?.as(Time.Timescale.MILLI)]); + expect(values).toEqual([ + [1, 1_000], + [2, 2_000], + [3, 3_000], + ]); + + const head = await new Consumer({ track: track.subscribe(subscription), compression: "deflate" }).latest(); + expect(head?.value).toEqual(bytes(3)); + expect(head?.at?.as(Time.Timescale.MILLI)).toBe(3_000); +}); diff --git a/js/flate/src/stream/consumer.ts b/js/flate/src/stream/consumer.ts index 0a313a3efa5..ef878d8d2ac 100644 --- a/js/flate/src/stream/consumer.ts +++ b/js/flate/src/stream/consumer.ts @@ -1,4 +1,5 @@ import type * as Moq from "@moq/net"; +import type { Timed } from "@moq/net"; import { race } from "@moq/signals"; import { Decoder as Flate } from "../codec.ts"; @@ -22,7 +23,8 @@ export class Rolled extends Error { } /** - * Consumes an ordered log of opaque payloads from a track, yielding every one in order. + * Consumes an ordered log of opaque payloads from a track, yielding every one in order with its + * frame's timestamp. * * The log is a single group. That is what makes the mode lossless: rolling to a second group means * the payloads that would have completed the first are gone, so a publisher that cannot write ends @@ -55,8 +57,8 @@ export class Consumer { this.#decompress = isDeflate(config.compression); } - /** Get the next payload, or `undefined` once the track ends. */ - next(): Promise { + /** Get the next payload with its frame's timestamp, or `undefined` once the track ends. */ + next(): Promise | undefined> { // One reader at a time. Two concurrent calls await the same `recvGroup`, so the second would // take the first's group for a rolled log and fail a perfectly good one. if (this.#reading) throw new Error("multiple calls to next not supported"); @@ -66,7 +68,7 @@ export class Consumer { }); } - async #read(): Promise { + async #read(): Promise | undefined> { for (;;) { if (!this.#group) { const { group } = await this.#recvGroup(); @@ -84,7 +86,9 @@ export class Consumer { } const frame = await this.#readFrame(this.#group); - if (frame) return this.#flate ? this.#flate.frame(frame.payload) : frame.payload; + if (frame) { + return { value: this.#flate ? this.#flate.frame(frame.payload) : frame.payload, at: frame.timestamp }; + } // The log's one group is exhausted. Keep reading the track so a clean end still // reports the log as complete, and so a second group is caught as Rolled. @@ -132,11 +136,11 @@ export class Consumer { } /** Iterate over every payload in order, until the track ends. */ - async *[Symbol.asyncIterator](): AsyncIterator { + async *[Symbol.asyncIterator](): AsyncIterator> { for (;;) { - const value = await this.next(); - if (value === undefined) return; - yield value; + const payload = await this.next(); + if (payload === undefined) return; + yield payload; } } } diff --git a/js/flate/src/stream/stream.test.ts b/js/flate/src/stream/stream.test.ts index 6232dbcaed7..88edd20419a 100644 --- a/js/flate/src/stream/stream.test.ts +++ b/js/flate/src/stream/stream.test.ts @@ -13,11 +13,7 @@ const payloads = (count: number) => Array.from({ length: count }, (_, n) => new async function drain(track: Track.Subscriber, compression: boolean): Promise { const consumer = new Consumer({ track, compression: compression ? "deflate" : "none" }); const out: Uint8Array[] = []; - for (;;) { - const value = await consumer.next(); - if (value === undefined) break; - out.push(value); - } + for await (const { value } of consumer) out.push(value); return out; } @@ -100,7 +96,7 @@ test("a second concurrent read is refused rather than served the first one's gro const consumer = new Consumer({ track: track.subscribe() }); const first = consumer.next(); expect(() => consumer.next()).toThrow("multiple calls to next not supported"); - expect(await first).toEqual(payloads(1)[0]); + expect((await first)?.value).toEqual(payloads(1)[0]); }); test("a second group is reported while the first is still open", async () => { @@ -116,7 +112,7 @@ test("a second group is reported while the first is still open", async () => { second.writeFrame({ payload: payloads(2)[1], timestamp: Time.Timestamp.now() }); const consumer = new Consumer({ track: track.subscribe({ maxDelay: REPLAY_LATENCY }) }); - expect(await consumer.next()).toEqual(payloads(1)[0]); + expect((await consumer.next())?.value).toEqual(payloads(1)[0]); await expect(consumer.next()).rejects.toThrow(Rolled); // Both mirrors are released. The read that lost the race would otherwise stay registered on the @@ -262,7 +258,7 @@ test("blocked reads leave nothing behind on the pending group read", async () => for (let n = 0; n < 1000; n++) { const next = consumer.next(); producer.append({ value: new Uint8Array([n & 0xff]) }); - expect((await next)?.[0]).toBe(n & 0xff); + expect((await next)?.value[0]).toBe(n & 0xff); } }); expect(reactions).toBeLessThan(10); @@ -271,24 +267,29 @@ test("blocked reads leave nothing behind on the pending group read", async () => producer.finish(); }); -test("a payload without a timestamp goes out untimed", async () => { +test("an untimed payload reads back untimed", async () => { const track = new Track.Producer("test").accept({}); const producer = new Producer({ track }); producer.append({ value: new Uint8Array([1]) }); producer.finish(); - const group = await track.subscribe().ordered().nextGroup(); - expect((await group?.readFrame())?.timestamp).toBeUndefined(); + const consumer = new Consumer({ track: track.subscribe() }); + expect(await consumer.next()).toEqual({ value: new Uint8Array([1]), at: undefined }); + expect(await consumer.next()).toBeUndefined(); }); -test("each record keeps its capture timestamp", async () => { - const track = new Track.Producer("test"); - const producer = new Producer({ track }); +test("each payload reads back with its capture timestamp", async () => { + const track = new Track.Producer("test").accept({ timescale: Time.Timescale.MILLI }); + const producer = new Producer({ track, compression: "deflate" }); producer.append({ value: new Uint8Array([1]), at: Time.Timestamp.fromMillis(1_000) }); producer.append({ value: new Uint8Array([2]), at: Time.Timestamp.fromMillis(2_000) }); producer.finish(); - const group = await track.subscribe().ordered().nextGroup(); - expect((await group?.readFrame())?.timestamp?.as(Time.Timescale.MILLI)).toBe(1_000); - expect((await group?.readFrame())?.timestamp?.as(Time.Timescale.MILLI)).toBe(2_000); + const consumer = new Consumer({ track: track.subscribe(), compression: "deflate" }); + const read: [number, number | undefined][] = []; + for await (const { value, at } of consumer) read.push([value[0], at?.as(Time.Timescale.MILLI)]); + expect(read).toEqual([ + [1, 1_000], + [2, 2_000], + ]); }); diff --git a/js/hang/src/catalog/root.ts b/js/hang/src/catalog/root.ts index 6c52a0a833f..67f955caa5e 100644 --- a/js/hang/src/catalog/root.ts +++ b/js/hang/src/catalog/root.ts @@ -114,7 +114,11 @@ export async function* watch(broadcast: Moq.Broadcast.Consumer): AsyncIterable({ track, schema: RootSchema }); - for await (const root of consumer) yield checkResolvable(checkRenditions(root), broadcast.path); + for (;;) { + const root = await consumer.latest(); + if (!root) return; + yield checkResolvable(checkRenditions(root.value), broadcast.path); + } } finally { track.close(); } diff --git a/js/json/README.md b/js/json/README.md index 6db2b4de9da..173950b2dfc 100644 --- a/js/json/README.md +++ b/js/json/README.md @@ -9,7 +9,7 @@ JSON publishing over [Media over QUIC](https://moq.dev/) tracks, in two modes: -- **`Snapshot`**: lossy. One JSON value updated over time; a consumer only gets the most recent value. Intermediate updates are collapsed and older groups are dropped. +- **`Snapshot`**: lossy. One JSON value updated over time; a newer group supersedes the older ones, and a consumer reads every state it receives or skips to the most recent. - **`Stream`**: lossless. An ordered append-log of self-contained records; every record is preserved and delivered in order, nothing is ever superseded. Pick `Snapshot` when consumers care about "what is the value now" (a catalog, a status document) and `Stream` when they care about every record (an event log, a media timeline). @@ -22,7 +22,7 @@ bun add @moq/json ### Snapshot: the latest value -A JSON value is published over a [`@moq/net`](../net) track as a series of groups. Each group is self-contained: its first frame is a full snapshot and any following frames are [RFC 7396 JSON Merge Patch](https://www.rfc-editor.org/rfc/rfc7396.html) deltas applied in order. A consumer jumps to the newest group, reads the snapshot, and applies the deltas, so a late joiner never needs older groups. This is lossy by design: only the most recent value is delivered. +A JSON value is published over a [`@moq/net`](../net) track as a series of groups. Each group is self-contained: its first frame is a full snapshot and any following frames are [RFC 7396 JSON Merge Patch](https://www.rfc-editor.org/rfc/rfc7396.html) deltas applied in order. A late joiner never needs older groups: it jumps to the newest one, reads the snapshot, and applies the deltas. This is lossy by design: superseded groups are dropped. ```ts import { Snapshot } from "@moq/json"; @@ -32,11 +32,14 @@ import { Time } from "@moq/net"; const producer = new Snapshot.Producer({ track }); producer.update({ value: { hello: "world" }, at: Time.Timestamp.now() }); -// Consume: yields the latest reconstructed value, collapsing any backlog. +// Consume: each state in order, with the timestamp of the frame that produced it. const consumer = new Snapshot.Consumer({ track: track.subscribe() }); -for await (const value of consumer) { - console.log(value); +for await (const { value, at } of consumer) { + console.log(value, at); } + +// Or skip to the newest state, collapsing any backlog. +const current = await consumer.latest(); ``` Pass `{ deltaRatio: x }` with a positive `x` to `Snapshot.Producer` to emit merge-patch deltas. A new snapshot group rolls once the deltas *already written* to the group exceed `x` times the fresh snapshot size; the delta that crosses the budget still lands first, so a group overshoots by at most one delta. `deltaRatio` defaults to `8` when unset. Set it to `0` to disable deltas: every change becomes a fresh single-frame snapshot group. Arrays are replaced wholesale within a delta; a value set to `null` falls back to a snapshot, since merge patch reads `null` as a key deletion. @@ -54,11 +57,11 @@ producer.append({ value: { event: "started" }, at: Time.Timestamp.now() }); producer.append({ value: { event: "stopped" }, at: Time.Timestamp.now() }); const consumer = new Stream.Consumer({ track: track.subscribe() }); -for await (const record of consumer) { - console.log(record); +for await (const { value, at } of consumer) { + console.log(value, at); } ``` -Producers take `{ value, at }`, where `at` is the capture time written as the frame timestamp. Nothing fills in now: a timed track needs `at` on every write, and an untimed track takes none. +Producers take `{ value, at }`, where `at` is the capture time written as the frame timestamp, and consumers return the same shape. Nothing fills in now: a timed track needs `at` on every write, and an untimed track takes none and reads back without one. Both modes support optional group-scoped DEFLATE compression (`{ compression: "deflate" }` on both sides), interoperable with the Rust `moq-json` crate. `Config` is the codec options; `Producer.Config` / `Consumer.Config` add the track. diff --git a/js/json/src/index.ts b/js/json/src/index.ts index 9b748eca7f2..6fe7d78279a 100644 --- a/js/json/src/index.ts +++ b/js/json/src/index.ts @@ -1,8 +1,8 @@ /** * JSON publishing over MoQ tracks, in three modes: * - * - {@link Snapshot}: **lossy**. One JSON value updated over time; a consumer only gets the most - * recent value. Intermediate updates are collapsed and older groups are dropped. + * - {@link Snapshot}: **lossy**. One JSON value updated over time; a newer group supersedes the + * older ones, and a consumer can read every state it receives or skip to the most recent. * - {@link Stream}: **lossless**. An ordered append-log of self-contained records; every record * is preserved and delivered in order, nothing is ever superseded. * - {@link Window}: a **bounded** run of records, appended to the back and dropped from the front, diff --git a/js/json/src/snapshot/compression.test.ts b/js/json/src/snapshot/compression.test.ts index 63f53c22344..456d01d3a7e 100644 --- a/js/json/src/snapshot/compression.test.ts +++ b/js/json/src/snapshot/compression.test.ts @@ -13,7 +13,7 @@ const REPLAY_LATENCY = Time.Milli(30_000); // Reconstruct every value a compressed consumer yields, in order. async function drainCompressed(track: Track.Subscriber): Promise { const out: Value[] = []; - for await (const value of new Consumer({ track, compression: "deflate" })) out.push(value); + for await (const { value } of new Consumer({ track, compression: "deflate" })) out.push(value); return out; } @@ -44,9 +44,8 @@ test("compressed snapshot per group round-trips", async () => { producer.update({ value: { a: 2 } }); producer.finish(); - // Deltas off: one compressed snapshot per group. A consumer joining after the fact - // collapses the backlog to the newest value (mirrors the Rust consumer). - expect(await drainCompressed(track.subscribe({ maxDelay: REPLAY_LATENCY }))).toEqual([{ a: 2 }]); + // Deltas off: one compressed snapshot per group, each its own cold window. + expect(await drainCompressed(track.subscribe({ maxDelay: REPLAY_LATENCY }))).toEqual([{ a: 1 }, { a: 2 }]); }); test("compressed live consumer sees each update in order", async () => { @@ -57,7 +56,7 @@ test("compressed live consumer sees each update in order", async () => { for (let n = 1; n <= 5; n++) { producer.update({ value: { a: n } }); - expect(await consumer.next()).toEqual({ a: n }); + expect((await consumer.next())?.value).toEqual({ a: n }); } }); diff --git a/js/json/src/snapshot/consumer.ts b/js/json/src/snapshot/consumer.ts index 65a254bd300..5621f74a1a4 100644 --- a/js/json/src/snapshot/consumer.ts +++ b/js/json/src/snapshot/consumer.ts @@ -1,3 +1,4 @@ +import type { Timed } from "@moq/net"; import * as Moq from "@moq/net"; import { Decoder } from "./decoder.ts"; @@ -14,10 +15,11 @@ const GAPS: Moq.StreamCode[] = [ * Consumes a JSON value from a track, reconstructing it from snapshots and deltas. * * A {@link Decoder} that owns its track: it reads groups, routes each frame by its position, and - * yields the reconstructed value. A live consumer yields each update as it arrives; a consumer that - * has fallen behind (or just joined) collapses the buffered backlog and yields only the latest - * value. See {@link next}. When something else already owns the track, use the {@link Decoder} - * directly. + * yields each reconstructed value with the timestamp of the frame that produced it. Two reads: + * {@link next} yields every state in order, for a caller that picks the state matching a playhead, + * and {@link latest} skips to the newest state, for a caller that only wants the current value. + * What {@link latest} skips is gone, so a later {@link next} resumes after it. + * When something else already owns the track, use the {@link Decoder} directly. */ export class Consumer { #track: Moq.Track.Ordered; @@ -32,32 +34,48 @@ export class Consumer { } /** - * Get the next reconstructed value, or `undefined` once the track ends. + * Get the next state in order, or `undefined` once the track ends. * - * Applies every frame already buffered in the group but yields only the latest reconstructed - * value: the intermediate reconstructions are stale, so a late joiner (or any consumer that has - * fallen behind) catches up to the head in one step instead of replaying every superseded state. - * Frames are still decoded in order (the DEFLATE window and merge patches are sequential); only - * the per-frame yield is skipped. + * Yields one state per frame and finishes a group before starting the next, buffering every + * state the reader has not reached yet. On a timed track, a group the subscription's `maxDelay` + * proves too old is skipped: the default of zero keeps only the newest group, so a playhead + * reader sets `maxDelay` to how far it trails the live edge. An untimed track never proves a + * group stale, so a reader that falls behind replays the whole backlog; use {@link latest} to + * skip it. A group lost to a gap resyncs from the next one. */ - async next(): Promise { + next(): Promise | undefined> { + return this.#read(false); + } + + /** + * Get the newest state, or `undefined` once the track ends. + * + * Skips to the newest group and applies every frame already buffered in it, yielding only the + * last: a late joiner (or any consumer that has fallen behind) catches up to the head in one + * step instead of replaying every superseded state. Frames are still decoded in order (the + * DEFLATE window and merge patches are sequential); only the per-frame yield is skipped. Blocks + * when nothing newer than the last yield has arrived. + */ + latest(): Promise | undefined> { + return this.#read(true); + } + + async #read(skip: boolean): Promise | undefined> { for (;;) { - // A newer group supersedes everything the current one still holds: it restarts - // from a full snapshot, so switching mid-group loses nothing but stale state - // (mirrors the Rust consumer, which drains to the newest group every poll). - if (this.#group !== undefined) { + if (skip) { + // A newer group supersedes everything the current one still holds: it restarts from a + // full snapshot, so switching mid-group loses nothing but stale state (mirrors the Rust + // consumer). Older groups only hold superseded state too, so raise the floor to the + // newest instead of replaying each one. const latest = this.#track.latest(); - if (latest !== undefined && latest > this.#group.sequence) { + if (this.#group !== undefined && latest !== undefined && latest > this.#group.sequence) { this.#group.close(); this.#group = undefined; } + if (!this.#group && latest !== undefined) this.#track.setGroups({ start: { included: latest } }); } if (!this.#group) { - // Collapse the retained backlog: older groups only hold superseded state, so - // jump the cursor to the newest buffered group instead of replaying each one. - const latest = this.#track.latest(); - if (latest !== undefined) this.#track.setGroups({ start: { included: latest } }); // Advance to the next group with a higher sequence number (skipping late arrivals). this.#group = await this.#track.nextGroup(); if (!this.#group) return undefined; @@ -65,44 +83,43 @@ export class Consumer { this.#framesRead = 0; } - // Drain every frame already buffered, keeping only the latest reconstructed value: a late - // joiner (or any consumer that fell behind) catches up to the head in one step. - let advanced = false; - for (let frame = this.#group.tryReadFrame(); frame !== undefined; frame = this.#group.tryReadFrame()) { - this.#apply(frame.payload); - advanced = true; - } - if (advanced) return this.#decoder.decode(); - - // Nothing buffered: block for the next frame (or the group's end). let frame: Moq.Group.Frame | undefined; try { frame = await this.#group.readFrame(); } catch (err) { if (!(err instanceof Moq.Error.Stream && GAPS.includes(err.code))) throw err; - // The group was reset or we fell behind its eviction window. Resync from - // the next group, which begins with a fresh snapshot (frame 0), so no - // partial state is presented. + // The group was reset or we fell behind its eviction window. Resync from the next + // group, which begins with a fresh snapshot (frame 0), so no partial state is presented. this.#group = undefined; continue; } - if (frame === undefined) { + if (!frame) { // The group is exhausted; advance to the next one. this.#group = undefined; continue; } this.#apply(frame.payload); - return this.#decoder.decode(); + if (skip) { + // Apply the rest of the backlog but yield only the head. + for (let more = this.#group.tryReadFrame(); more; more = this.#group.tryReadFrame()) { + this.#apply(more.payload); + frame = more; + } + } + + // Every group starts with a snapshot, so a frame has been applied to one by now. + return { value: this.#decoder.decode() as T, at: frame.timestamp }; } } - async *[Symbol.asyncIterator](): AsyncIterator { + /** Iterate over every state in order, as {@link next} yields them, until the track ends. */ + async *[Symbol.asyncIterator](): AsyncIterator> { for (;;) { - const value = await this.next(); - if (value === undefined) return; - yield value; + const state = await this.next(); + if (state === undefined) return; + yield state; } } diff --git a/js/json/src/snapshot/index.ts b/js/json/src/snapshot/index.ts index 82625c5bdc9..a6b4d103fea 100644 --- a/js/json/src/snapshot/index.ts +++ b/js/json/src/snapshot/index.ts @@ -1,12 +1,13 @@ /** * Lossy latest-value JSON publishing over MoQ tracks. * - * One JSON value updated over time, for consumers that only care about the current state (a - * catalog, a status document). This mode is **lossy** by design: a consumer yields only the most - * recent value. A late joiner (or a consumer that falls behind) jumps straight to the newest - * group and collapses any buffered backlog into a single yield, and older groups are dropped - * entirely. Intermediate updates are never replayed. For an ordered log where every record is - * preserved, use the `Stream` module instead. + * One JSON value updated over time, for consumers that care about the state rather than every + * record (a catalog, a status document). This mode is **lossy** by design: a new group supersedes + * the older ones, which are dropped. {@link Consumer.next} yields every state it still receives, + * in order, each with its frame's timestamp, for a caller that picks the state at a playhead. + * {@link Consumer.latest} jumps straight to the newest group and collapses any buffered backlog + * into a single yield, for a caller that only wants the current value. For an ordered log where + * every record is preserved, use the `Stream` module instead. * * On the wire the value is a series of self-contained groups: frame 0 is a full snapshot and any * following frames are RFC 7396 JSON Merge Patch deltas applied in order. Interoperable with the diff --git a/js/json/src/snapshot/snapshot.test.ts b/js/json/src/snapshot/snapshot.test.ts index 1e8bf0aa5f7..b5a1cf21447 100644 --- a/js/json/src/snapshot/snapshot.test.ts +++ b/js/json/src/snapshot/snapshot.test.ts @@ -10,10 +10,18 @@ type Value = Record; // instead of the transport's live-edge default. const REPLAY_LATENCY = Time.Milli(30_000); -// Reconstruct every value a consumer yields, in order. +// Reconstruct every state a consumer yields from `next()`, in order. async function drain(track: Track.Subscriber): Promise { const out: Value[] = []; - for await (const value of new Consumer({ track })) out.push(value); + for await (const { value } of new Consumer({ track })) out.push(value); + return out; +} + +// Reconstruct every state a consumer yields from `latest()`, in order. +async function drainLatest(track: Track.Subscriber): Promise { + const consumer = new Consumer({ track }); + const out: Value[] = []; + for (let state = await consumer.latest(); state; state = await consumer.latest()) out.push(state.value); return out; } @@ -110,10 +118,11 @@ test("deltas off: a snapshot group per change", async () => { producer.update({ value: { a: 2 } }); producer.finish(); - // Two changes => two single-frame snapshot groups. A consumer joining after the fact - // collapses the backlog to the newest value: older groups only hold superseded state - // (mirrors the Rust consumer). The layout itself is asserted via structure() below. - expect(await drain(track.subscribe({ maxDelay: REPLAY_LATENCY }))).toEqual([{ a: 2 }]); + // Two changes => two single-frame snapshot groups. `next()` reads both in order, while `latest()` + // collapses the backlog to the newest value: older groups only hold superseded state (mirrors the + // Rust consumer). The layout itself is asserted via structure() below. + expect(await drain(track.subscribe({ maxDelay: REPLAY_LATENCY }))).toEqual([{ a: 1 }, { a: 2 }]); + expect(await drainLatest(track.subscribe({ maxDelay: REPLAY_LATENCY }))).toEqual([{ a: 2 }]); }); test("deltaRatio 0 disables deltas, like off", async () => { @@ -128,25 +137,58 @@ test("deltaRatio 0 disables deltas, like off", async () => { expect(await structure(track.subscribe({ maxDelay: REPLAY_LATENCY }).ordered())).toEqual([1, 1]); }); -// A consumer holding a group must jump when a newer snapshot group rolls: the +// `latest()` holding a group must jump when a newer snapshot group rolls: the // buffered deltas in the held group only reconstruct superseded state, and every // group restarts from a full snapshot (mirrors the Rust consumer, which drains to // the newest group on every poll). -test("a held group is abandoned when a newer snapshot group rolls", async () => { +test("latest abandons a held group when a newer snapshot group rolls", async () => { const track = new Track.Producer("test"); // A tiny ratio forces the update after any delta to roll a fresh snapshot group. const producer = new Producer({ track, deltaRatio: 0.001 }); const consumer = new Consumer({ track: track.subscribe({ maxDelay: REPLAY_LATENCY }) }); producer.update({ value: { a: 1 } }); - expect(await consumer.next()).toEqual({ a: 1 }); + expect((await consumer.latest())?.value).toEqual({ a: 1 }); // A delta lands in the held group, then the ratio rolls a new snapshot group. producer.update({ value: { a: 2 } }); producer.update({ value: { a: 3 } }); producer.finish(); - expect(await consumer.next()).toEqual({ a: 3 }); + expect((await consumer.latest())?.value).toEqual({ a: 3 }); + expect(await consumer.latest()).toBeUndefined(); +}); + +// `next()` finishes the held group before the newer one, so a playhead sees every state. +test("next reads a held group out before a newer snapshot group", async () => { + const track = new Track.Producer("test"); + const producer = new Producer({ track, deltaRatio: 0.001 }); + const consumer = new Consumer({ track: track.subscribe({ maxDelay: REPLAY_LATENCY }) }); + + producer.update({ value: { a: 1 } }); + expect((await consumer.next())?.value).toEqual({ a: 1 }); + + producer.update({ value: { a: 2 } }); + producer.update({ value: { a: 3 } }); + producer.finish(); + + expect((await consumer.next())?.value).toEqual({ a: 2 }); + expect((await consumer.next())?.value).toEqual({ a: 3 }); + expect(await consumer.next()).toBeUndefined(); +}); + +// Reading in order is still bounded: a group the subscription's max delay proves stale is skipped, +// as on any ordered track, so a reader that falls behind jumps ahead instead of replaying it. +test("next skips the groups the max delay abandons", async () => { + const track = new Track.Producer("test").accept({ timescale: Time.Timescale.MILLI }); + const producer = new Producer({ track, deltaRatio: 0 }); + for (let n = 1; n <= 3; n++) producer.update({ value: { n }, at: Time.Timestamp.fromMillis(n * 1_000) }); + producer.finish(); + + expect(await drain(track.subscribe({ maxDelay: REPLAY_LATENCY }))).toEqual([{ n: 1 }, { n: 2 }, { n: 3 }]); + expect(await drain(track.subscribe({ maxDelay: Time.Milli(500) }))).toEqual([{ n: 2 }, { n: 3 }]); + // The default of zero keeps only the newest group. + expect(await drain(track.subscribe())).toEqual([{ n: 3 }]); }); test("live consumer sees each update", async () => { @@ -156,7 +198,7 @@ test("live consumer sees each update", async () => { for (let n = 1; n <= 3; n++) { producer.update({ value: { a: n } }); - expect(await consumer.next()).toEqual({ a: n }); + expect((await consumer.next())?.value).toEqual({ a: n }); } }); @@ -204,13 +246,13 @@ test("mutate composes independent owners", async () => { producer.mutate((v) => { v.video = "v1"; }); - expect(await consumer.next()).toEqual({ video: "v1" }); + expect((await consumer.next())?.value).toEqual({ video: "v1" }); // A second owner starts from the latest value and adds its own key without clobbering the first. producer.mutate((v) => { v.scte35 = { id: 1 }; }); - expect(await consumer.next()).toEqual({ video: "v1", scte35: { id: 1 } }); + expect((await consumer.next())?.value).toEqual({ video: "v1", scte35: { id: 1 } }); }); test("mutate starts from the configured initial value", async () => { @@ -221,7 +263,7 @@ test("mutate starts from the configured initial value", async () => { producer.mutate((v) => { v.a = 1; }); - expect(await consumer.next()).toEqual({ a: 1 }); + expect((await consumer.next())?.value).toEqual({ a: 1 }); }); test("mutate without a prior value or initial throws", () => { @@ -240,12 +282,12 @@ test("mutate removes a section", async () => { v.a = 1; v.scte35 = { id: 1 }; }); - expect(await consumer.next()).toEqual({ a: 1, scte35: { id: 1 } }); + expect((await consumer.next())?.value).toEqual({ a: 1, scte35: { id: 1 } }); producer.mutate((v) => { delete v.scte35; }); - expect(await consumer.next()).toEqual({ a: 1 }); + expect((await consumer.next())?.value).toEqual({ a: 1 }); }); test("tight ratio rolls snapshots", async () => { @@ -288,18 +330,20 @@ test("array change is a wholesale delta", async () => { expect(await structure(track.subscribe().ordered())).toEqual([2]); }); -test("late joiner collapses a buffered backlog to the latest value", async () => { +test("a late joiner reads a buffered backlog state by state, or collapses it to the latest", async () => { const track = new Track.Producer("test"); const producer = new Producer({ track, deltaRatio: 100 }); const subscriber = track.subscribe(); + const collapsed = track.subscribe(); for (let n = 0; n <= 20; n++) { producer.update({ value: { n } }); } producer.finish(); - // A whole group's worth of snapshot + deltas is buffered before the consumer reads, so it applies - // them all but yields only the latest value once, not every superseded state. - expect(await drain(subscriber)).toEqual([{ n: 20 }]); + // A whole group's worth of snapshot + deltas is buffered before the consumer reads. `next()` + // yields every state in it; `latest()` applies them all but yields only the head, once. + expect(await drain(subscriber)).toEqual(Array.from({ length: 21 }, (_, n) => ({ n }))); + expect(await drainLatest(collapsed)).toEqual([{ n: 20 }]); }); test("frame cap rolls snapshot", async () => { @@ -394,7 +438,7 @@ test("a compressed delta is gated on its encoded size, not its plaintext", async compression: "deflate", }); const values: Value[] = []; - for await (const out of consumer) values.push(out); + for await (const { value } of consumer) values.push(value); expect(values[values.length - 1]).toEqual(patched); }); @@ -404,7 +448,7 @@ test("snapshot consumer propagates a non-gap frame failure", async () => { const consumer = new Consumer({ track: track.subscribe() }); const group = track.appendGroup(); group.writeFrame({ payload: new TextEncoder().encode('{"ok":true}'), timestamp: Time.Timestamp.now() }); - expect(await consumer.next()).toEqual({ ok: true }); + expect((await consumer.next())?.value).toEqual({ ok: true }); group.close(new NetError.Stream(StreamCode.Internal)); await expect(consumer.next()).rejects.toMatchObject({ code: StreamCode.Internal }); }); @@ -434,3 +478,41 @@ test("a capture timestamp is written on snapshots and deltas alike", async () => expect((await group?.readFrame())?.timestamp?.as(Time.Timescale.MILLI)).toBe(1_000); expect((await group?.readFrame())?.timestamp?.as(Time.Timescale.MILLI)).toBe(2_000); }); + +test("an untimed state reads back untimed", async () => { + const track = new Track.Producer("test").accept({}); + const producer = new Producer({ track, deltaRatio: 100 }); + producer.update({ value: { a: 1, b: "x".repeat(64) } }); + producer.update({ value: { a: 2, b: "x".repeat(64) } }); + producer.finish(); + + const consumer = new Consumer({ track: track.subscribe() }); + expect(await consumer.next()).toEqual({ value: { a: 1, b: "x".repeat(64) }, at: undefined }); + expect(await consumer.next()).toEqual({ value: { a: 2, b: "x".repeat(64) }, at: undefined }); +}); + +// Each state carries the timestamp of the frame that produced it, snapshot or delta, so a caller can +// hold it back until a playhead reaches it. `latest()` reports the head's own frame. +test("each state reads back with its frame's timestamp", async () => { + const track = new Track.Producer("test").accept({ timescale: Time.Timescale.MILLI }); + const producer = new Producer({ track, deltaRatio: 100, compression: "deflate" }); + for (let n = 1; n <= 3; n++) { + producer.update({ value: { n, b: "x".repeat(64) }, at: Time.Timestamp.fromMillis(n * 1_000) }); + } + producer.finish(); + + const subscription = { maxDelay: REPLAY_LATENCY }; + const consumer = new Consumer({ track: track.subscribe(subscription), compression: "deflate" }); + const states: [unknown, number | undefined][] = []; + for await (const { value, at } of consumer) states.push([value.n, at?.as(Time.Timescale.MILLI)]); + expect(states).toEqual([ + [1, 1_000], + [2, 2_000], + [3, 3_000], + ]); + + const latest = new Consumer({ track: track.subscribe(subscription), compression: "deflate" }); + const head = await latest.latest(); + expect(head?.value.n).toBe(3); + expect(head?.at?.as(Time.Timescale.MILLI)).toBe(3_000); +}); diff --git a/js/json/src/stream/consumer.ts b/js/json/src/stream/consumer.ts index 6ca14024bec..3b31a1eddeb 100644 --- a/js/json/src/stream/consumer.ts +++ b/js/json/src/stream/consumer.ts @@ -1,4 +1,5 @@ import type * as Moq from "@moq/net"; +import type { Timed } from "@moq/net"; import { race } from "@moq/signals"; import { Decoder } from "./decoder.ts"; import type { Config as CodecConfig } from "./encoder.ts"; @@ -20,7 +21,8 @@ export class Rolled extends Error { } /** - * Consumes an ordered log of JSON records from a track, yielding every record in order. + * Consumes an ordered log of JSON records from a track, yielding every record in order with its + * frame's timestamp. * * A {@link Decoder} that owns its track, reading one record per frame. The log is a single group, * which is what makes the mode lossless: rolling to a second group means the records that would @@ -53,8 +55,8 @@ export class Consumer { this.#decoder = new Decoder(config); } - /** Get the next record, or `undefined` once the track ends. */ - next(): Promise { + /** Get the next record with its frame's timestamp, or `undefined` once the track ends. */ + next(): Promise | undefined> { // One reader at a time. Two concurrent calls await the same `recvGroup`, so the second would // take the first's group for a rolled log and fail a perfectly good one. if (this.#reading) throw new Error("multiple calls to next not supported"); @@ -64,7 +66,7 @@ export class Consumer { }); } - async #read(): Promise { + async #read(): Promise | undefined> { for (;;) { if (!this.#group) { const { group } = await this.#recvGroup(); @@ -83,7 +85,7 @@ export class Consumer { } const frame = await this.#readFrame(this.#group); - if (frame) return this.#decoder.decode(frame.payload); + if (frame) return { value: this.#decoder.decode(frame.payload), at: frame.timestamp }; // The log's one group is exhausted. Keep reading the track so a clean end still // reports the log as complete, and so a second group is caught as Rolled. @@ -130,11 +132,12 @@ export class Consumer { return this.#pending; } - async *[Symbol.asyncIterator](): AsyncIterator { + /** Iterate over every record in order, until the track ends. */ + async *[Symbol.asyncIterator](): AsyncIterator> { for (;;) { - const value = await this.next(); - if (value === undefined) return; - yield value; + const record = await this.next(); + if (record === undefined) return; + yield record; } } } diff --git a/js/json/src/stream/stream.test.ts b/js/json/src/stream/stream.test.ts index 6fd76eb5bfd..437ff44afe4 100644 --- a/js/json/src/stream/stream.test.ts +++ b/js/json/src/stream/stream.test.ts @@ -11,7 +11,7 @@ async function drain(track: Track.Subscriber, compression: boolean): Promise { // Ask for a replay window, so the first group is delivered rather than skipped by the // subscriber's default max delay budget once a newer group exists. const consumer = new Consumer({ track: track.subscribe({ maxDelay: Time.Milli(30_000) }) }); - expect(await consumer.next()).toEqual({ n: 0 }); + expect((await consumer.next())?.value).toEqual({ n: 0 }); await expect(consumer.next()).rejects.toThrow(Rolled); // Both mirrors are released. The read that lost the race would otherwise stay registered on the @@ -108,7 +108,7 @@ test("a second concurrent read is refused rather than served the first one's gro const consumer = new Consumer({ track: track.subscribe() }); const first = consumer.next(); expect(() => consumer.next()).toThrow("multiple calls to next not supported"); - expect(await first).toEqual({ n: 0 }); + expect((await first)?.value).toEqual({ n: 0 }); }); // Counts the reactions `run` attaches to promises still pending once it returns. A promise holds each @@ -141,7 +141,7 @@ test("blocked reads leave nothing behind on the pending group read", async () => for (let n = 0; n < 1000; n++) { const next = consumer.next(); producer.append({ value: { n } }); - expect((await next)?.n).toBe(n); + expect((await next)?.value.n).toBe(n); } }); expect(reactions).toBeLessThan(10); @@ -150,26 +150,31 @@ test("blocked reads leave nothing behind on the pending group read", async () => producer.finish(); }); -test("a record without a timestamp goes out untimed", async () => { +test("an untimed record reads back untimed", async () => { const track = new Track.Producer("test").accept({}); const producer = new Producer({ track }); producer.append({ value: 1 }); producer.finish(); - const group = await track.subscribe().ordered().nextGroup(); - expect((await group?.readFrame())?.timestamp).toBeUndefined(); + const consumer = new Consumer({ track: track.subscribe() }); + expect(await consumer.next()).toEqual({ value: 1, at: undefined }); + expect(await consumer.next()).toBeUndefined(); }); -test("each record keeps its capture timestamp", async () => { - const track = new Track.Producer("test"); - const producer = new Producer({ track }); +test("each record reads back with its capture timestamp", async () => { + const track = new Track.Producer("test").accept({ timescale: Time.Timescale.MILLI }); + const producer = new Producer({ track, compression: "deflate" }); producer.append({ value: 1, at: Time.Timestamp.fromMillis(1_000) }); producer.append({ value: 2, at: Time.Timestamp.fromMillis(2_000) }); producer.finish(); - const group = await track.subscribe().ordered().nextGroup(); - expect((await group?.readFrame())?.timestamp?.as(Time.Timescale.MILLI)).toBe(1_000); - expect((await group?.readFrame())?.timestamp?.as(Time.Timescale.MILLI)).toBe(2_000); + const consumer = new Consumer({ track: track.subscribe(), compression: "deflate" }); + const records: [number, number | undefined][] = []; + for await (const { value, at } of consumer) records.push([value, at?.as(Time.Timescale.MILLI)]); + expect(records).toEqual([ + [1, 1_000], + [2, 2_000], + ]); }); // A record past the group budget is refused before anything is written, so the log carries on: the diff --git a/js/moq-boy/src/game.ts b/js/moq-boy/src/game.ts index 87d82e4fb36..87895661a6c 100644 --- a/js/moq-boy/src/game.ts +++ b/js/moq-boy/src/game.ts @@ -262,13 +262,13 @@ export class Game { // Reconstruct each status from snapshots and deltas, validated against the schema. const consumer = new Json.Snapshot.Consumer({ track: statusTrack, schema: GameStatusSchema }); - // Closing the track on cleanup unblocks a pending next() (it returns undefined), so the loop + // Closing the track on cleanup unblocks a pending latest() (it returns undefined), so the loop // ends without racing the teardown. effect.spawn(async () => { for (;;) { let status: GameStatus | undefined; try { - status = await consumer.next(); + status = (await consumer.latest())?.value; } catch (err) { console.warn("Invalid status JSON:", err); continue; diff --git a/js/publish/bench/catalog.ts b/js/publish/bench/catalog.ts index 616719f9a59..8dffeccabd0 100644 --- a/js/publish/bench/catalog.ts +++ b/js/publish/bench/catalog.ts @@ -46,7 +46,7 @@ async function measure(tracks: number, viewers: number) { }; }); // Initial configuration is outside the estimate traffic being measured. - for (const viewer of subscribers) await viewer.catalog.next(); + for (const viewer of subscribers) await viewer.catalog.latest(); let latest = subscribers[0].subscriber.latest(); let publishes = 0; let updates = 0; @@ -56,7 +56,7 @@ async function measure(tracks: number, viewers: number) { latest = next; publishes++; for (const viewer of subscribers) { - const root = await viewer.catalog.next(); + const root = (await viewer.catalog.latest())?.value; for (let index = 0; index < media.length; index++) { // Model the player's changed latency floor with real subscription handles. viewer.media[index].update({ diff --git a/js/publish/src/broadcast.test.ts b/js/publish/src/broadcast.test.ts index 797b2d3fdf8..30a8cf9be9e 100644 --- a/js/publish/src/broadcast.test.ts +++ b/js/publish/src/broadcast.test.ts @@ -17,7 +17,7 @@ async function readCatalog(broadcast: Broadcast): Promise({ track: track.subscribe() }).next(); + const catalog = (await new Json.Snapshot.Consumer({ track: track.subscribe() }).latest())?.value; effect.close(); return catalog; } @@ -138,7 +138,7 @@ test("serves the catalog through a shared static track", async () => { if (!net) throw new Error("expected a network producer once connected"); const subscriber = net.track(Broadcast.CATALOG_TRACK).subscribe(); - const catalog = await new Json.Snapshot.Consumer({ track: subscriber }).next(); + const catalog = (await new Json.Snapshot.Consumer({ track: subscriber }).latest())?.value; expect(catalog?.video?.renditions.video?.codec).toBe("avc1.640028"); // Dropping the subscriber closes the served track; the broadcast keeps running for the next viewer. @@ -200,14 +200,14 @@ test("keeps the current catalog snapshot for a reconnecting viewer", async () => if (!net) throw new Error("expected a network producer once connected"); const first = net.track(Broadcast.CATALOG_TRACK).subscribe(); - expect((await new Json.Snapshot.Consumer({ track: first }).next())?.video).toBeDefined(); + expect((await new Json.Snapshot.Consumer({ track: first }).latest())?.value.video).toBeDefined(); first.close(); // A reconnect past the idle cache window must still receive the live track's newest // snapshot instead of waiting forever for an edit that may never come. now += 60_000; const second = net.track(Broadcast.CATALOG_TRACK).subscribe(); - expect((await new Json.Snapshot.Consumer({ track: second }).next())?.video).toBeDefined(); + expect((await new Json.Snapshot.Consumer({ track: second }).latest())?.value.video).toBeDefined(); second.close(); broadcast.close(); diff --git a/js/publish/src/catalog.test.ts b/js/publish/src/catalog.test.ts index 3b3a51531f8..9a2d04fa66c 100644 --- a/js/publish/src/catalog.test.ts +++ b/js/publish/src/catalog.test.ts @@ -19,13 +19,13 @@ test("catalog producer seeds subscribers and fans out edits", async () => { const consumer = new Json.Snapshot.Consumer({ track: track.subscribe() }); // A new subscriber is seeded with the current catalog. - expect((await consumer.next())?.video).toEqual({ renditions: {} }); + expect((await consumer.latest())?.value.video).toEqual({ renditions: {} }); // An extension owner adds its own section; the subscriber sees the update, video untouched. catalog.mutate((c) => { c.scte35 = { splices: [] }; }); - const update = await consumer.next(); + const update = (await consumer.latest())?.value; expect(update?.video).toEqual({ renditions: {} }); expect(update?.scte35).toEqual({ splices: [] }); @@ -73,7 +73,7 @@ test("catalog producer advertises the page clock from the first snapshot", async const consumer = new Json.Snapshot.Consumer({ track: track.subscribe() }); // Before any rendition: a live-only publisher exposes its clock without an archive. - const first = Catalog.RootSchema.parse(await consumer.next()); + const first = Catalog.RootSchema.parse((await consumer.latest())?.value); if (!first.clock) throw new Error("expected a root clock"); expect(first.archive).toBeUndefined(); expect(first.clock.timescale).toBe(1_000_000); @@ -88,7 +88,7 @@ test("catalog producer advertises the page clock from the first snapshot", async catalog.mutate((c) => { c.video = { renditions: {} }; }); - const second = Catalog.RootSchema.parse(await consumer.next()); + const second = Catalog.RootSchema.parse((await consumer.latest())?.value); expect(second.clock).toEqual(first.clock); effect.close(); @@ -110,7 +110,7 @@ test("a reconnecting subscriber is seeded with the full current catalog", async const effect = new Effect(); const track = new Track.Producer("catalog.json"); catalog.serve(track, effect); - const seeded = await new Json.Snapshot.Consumer({ track: track.subscribe() }).next(); + const seeded = (await new Json.Snapshot.Consumer({ track: track.subscribe() }).latest())?.value; expect(seeded?.video).toEqual({ renditions: {} }); expect(seeded?.scte35).toEqual({ splices: [] }); @@ -292,9 +292,9 @@ describe("estimate publication window", () => { test("estimate rises publish at the leading edge and coalesce to the latest trailing value", async () => { const { track, consumer, estimate } = fixture(); - await consumer.next(); + await consumer.latest(); estimate(1); - expect((await consumer.next())?.video?.renditions.v.jitter).toBe(Catalog.u53(1)); + expect((await consumer.latest())?.value.video?.renditions.v.jitter).toBe(Catalog.u53(1)); const first = track.latest(); advance(100); estimate(2); @@ -302,26 +302,26 @@ describe("estimate publication window", () => { estimate(17); expect(track.latest()).toBe(first); advance(1); - expect((await consumer.next())?.video?.renditions.v.jitter).toBe(Catalog.u53(17)); + expect((await consumer.latest())?.value.video?.renditions.v.jitter).toBe(Catalog.u53(17)); const trailing = track.latest(); advance(1000); expect(track.latest()).toBe(trailing); estimate(18); - expect((await consumer.next())?.video?.renditions.v.jitter).toBe(Catalog.u53(18)); + expect((await consumer.latest())?.value.video?.renditions.v.jitter).toBe(Catalog.u53(18)); }); test("a structural edit publishes immediately with any pending estimate", async () => { const { producer, track, consumer, estimate } = fixture(); - await consumer.next(); + await consumer.latest(); estimate(1); - await consumer.next(); + await consumer.latest(); advance(100); estimate(2); producer.mutate((catalog) => { if (!catalog.video) throw new Error("video missing"); catalog.video.renditions.v.codedWidth = Catalog.u53(1280); }); - const catalog = await consumer.next(); + const catalog = (await consumer.latest())?.value; expect(catalog?.video?.renditions.v.jitter).toBe(Catalog.u53(2)); expect(catalog?.video?.renditions.v.codedWidth).toBe(Catalog.u53(1280)); const folded = track.latest(); @@ -331,14 +331,14 @@ describe("estimate publication window", () => { test("removing a track cancels the pending estimate and publishes immediately", async () => { const { producer, track, consumer, estimate } = fixture(); - await consumer.next(); + await consumer.latest(); estimate(1); - await consumer.next(); + await consumer.latest(); estimate(2); producer.mutate((catalog) => { delete catalog.video; }); - expect((await consumer.next())?.video).toBeUndefined(); + expect((await consumer.latest())?.value.video).toBeUndefined(); const removed = track.latest(); advance(1000); expect(track.latest()).toBe(removed); @@ -346,18 +346,18 @@ describe("estimate publication window", () => { test("closing the last output cancels its trailing timer", async () => { const { effect, consumer, estimate } = fixture(); - await consumer.next(); + await consumer.latest(); estimate(1); - await consumer.next(); + await consumer.latest(); estimate(2); effect.close(); expect(timers).toHaveLength(0); }); test("delay shares the window across tracks and new tracks remain immediate", async () => { const { producer, track, consumer, estimate } = fixture(); - await consumer.next(); + await consumer.latest(); estimate(1); - await consumer.next(); + await consumer.latest(); advance(10); producer.mutate((catalog) => { if (!catalog.video) throw new Error("video missing"); @@ -365,31 +365,31 @@ describe("estimate publication window", () => { }); const leading = track.latest(); advance(990); - expect((await consumer.next())?.video?.renditions.v.delay).toBe(Catalog.u53(7)); + expect((await consumer.latest())?.value.video?.renditions.v.delay).toBe(Catalog.u53(7)); expect(track.latest()).toBe((leading ?? 0) + 1); producer.mutate((catalog) => { if (!catalog.video) throw new Error("video missing"); catalog.video.renditions.other = { codec: "avc1.640028", container: { kind: "legacy" } }; }); - expect((await consumer.next())?.video?.renditions.other).toBeDefined(); + expect((await consumer.latest())?.value.video?.renditions.other).toBeDefined(); }); test("a folded estimate starts a new window and an extension edit stays immediate", async () => { const { producer, track, consumer, estimate } = fixture(); - await consumer.next(); + await consumer.latest(); estimate(1); - await consumer.next(); + await consumer.latest(); advance(100); estimate(2); producer.mutate((catalog) => { catalog.scte35 = { jitter: 3 }; }); - expect((await consumer.next())?.scte35).toEqual({ jitter: 3 }); + expect((await consumer.latest())?.value.scte35).toEqual({ jitter: 3 }); estimate(3); const folded = track.latest(); advance(900); expect(track.latest()).toBe(folded); advance(100); - expect((await consumer.next())?.video?.renditions.v.jitter).toBe(Catalog.u53(3)); + expect((await consumer.latest())?.value.video?.renditions.v.jitter).toBe(Catalog.u53(3)); }); }); diff --git a/js/room/src/metadata.test.ts b/js/room/src/metadata.test.ts index 3acfe426edf..68f6abd902a 100644 --- a/js/room/src/metadata.test.ts +++ b/js/room/src/metadata.test.ts @@ -39,10 +39,10 @@ test("metadata updates keep the same track and reach an existing subscriber", as await flush(); const track = net.consume().track(TRACK.user).subscribe(); const consumer = new Json.Snapshot.Consumer<{ name: string }>({ track }); - expect((await consumer.next())?.name).toBe("Alice"); + expect((await consumer.latest())?.value.name).toBe("Alice"); user.name.set("Bob"); await flush(); - expect((await consumer.next())?.name).toBe("Bob"); + expect((await consumer.latest())?.value.name).toBe("Bob"); track.close(); } finally { effect.close(); diff --git a/js/room/src/metadata.ts b/js/room/src/metadata.ts index 35e3ea170e6..b298dfed578 100644 --- a/js/room/src/metadata.ts +++ b/js/room/src/metadata.ts @@ -236,9 +236,9 @@ function subscribeJson( const consumer = new Json.Snapshot.Consumer({ track }); effect.spawn(async () => { for (;;) { - const value = await effect.race(consumer.next()); - if (value === undefined) break; - update(value); + const state = await effect.race(consumer.latest()); + if (state === undefined) break; + update(state.value); } }); } diff --git a/js/watch/src/broadcast.ts b/js/watch/src/broadcast.ts index e82da627aa1..b394800e81f 100644 --- a/js/watch/src/broadcast.ts +++ b/js/watch/src/broadcast.ts @@ -386,7 +386,7 @@ export class Broadcast { schema: Catalog.RootSchema, compression: format === "hangz" ? "deflate" : "none", }); - fetchNext = () => consumer.next(); + fetchNext = async () => (await consumer.latest())?.value; } else { const ordered = track.ordered(); fetchNext = async () => { diff --git a/quest/m1/README.md b/quest/m1/README.md index 542d1a9121f..b8cefdd8548 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -81,7 +81,6 @@ bench all require its crate. - [moq.sh deploys from CI](/quest/m1/moq-sh-deploy.md) - the first `release` run of the moq.sh workflow deploys with the Workers Editor token - [Plan: untimed verbatim PES](/quest/m1/plan-ts-pes-untimed.md) - decide how a verbatim TS track carries a PES that has no PTS, then write the implementation quest - [Data consumer timestamps](/quest/m1/data-consumer-timestamps.md) - json and binary consumers return each value's timestamp, in Rust and every binding; snapshots add `latest()` beside an in-order `next()` -- [JS data consumer timestamps](/quest/m1/js-data-consumer-timestamps.md) - @moq/json and @moq/flate consumers return each value's timestamp, with snapshot `next()` and `latest()` - [IETF object gaps](/quest/m1/ietf-object-gaps.md) - a gapped object ID is refused loudly like a subgroup in Rust and JS, and an overflowing one closes the session in Rust - [JavaScript FETCH](/quest/m1/js-fetch.md) - browser publishers answer IETF FETCH through the JS ranges request surface - [Relay session limits](/quest/m1/relay-session-limits.md) - moq-relay sets per-session request limits, tighter for clients than peers, and the bindings name a refused request diff --git a/quest/m1/data-consumer-timestamps.md b/quest/m1/data-consumer-timestamps.md index 9cfc25a21af..f552f69fe6a 100644 --- a/quest/m1/data-consumer-timestamps.md +++ b/quest/m1/data-consumer-timestamps.md @@ -38,8 +38,8 @@ playhead needs the newest state at or before it. - Same names in the moq-mux wrappers, moq-ffi, and every binding. moq-ffi's json/flate consumers return the timestamp too, and the py, swift, -kt, go, and dart wrappers and `doc/lib/*` follow. JS is -[JS data consumer timestamps](/quest/m1/js-data-consumer-timestamps.md). +kt, go, and dart wrappers and `doc/lib/*` follow. `@moq/json` and `@moq/flate` +already return `Timed` with the same `next()` and `latest()`. Public API: breaking. Wire: none. diff --git a/quest/m1/js-data-consumer-timestamps.md b/quest/m1/js-data-consumer-timestamps.md deleted file mode 100644 index ecfb5d3e8fd..00000000000 --- a/quest/m1/js-data-consumer-timestamps.md +++ /dev/null @@ -1,32 +0,0 @@ -# [M] JS data consumers return each value's timestamp - -## Goal - -`@moq/json` and `@moq/flate` snapshot and stream consumers return each value -with its frame's timestamp, so a browser can hold telemetry back until the -video playhead reaches it. Mirrors -[Data consumer timestamps](/quest/m1/data-consumer-timestamps.md) in Rust. - -## Plan - -A web frontend showing KLV or MAVLink telemetry beside video has to read raw -frames today to recover the timestamps. - -Decided (2026-10-01): `next()` and the async iterator yield `@moq/net`'s -`Timed` (`{ value, at? }`), the type the producers take. -`at` is the frame's timestamp, absent for an untimed frame. Decided -(2026-10-05, types settled 2026-10-06): timedness is per track, as the -untimed model ([#4822](https://github.com/moq-dev/moq/pull/4822)) decided and `@moq/net` mirrors: -`timescale` is optional, an untimed track's frames have no `at`, and a frame -whose timedness doesn't match its track is refused. Snapshot -consumers get the same two reads as Rust: `next()` yields every state in -order (`@moq/json` stops draining to the latest, `@moq/flate` reads groups in -order), and `latest()` skips to the newest state, today's behavior. A reader -that falls behind still jumps to the newest group. Update -`doc/lib/js/{json,flate}.md`. - -Public API: breaking. Wire: none. - -## Related - -- [Synced data playback](/quest/m2/watch-data-sync.md) - the js/watch reader built on these timestamps diff --git a/quest/m2/watch-data-sync.md b/quest/m2/watch-data-sync.md index bb4ed367106..dddd2bfec60 100644 --- a/quest/m2/watch-data-sync.md +++ b/quest/m2/watch-data-sync.md @@ -28,6 +28,5 @@ releases it. ## Required -- [JS data consumer timestamps](/quest/m1/js-data-consumer-timestamps.md) - the reader releases each value by the timestamp its consumer returns - [A/V clock](/quest/m1/av-clock.md) - the playhead the reader releases against - [Watch worker](/quest/m1/watch-worker.md) - moves `Sync` into a worker, where the reader registers diff --git a/test/interop/clients/js-native/subscribe.ts b/test/interop/clients/js-native/subscribe.ts index b07c9fe0511..4a11e8f3677 100644 --- a/test/interop/clients/js-native/subscribe.ts +++ b/test/interop/clients/js-native/subscribe.ts @@ -58,7 +58,7 @@ async function video(bc: Moq.Broadcast.Consumer): Promise<[string, Catalog.Video const track = bc.track("catalog.json").subscribe({ priority: Catalog.PRIORITY.catalog }); const catalog = new Json.Snapshot.Consumer({ track, schema: Catalog.RootSchema }); for (;;) { - const root = await catalog.next(); + const root = (await catalog.latest())?.value; if (!root) throw new Error("catalog ended without a video track"); const first = Object.entries(root.video?.renditions ?? {})[0]; if (first) return first;