Skip to content
Merged
3 changes: 2 additions & 1 deletion js/net/src/origin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,8 @@ class OriginState {
*
* What a shared connection lends out. {@link Producer} implements it, so code that is
* handed an origin rather than owning one should accept this type: closing the origin
* stays the owner's alone, and a borrower cannot express it.
* stays the owner's alone, and a borrower cannot express it. A borrower that only reads
* should accept just the {@link Consumer} members it needs, so `producer.consume()` fits too.
*
* @public
*/
Expand Down
2 changes: 1 addition & 1 deletion js/watch/api/element.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Video } from "@moq/watch";

export default class extends HTMLElement {
constructor();
attributeChangedCallback(name: "delay" | "buffer" | "url" | "name" | "follow" | "volume" | "muted" | "paused" | "visible" | "backend" | "versions" | "announced" | "catalog-format" | "captions", oldValue: string | null, newValue: string | null): void;
attributeChangedCallback(name: "delay" | "buffer" | "follow" | "announced" | "url" | "name" | "volume" | "muted" | "paused" | "visible" | "backend" | "versions" | "catalog-format" | "captions", oldValue: string | null, newValue: string | null): void;
readonly audio: Audio.Decoder;
get backend(): Video.Backend;
set backend(value: Video.Backend);
Expand Down
4 changes: 2 additions & 2 deletions js/watch/api/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export type BroadcastInput = {
enabled: Signals.Getter<boolean>;
follow: Signals.Getter<boolean>;
name: Signals.Getter<Net.Path.Valid>;
origin: Signals.Getter<Net.Origin.Table | undefined>;
origin: Signals.Getter<Pick<Net.Origin.Consumer, "closed" | "discovery" | "request" | "follow" | "announced"> | undefined>;
};

export type BroadcastOutput = {
Expand Down Expand Up @@ -161,7 +161,7 @@ export type PlayerInput = {
enabled: Signals.Getter<boolean>;
follow: Signals.Getter<boolean>;
name: Signals.Getter<Net.Path.Valid>;
origin: Signals.Getter<Net.Origin.Table | undefined>;
origin: BroadcastInput["origin"];
paused: Signals.Getter<boolean>;
probe: Signals.Getter<Net.Connection.Probe | undefined>;
text: {
Expand Down
2 changes: 1 addition & 1 deletion js/watch/src/broadcast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function origin(paths: string[]): Origin.Producer {
function broadcast(name: string, paths: string[] = [name]): { source: Broadcast; owner: Origin.Producer } {
const owner = origin(paths);
const source = new Broadcast({
origin: owner,
origin: owner.consume(),
name: Path.from(name),
enabled: true,
follow: false,
Expand Down
20 changes: 9 additions & 11 deletions js/watch/src/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function provesOther(active: Moq.Broadcast.Consumer | undefined, epoch: Moq.Epoc
// Whether an announced route covers `path`, until closed. Hidden routes count: a service claim
// under a `.`-named prefix is kept out of listings, but it still covers the renditions it would
// produce, and this is never shown to anyone.
function watchCovered(origin: Moq.Origin.Table, path: Moq.Path.Valid): { covered: Getter<boolean>; close: () => void } {
function watchCovered(origin: Origin, path: Moq.Path.Valid): { covered: Getter<boolean>; close: () => void } {
const announced = origin.announced(Path.Pattern.literal(path), { hidden: true, covering: true });
const covered = new Signal(false);
const active = new Set<Moq.Path.Valid>();
Expand Down Expand Up @@ -101,9 +101,9 @@ export type CatalogFormat = (typeof CATALOG_FORMATS)[number];
* another component whose output is wired in) does the writing.
*/
export type BroadcastInput = {
// The origin to consume from. Independent of any connection: whichever sessions feed
// The read-side origin to borrow. Independent of any connection: whichever sessions feed
// the origin resolve the broadcast, and the handle spans their reconnects.
origin: Getter<Moq.Origin.Table | undefined>;
origin: Getter<Pick<Moq.Origin.Consumer, "closed" | "discovery" | "request" | "follow" | "announced"> | undefined>;

// Whether to start downloading the broadcast. Defaults to true.
enabled: Getter<boolean>;
Expand All @@ -127,6 +127,8 @@ export type BroadcastInput = {
catalog: Getter<Catalog.Root | undefined>;
};

type Origin = NonNullable<GetterType<BroadcastInput["origin"]>>;

/** Signals the component derives. */
export type BroadcastOutput = {
// "error" means the origin refused the broadcast; `error` says why.
Expand Down Expand Up @@ -164,13 +166,13 @@ export class Broadcast {
// its relay about the broadcasts a catalog actually references.
readonly #watches = new Map<
Moq.Path.Valid,
{ origin: Moq.Origin.Table; covered: Getter<boolean>; close: () => void; users: number }
{ origin: Origin; covered: Getter<boolean>; close: () => void; users: number }
>();

// Requests are shared by callers; each owns its restart watch in the origin.
readonly #references = new Map<
Moq.Path.Valid,
{ origin: Moq.Origin.Table; follow: boolean; request: Moq.Origin.Requesting; users: number }
{ origin: Origin; follow: boolean; request: Moq.Origin.Requesting; users: number }
>();

// The catalog as published, before renditions this consumer cannot use are dropped. Kept
Expand Down Expand Up @@ -226,7 +228,7 @@ export class Broadcast {
// gets selected: its service claims a covering prefix, and nothing announces the exact path
// until this subscribes. Watches exactly `path`, shared by every caller asking about it.
// The blind cases (announcement gate off, no discovery) never reach here; see `#relativeTarget`.
#isPathAnnounced(effect: Effect, origin: Moq.Origin.Table, path: Moq.Path.Valid): boolean {
#isPathAnnounced(effect: Effect, origin: Origin, path: Moq.Path.Valid): boolean {
let entry = this.#watches.get(path);
if (!entry || entry.origin !== origin) {
entry?.close();
Expand All @@ -249,11 +251,7 @@ export class Broadcast {
}

// Share one origin request across callers reading the same referenced broadcast.
#requestBroadcast(
effect: Effect,
origin: Moq.Origin.Table,
path: Moq.Path.Valid,
): Moq.Broadcast.Consumer | undefined {
#requestBroadcast(effect: Effect, origin: Origin, path: Moq.Path.Valid): Moq.Broadcast.Consumer | undefined {
const follow = effect.get(this.in.follow);
let entry = this.#references.get(path);
if (!entry || entry.origin !== origin || entry.follow !== follow) {
Expand Down
36 changes: 36 additions & 0 deletions js/watch/src/player.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, mock, test } from "bun:test";
import { Origin, Path } from "@moq/net";
import { Signal } from "@moq/signals";

// Bun does not run Vite's worklet loader; these tests never create an AudioContext.
Expand All @@ -15,6 +16,41 @@ test("Player refuses the renamed reload and announced inputs", () => {
expect(() => new Player({ announced: true } as never)).toThrow("`announced` was renamed to `follow`");
});

test.each(["table", "consumer", "structural"] as const)("Player borrows a %s origin", async (role) => {
const producer = new Origin.Producer();
const consumer = producer.consume();
const table: Origin.Table = producer;
const origins = {
table,
consumer,
structural: {
closed: consumer.closed,
discovery: consumer.discovery,
request: consumer.request.bind(consumer),
follow: consumer.follow.bind(consumer),
announced: consumer.announced.bind(consumer),
},
};
const name = Path.from("live.hang");
const player = new Player({ origin: origins[role], name, catalogFormat: "manual", catalog: {} });
try {
const active = player.broadcast.out.active.changed();
const publication = producer.createBroadcast(name);
publication.announce();
expect((await active)?.path).toBe(name);
expect(player.in.origin.peek()).toBe(origins[role]);
expect(player.broadcast.in.origin.peek()).toBe(origins[role]);

player.close();
await flush();
expect(producer.closed.peek()).toBeUndefined();
expect(publication.broadcast.closed.peek()).toBeUndefined();
} finally {
player.close();
producer.close();
}
});

test("Player owns the shared pipeline and feeds output policy into decoders", async () => {
const enabled = new Signal(true);
const paused = new Signal(false);
Expand Down
6 changes: 3 additions & 3 deletions js/watch/src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import type * as Moq from "@moq/net";
import { Path } from "@moq/net";
import { Effect, type Getter, getter, type Inputs, type Readonlys, Signal } from "@moq/signals";
import * as Audio from "./audio";
import { Broadcast, type CatalogFormat } from "./broadcast";
import { Broadcast, type BroadcastInput, type CatalogFormat } from "./broadcast";
import { type Delay, Sync } from "./sync";
import * as Text from "./text";
import * as Video from "./video";

/** Inputs for a complete broadcast playback pipeline. */
export type PlayerInput = {
/** Origin that supplies the broadcast. */
origin: Getter<Moq.Origin.Table | undefined>;
/** Read-side origin that supplies the broadcast, borrowed without its lifecycle. */
origin: BroadcastInput["origin"];
/** Broadcast path relative to the origin. */
name: Getter<Moq.Path.Valid>;
/** Whether the pipeline may subscribe. */
Expand Down
1 change: 0 additions & 1 deletion quest/m1/api-audit/resolve/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ Wire: none.

## Required

- [The player takes any read-side origin](/quest/m1/api-audit/resolve/watch-origin.md) - `@moq/watch`'s `Player` and `Broadcast` accept a read-only `Origin.Consumer` as well as a `Table`
- [JS follow returns a Following](/quest/m1/api-audit/resolve/js-following.md) - `follow` and `request` get their own JS types, matching Rust and the bindings
- [JS follow takes FollowOptions](/quest/m1/api-audit/resolve/follow-options.md) - `follow` and `request` get their own JS options types

Expand Down
22 changes: 0 additions & 22 deletions quest/m1/api-audit/resolve/watch-origin.md

This file was deleted.

1 change: 0 additions & 1 deletion quest/m1/api-audit/route/handle.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,4 @@ wrappers. Wire: none.

## Related

- [The player takes any read-side origin](/quest/m1/api-audit/resolve/watch-origin.md) - also retypes JS `Table`
- No constructible internal handles - the same JS hygiene rule