pi-mono/packages/chord/src/registry.ts
Mario Zechner 2688aba7dc refactor(chord): name things after the concepts; object variants; link protocol version; object lifetimes
- References are Ref<C> (was Handle<C>), objects ObjectImpl (record.ts is object.ts), ImplementationOf<C> (was
  BehaviorOf<C>), ResourceDeclaration (was ResourceSpec); epochs are code versions (codeVersion in Ctx, RootOptions,
  Shape, Host, frames and reload reports). Docs say object and reference.
- A remote object's variant is named variant (was kind): factories return { variant }, references expose ref.variant.
- Links announce a protocol version (PROTOCOL_VERSION = 2) in hello; peers of different versions close the link with
  reason protocol-version <n> and report it through LinkOptions.onProtocolMismatch.
- ServeOptions.lifetime ("referenced" | "host") replaces eager: a host-lifetime object is local, has an empty input,
  exists once per host and lives until the host closes; awaitEager is awaitHostObjects. A reload that makes a type
  referenced releases the host's own reference.
2026-10-14 05:34:14 +00:00

30 lines
1 KiB
TypeScript

import type { Ctx } from "./types.ts";
export interface Registry<V> {
readonly name: string;
readonly conflict: "error" | "last";
/** Type carrier only; never set. */
readonly value?: V;
}
/** `conflict: "error"` rejects two objects registering one key; `"last"` keeps the later registration. */
export const defineRegistry = <V>(name: string, options: { conflict?: "error" | "last" } = {}): Registry<V> => ({
name,
conflict: options.conflict ?? "error",
});
export type RegistryTable<V> = ReadonlyMap<string, V>;
export interface RegistryView<V> {
/** Table of `ctx.codeVersion`, or of the current code version without ctx. */
table(ctx?: Ctx): RegistryTable<V>;
get(key: string, ctx?: Ctx): V | undefined;
/** Called with the current table immediately and whenever a new table is installed. */
subscribe(listener: (table: RegistryTable<V>, codeVersion: number) => void): () => void;
}
export interface Registration {
readonly registry: Registry<unknown>;
readonly key: string;
readonly value: unknown;
}