Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/web/src/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ discovery.run((effect) => {
});
});

function subscribeNode(effect: Signals.Effect, origin: Net.Origin.Table, path: Net.Path.Valid, node: string) {
function subscribeNode(effect: Signals.Effect, origin: Net.Origin.Reader, path: Net.Path.Valid, node: string) {
nodeStats.mutate((s) => {
s[node] = {
egress: {},
Expand Down
16 changes: 9 additions & 7 deletions js/net/api/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export namespace Group {
export const IMPLEMENTATION: string;

export namespace Origin {
export class Consumer {
export class Consumer implements Origin.Reader {
announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer;
get closed(): Signals.GetPromise<Error | null>;
get discovery(): Signals.Getter<boolean | undefined>;
Expand Down Expand Up @@ -418,6 +418,13 @@ export namespace Origin {
get route(): Origin.Route | undefined;
unannounce(): void;
}
export interface Reader {
announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer;
readonly closed: Signals.GetPromise<Error | null>;
readonly discovery: Signals.Getter<boolean | undefined>;
follow(path: Path.Valid, options?: Origin.RequestOptions): Origin.Requesting;
request(path: Path.Valid, options?: Origin.RequestOptions): Origin.Requesting;
}
export class Request {
readonly path: Path.Valid;
prepare(epoch: Epoch.Valid): Origin.Prepared;
Expand All @@ -444,13 +451,8 @@ export namespace Origin {
epoch?: Epoch.Valid;
hops: Origin.Hop[];
}
export interface Table {
announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer;
readonly closed: Signals.GetPromise<Error | null>;
export interface Table extends Origin.Reader {
createBroadcast(path: Path.Valid): Origin.Publication;
readonly discovery: Signals.Getter<boolean | undefined>;
follow(path: Path.Valid, options?: Origin.RequestOptions): Origin.Requesting;
request(path: Path.Valid, options?: Origin.RequestOptions): Origin.Requesting;
requests(prefix: Path.Valid): Origin.Requests;
}
export function isAnonymous(route: Origin.Route): boolean;
Expand Down
34 changes: 23 additions & 11 deletions js/net/src/origin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,25 +1101,22 @@ class OriginState {
}

/**
* A non-owning handle on an origin: publish into it and read it, without its lifecycle.
* The read side of an origin, borrowed: resolve broadcasts by path and watch what is
* available, without publishing into it or owning its lifecycle.
*
* 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. A borrower that only reads
* should accept just the {@link Consumer} members it needs, so `producer.consume()` fits too.
* Code that only reads an origin should accept this type. {@link Consumer} implements it
* and {@link Table} extends it, so `producer.consume()`, a borrowed table, and a structural
* equivalent all fit.
*
* @public
*/
export interface Table {
/** Settles once the origin closes; see {@link Producer.closed}. */
export interface Reader {
/** Settles once the origin closes; see {@link Consumer.closed}. */
readonly closed: GetPromise<Error | null>;

/** Whether every attached session announces into the table; see {@link Consumer.discovery}. */
readonly discovery: Getter<boolean | undefined>;

/** Create an unannounced broadcast at `path`; see {@link Producer.createBroadcast}. */
createBroadcast(path: Path.Valid): Publication;

/** Resolve one publisher instance at `path`; see {@link Consumer.request}. */
request(path: Path.Valid, options?: RequestOptions): Requesting;

Expand All @@ -1128,6 +1125,21 @@ export interface Table {

/** The available broadcasts under `scope`, as a live stream; see {@link Consumer.announced}. */
announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer;
}

/**
* A non-owning handle on an origin: publish into it and read it, without its lifecycle.
*
* 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. A borrower that only reads
* should accept {@link Reader} instead, so `producer.consume()` fits too.
*
* @public
*/
export interface Table extends Reader {
/** Create an unannounced broadcast at `path`; see {@link Producer.createBroadcast}. */
createBroadcast(path: Path.Valid): Publication;

/** Advertise a prefix and serve requests under it; see {@link Producer.requests}. */
requests(prefix: Path.Valid): Requests;
Expand Down Expand Up @@ -1679,7 +1691,7 @@ export class Requesting {
*
* @public
*/
export class Consumer {
export class Consumer implements Reader {
#state: OriginState;
#scope: Scope;

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" | "follow" | "announced" | "url" | "name" | "volume" | "muted" | "paused" | "visible" | "backend" | "versions" | "catalog-format" | "captions", oldValue: string | null, newValue: string | null): void;
attributeChangedCallback(name: "delay" | "buffer" | "url" | "name" | "follow" | "volume" | "muted" | "paused" | "visible" | "backend" | "versions" | "announced" | "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<Pick<Net.Origin.Consumer, "closed" | "discovery" | "request" | "follow" | "announced"> | undefined>;
origin: Signals.Getter<Net.Origin.Reader | 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: BroadcastInput["origin"];
origin: Signals.Getter<Net.Origin.Reader | undefined>;
paused: Signals.Getter<boolean>;
probe: Signals.Getter<Net.Connection.Probe | undefined>;
text: {
Expand Down
21 changes: 13 additions & 8 deletions js/watch/src/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ 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: Origin, path: Moq.Path.Valid): { covered: Getter<boolean>; close: () => void } {
function watchCovered(
origin: Moq.Origin.Reader,
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 @@ -103,7 +106,7 @@ export type CatalogFormat = (typeof CATALOG_FORMATS)[number];
export type BroadcastInput = {
// 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<Pick<Moq.Origin.Consumer, "closed" | "discovery" | "request" | "follow" | "announced"> | undefined>;
origin: Getter<Moq.Origin.Reader | undefined>;

// Whether to start downloading the broadcast. Defaults to true.
enabled: Getter<boolean>;
Expand All @@ -127,8 +130,6 @@ 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 @@ -166,13 +167,13 @@ export class Broadcast {
// its relay about the broadcasts a catalog actually references.
readonly #watches = new Map<
Moq.Path.Valid,
{ origin: Origin; covered: Getter<boolean>; close: () => void; users: number }
{ origin: Moq.Origin.Reader; 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: Origin; follow: boolean; request: Moq.Origin.Requesting; users: number }
{ origin: Moq.Origin.Reader; 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 @@ -228,7 +229,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: Origin, path: Moq.Path.Valid): boolean {
#isPathAnnounced(effect: Effect, origin: Moq.Origin.Reader, path: Moq.Path.Valid): boolean {
let entry = this.#watches.get(path);
if (!entry || entry.origin !== origin) {
entry?.close();
Expand All @@ -251,7 +252,11 @@ export class Broadcast {
}

// Share one origin request across callers reading the same referenced broadcast.
#requestBroadcast(effect: Effect, origin: Origin, path: Moq.Path.Valid): Moq.Broadcast.Consumer | undefined {
#requestBroadcast(
effect: Effect,
origin: Moq.Origin.Reader,
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
4 changes: 2 additions & 2 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 BroadcastInput, type CatalogFormat } from "./broadcast";
import { Broadcast, 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 = {
/** Read-side origin that supplies the broadcast, borrowed without its lifecycle. */
origin: BroadcastInput["origin"];
origin: Getter<Moq.Origin.Reader | undefined>;
/** 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/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ Rejected in planning:

- [Two broadcast resolutions](/quest/m1/api-audit/resolve/README.md) - Rust, JS, and the bindings resolve a broadcast with `request(path, epoch)` or `follow(path, epoch)`, and every racy two-step caller is gone
- [Routes are the origin API](/quest/m1/api-audit/route/README.md) - apps announce exact or prefix routes through one `Route` handle that owns its broadcast and carries an `identity`, prefix routes can serve a held broadcast, and readers get a listing plus `follow`
- [@moq/net names the origin read side](/quest/m1/api-audit/origin-reader.md) - `Origin.Reader`, which `Origin.Table` extends, replaces `Pick` lists

## Related

Expand Down
20 changes: 0 additions & 20 deletions quest/m1/api-audit/origin-reader.md

This file was deleted.