skip to the page

@jam-overture/loom/live

Pulling the project's live pages from Loom and holding them in your server, so an accepted change reaches readers without a deploy.

Everything below is exported from that import. The names, the signatures and the sentences are read from the package itself rather than written here, so this page says what the copy of Loom in your node_modules says — and it changes in the same pull request the code does.

36 exports, in 5 modules. Generated from ./dist/live/index.d.ts, which is the declaration file this package publishes for @jam-overture/loom/live. Each comment inside a declaration is shown as its first paragraph. The whole of it is in that file.

Nothing to install first. Everything this import loads arrives with @jam-overture/loom itself.

No import here has everything behind it. @jam-overture/loom/live publishes 36 of the 2,237 names this package publishes. The other 2,201 are behind one of the 19 other imports, and 18 of those 19 publish nothing this one does. The imports do not nest: a name that is not on this page is not a name that does not exist, and the search at the top of the page says which import it comes from.

Some of these names are published elsewhere too: 3 by @jam-overture/loom/store — the same declarations reached through two doors, so either import gives you the same thing.

Compose

audience/compose

A page as the reader in front of it gets it: the base tree, plus the deltas the audiences they are in have earned.

AudienceFormtype

The delta one audience earns, against the base it was authored on.

type AudienceForm = {
    readonly audienceId: AudienceId;
    readonly delta: TreeDelta;
};

ComposeErrortype

type ComposeError = 
/** A rule matched an audience the overlay has no delta for. */
{
    readonly code: "no-form";
    readonly audienceId: AudienceId;
}
/** The base moved and this delta was authored against an older revision. */
 | {
    readonly code: "stale-form";
    readonly audienceId: AudienceId;
    readonly expected: number;
    readonly actual: number;
}
/** The delta is for another page. */
 | {
    readonly code: "form-mismatch";
    readonly audienceId: AudienceId;
    readonly treeId: string;
}
/** The operations did not apply, or what they produced is not a tree. */
 | {
    readonly code: "unapplicable";
    readonly audienceId: AudienceId;
    readonly detail: string;
};

describeComposeErrorfunction

Shown in use on How a change reaches readers — One call, where you render

const describeComposeError: (error: ComposeError) => string

Pages

live/pages

The live pages a customer's server holds, and the one question a render asks of them.

LivePagestype

What a pull leaves behind: the pages, the rules that sort their readers, the tag that names both, and enough of the last outcome to say whether the site is current.

type LivePages = {
    /** The live pages, by the id of the page each one is. */
    readonly pages: ReadonlyMap<TreeId, LoomTree>;
    /**
     * Each live page's segment rules, in the order the portal set. A page with
     * none is absent, and every reader of it is in no segment.
     */
    readonly rules: ReadonlyMap<TreeId, readonly AudienceRule[]>;
    /**
     * Each live page's segment forms: the delta one segment's readers are served
     * over the base. A page with none is absent,
     * and so is a segment that is only a name — which is what every segment was
     * before the door answered forms.
     */
    readonly forms: ReadonlyMap<TreeId, readonly AudienceForm[]>;
    /**
     * How long this project holds its readers, and each page's versions a pin can
     * still be served. `WINDOW_OFF` where nobody chose a window, and for a
     * door from before the window answered one.
     */
    readonly window: LiveWindow;
    /** The entity tag the door gave for exactly these pages, if it gave one. */
    readonly tag: string | undefined;
    /** When the door last answered, whether it answered pages or `304`. */
    readonly pulledAt: number | undefined;
    /** When these pages arrived, which is older than `pulledAt` after a `304`. */
    readonly freshAt: number | undefined;
    /** Failed pulls since the last one the door answered. Zero while current. */
    readonly failures: number;
};

NO_LIVE_PAGESvalue

A server that has not pulled yet, which is every server at the moment it starts.

const NO_LIVE_PAGES: LivePages

livePageOffunction

The live page for this page id, or undefined to render the page as built.

const livePageOf: (held: LivePages, page: TreeId, pin?: number) => LoomTree | undefined

segmentOffunction

The segment this reader of this page is in, or undefined for none — which is aud_everyone, and is what a page with no rules answers for every reader.

const segmentOf: (held: LivePages, page: TreeId, context: JsonObject | undefined) => AudienceId | undefined

ServedPagetype

A page as one reader gets it: what to render, and what to call them while they read it.

type ServedPage = {
    /** The tree to render — the live page with this reader's forms applied. */
    readonly tree: LoomTree;
    /**
     * The segment to file this page view under, or `undefined` for
     * `aud_everyone`. Absent after a refusal, because the reader got the base.
     */
    readonly audience: AudienceId | undefined;
    /**
     * Why the forms were not applied, for the host's log. `undefined` on every
     * request a deployment intends, including every request of a page with no
     * segments at all.
     */
    readonly refusal: ComposeError | undefined;
};

pageForReaderfunction

What to render for this reader, and what to file their signals under.

Shown in use on How a change reaches readers — One call, where you render

const pageForReader: (held: LivePages, built: LoomTree, context: JsonObject | undefined, pin?: number) => ServedPage

livePagesHeldfunction

Every live page held, in the order the door answered them.

const livePagesHeld: (held: LivePages) => readonly LoomTree[]

isPullingfunction

Whether the server is drawing on pages the door confirmed, rather than on nothing.

const isPulling: (held: LivePages) => boolean

Pull

live/pull

One pull of a project's live pages, and what it does to the pages a server already holds.

LIVE_PAGES_PATHvalue

The one path the client asks for, so no customer's configuration holds it.

const LIVE_PAGES_PATH = "/api/pages/live"

DEFAULT_PULL_CEILING_MSvalue

10s. A pull nobody is waiting for, with a ceiling well inside the interval.

const DEFAULT_PULL_CEILING_MS = 10000

SitePullFailuretype

Why a pull did not move what the server holds.

type SitePullFailure = {
    readonly reason: "unconfigured" | "unreachable" | "expired" | "refused" | "unreadable";
    readonly detail: string;
    readonly status?: number;
};

LivePageRulestype

One page's segment rules as the door answers them: the page, and its list in the order the portal set.

type LivePageRules = {
    readonly treeId: TreeId;
    readonly rules: readonly AudienceRule[];
};

LivePageFormstype

One page's segment forms as the door answers them: the page, and a delta per segment that has one, each authored against the revision of the page it arrives beside.

type LivePageForms = {
    readonly treeId: TreeId;
    readonly forms: readonly AudienceForm[];
};

SitePullOutcometype

What one pull did: brought pages and their rules, confirmed the ones held, or failed.

type SitePullOutcome = {
    readonly kind: "fresh";
    readonly pages: readonly LoomTree[];
    readonly rules: readonly LivePageRules[];
    readonly forms: readonly LivePageForms[];
    readonly window: LiveWindow;
    readonly tag: string | undefined;
} | {
    readonly kind: "unchanged";
} | {
    readonly kind: "failed";
    readonly failure: SitePullFailure;
};

PullFetchtype

The shape of fetch a pull needs, which is narrower than the global.

type PullFetch = (url: string, init: {
    readonly method: "GET";
    readonly headers: Readonly<Record<string, string>>;
    readonly signal: AbortSignal;
    readonly cache: "no-store";
}) => Promise<Response>;

PullLivePagesOptionstype

type PullLivePagesOptions = {
    /**
     * Where this project's Loom deployment is, as an origin — `https://loom.example.com`.
     * The path is this module's, so a project's configuration never carries one.
     */
    readonly deployment: string;
    /**
     * The project's public key, the one its pages already post signals with
     *. Opaque here: what a key may look like is the door's to say, and a
     * second spelling of the rule in the client would be a second place to be
     * wrong.
     */
    readonly key: string;
    /** How long to wait for the door. `DEFAULT_PULL_CEILING_MS` when unset. */
    readonly ceilingMs?: number;
    /** `globalThis.fetch` unless a host hands over its own. */
    readonly fetch?: PullFetch;
};

livePagesUrlOffunction

The URL a pull asks for, which by construction carries no query and no fragment.

const livePagesUrlOf: (deployment: string) => Result<URL, SitePullFailure>

pullLivePagesfunction

Asks the door once, and says what the answer was.

const pullLivePages: (options: PullLivePagesOptions, held: LivePages) => Promise<SitePullOutcome>

withPullOutcomefunction

What the server holds after an outcome: the pure half, and the only place the holding changes.

const withPullOutcome: (held: LivePages, outcome: SitePullOutcome, at: number) => LivePages

describeSitePullFailurefunction

One sentence for a log, naming what failed and what it cost.

Shown in use on How a change reaches readers — Your server asks, and keeps the answer

const describeSitePullFailure: (failure: SitePullFailure) => string

Pulling

live/pulling

The loop that keeps a customer's server current: pull, hold, wait, pull again.

DEFAULT_PULL_INTERVAL_MSvalue

A minute, which is a change live within a minute of somebody accepting it.

const DEFAULT_PULL_INTERVAL_MS = 60000

DEFAULT_FIRST_WAIT_MSvalue

How long a render waits for a new server's first answer, when Loom says nothing at all.

const DEFAULT_FIRST_WAIT_MS = 5000

PullScheduletype

How the loop waits, as a function, so a test can drive it without a clock.

type PullSchedule = (run: () => void, afterMs: number) => () => void;

StartPullingOptionstype

type StartPullingOptions = PullLivePagesOptions & {
    /** How long between pulls. `DEFAULT_PULL_INTERVAL_MS` when unset. */
    readonly intervalMs?: number;
    /** How long `ready()` waits on a silent Loom. `DEFAULT_FIRST_WAIT_MS` when unset. */
    readonly firstWaitMs?: number;
    /**
     * Told of every outcome, after what is held has moved.
     */
    readonly onOutcome?: (outcome: SitePullOutcome, held: LivePages) => void;
    readonly now?: () => number;
    readonly schedule?: PullSchedule;
};

LivePullLooptype

A running pull: what it holds, a way to pull out of turn, and a way to stop.

type LivePullLoop = {
    readonly ready: () => Promise<LivePages>;
    readonly held: () => LivePages;
    readonly pullNow: () => Promise<SitePullOutcome>;
    readonly stop: () => void;
};

startPullingLivePagesfunction

Shown in use on How a change reaches readers — Your server asks, and keeps the answer

const startPullingLivePages: (options: StartPullingOptions) => LivePullLoop

Window

live/window

The pin a reader carries, and the version of a page it asks for.

HeldVersiontype

One version of a page a pin can still be served.

type HeldVersion = {
    readonly until: number;
    readonly tree: LoomTree;
};

ReadersHeldtype

How long this project holds its readers, and what a pin needs to be resolved — the door's window, as the pull read it.

type ReadersHeld = {
    /** How long a reader keeps the pages they first saw. */
    readonly ms: number;
    /** The number below which a pin reads the newest pages: a snapshot or a revert raised it. */
    readonly floor: number;
    /** What the pages stand at now, which is what a new reader is pinned to. */
    readonly number: number;
    /** Each page's versions a pin can still be served, by the page's own id. */
    readonly held: ReadonlyMap<TreeId, readonly HeldVersion[]>;
};

LiveWindowtype

type LiveWindow = {
    readonly ms: 0;
} | ReadersHeld;

WINDOW_OFFvalue

Readers are not held: every page view reads the newest live page, no cookie is set, and nothing is kept on a reader's device.

const WINDOW_OFF: LiveWindow

pinInfunction

The pin in a request's Cookie header, or undefined for a reader who has no usable one.

const pinIn: (cookies: string | null | undefined) => number | undefined

pinCookieForfunction

The Set-Cookie value for this reader, or undefined to set none.

const pinCookieFor: (held: {
    readonly window: LiveWindow;
}, pin: number | undefined) => string | undefined