返回源码地图

packages/chord/src/types.ts

v1.3.0 · 71023aec1546 · 22:Ctx、Ref状态、资源声明契约

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1import type { AnyContract, InputOf, Ref } from "./contract.ts";
2import type { Op } from "./delta/index.ts";
3
4/**
5 * Options of `get`. `P` is the app's principal type: a JSON-serializable identity of a caller, mapped to an object key
6 * by `createHost({ principalKey })`.
7 */
8export interface GetOptions<P = unknown> {
9 /** Principal the object is created for. Defaults to the host's own principal (`createHost({ self })`). */
10 readonly principal?: P;
11 /**
12 * False: a method-only reference. It receives no state (unless another reference to the same object wants it), and
13 * is ready once its object is live.
14 */
15 readonly state?: boolean;
16}
17
18/**
19 * The explicit call context of one root (a short Chord call tree). Calls made with it carry its root id and code
20 * version; `get` references are released when the call that received this ctx settles.
21 */
22export interface Ctx<P = unknown> {
23 readonly signal: AbortSignal;
24 readonly rootId: string;
25 readonly codeVersion: number;
26 /** Who the root acts as: the caller at ingress, the host's own principal inside objects. */
27 readonly principal: P;
28 /**
29 * A reference for this call, released when the call settles. Resolves once it is ready: live, with every state
30 * member of its variant (unless `state: false`). Rejects with the activation error, `aborted` when the ctx is
31 * aborted or the call settles first, or `unavailable/ready_timeout` after the host's `readyTimeout`; the reference
32 * is released then.
33 */
34 get<C extends AnyContract>(type: C, input: InputOf<C>, options?: GetOptions<P>): Promise<Ref<C>>;
35}
36
37export type StateEvent<T> =
38 | { readonly type: "snap"; readonly value: T }
39 | { readonly type: "ops"; readonly ops: readonly Op[]; readonly value: T }
40 | { readonly type: "reset"; readonly value: T };
41
42/** A readable state source: a cell, a derivation, or a reference's state member. Values are immutable by contract. */
43export interface Readable<T> {
44 readonly value: T;
45 /** Called with the current value immediately, then with every new value. */
46 subscribe(listener: (value: T) => void): () => void;
47 /** Delta stream: a snapshot immediately, then ops, or a reset after a migration or rebind. */
48 events(listener: (event: StateEvent<T>) => void): () => void;
49}
50
51/**
52 * What an implementation may expose as a state member: a cell, a derivation, or a held object's state member (an
53 * object goes live only once the members it exposes have their values).
54 */
55export type StateSource<T> = Readable<T>;
56
57declare const EXPOSED: unique symbol;
58
59/** A local member backed by a resource (`self.expose(res.x)`): references read the resource's value once live. */
60export interface Exposed<T> {
61 readonly [EXPOSED]: T;
62}
63
64export type RefStatus = "loading" | "live" | "released" | "gone";
65
66export interface Shape {
67 readonly variant: string | undefined;
68 readonly members: readonly string[];
69 /** Fingerprint of the provider contract. */
70 readonly contract: string;
71}
72
73export interface RefBase<C extends AnyContract> {
74 /** The consumer contract token this reference was bound with. */
75 readonly contract: C;
76 readonly status: RefStatus;
77 readonly ready: Promise<void>;
78 readonly variant: string | undefined;
79 readonly shape: Shape | undefined;
80 /** Why the reference is gone, if it is. */
81 readonly error: unknown;
82 onShape(listener: (shape: Shape) => void): () => void;
83 /**
84 * Called after every status change (`loading` → `live`, `live` → `loading` while a link reconnects, `released`,
85 * `gone` with `error`). A UI watches for `gone` to `get` again.
86 */
87 onStatus(listener: (status: RefStatus, error: unknown) => void): () => void;
88 release(): void;
89}