1
import type { AnyContract, InputOf, Ref } from "./contract.ts";2
import type { Op } from "./delta/index.ts";4
/**5
* Options of `get`. `P` is the app's principal type: a JSON-serializable identity of a caller, mapped to an object key6
* by `createHost({ principalKey })`.7
*/8
export 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), and13
* is ready once its object is live.14
*/15
readonly state?: boolean;16
}18
/**19
* The explicit call context of one root (a short Chord call tree). Calls made with it carry its root id and code20
* version; `get` references are released when the call that received this ctx settles.21
*/22
export 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 state30
* member of its variant (unless `state: false`). Rejects with the activation error, `aborted` when the ctx is31
* aborted or the call settles first, or `unavailable/ready_timeout` after the host's `readyTimeout`; the reference32
* is released then.33
*/34
get<C extends AnyContract>(type: C, input: InputOf<C>, options?: GetOptions<P>): Promise<Ref<C>>;35
}37
export 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 };42
/** A readable state source: a cell, a derivation, or a reference's state member. Values are immutable by contract. */43
export 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
}51
/**52
* What an implementation may expose as a state member: a cell, a derivation, or a held object's state member (an53
* object goes live only once the members it exposes have their values).54
*/55
export type StateSource<T> = Readable<T>;57
declare const EXPOSED: unique symbol;59
/** A local member backed by a resource (`self.expose(res.x)`): references read the resource's value once live. */60
export interface Exposed<T> {61
readonly [EXPOSED]: T;62
}64
export type RefStatus = "loading" | "live" | "released" | "gone";66
export interface Shape {67
readonly variant: string | undefined;68
readonly members: readonly string[];69
/** Fingerprint of the provider contract. */70
readonly contract: string;71
}73
export 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
}