Reintent

@reintent/lib/remote

An intent's handle across a transport (docs/design/0056 §4): `serveHandle` on the side that holds the intent (a worker, a native host's JavaScript engine, a server), `remoteHandle` on the host's side.

The handle there is the type handleOf gives here, so application code works with either.

serveHandle(intent, transport, { setInput: false })     the side with the intent
const handle = await remoteHandle(transport)            the host's side, once the state came
await handle.history.undo()                             → { ok: true, at }

The messages are JSON data. The side with the intent sends a step's state before its receipt; a transport keeps the order, so a host has the new snapshot when the receipt comes. A proposal stays on the side with the intent, by its id, until it is reviewed.

Its display goes the same way (0060 §5): serveViews on the side with the intent commits each view one way; remoteView on the host's side gets each commit and sends actions back.

Over the air (0063): when the side with the intent allows it (serveHandle(…, { deploy })), a host deploys a new version of the program with handle.deploy(pkg). It is checked against the kinds each open view draws (remoteView(…, { kinds })) before it goes live, and the decisions stay.

New. The transport's shape is 0054's (#376, a prototype): { send, onMessage, close }.

Type Aliases

DeployOptions

type DeployOptions = object;

Defined in: packages/lib/src/remote.ts

What the side with the intent needs to deploy a version: how a package becomes a component.

Properties

PropertyTypeDescription
catalog?HostCatalogThe catalog a version's trial tree is checked against, when there is one (0043).

Methods

compile()
compile(pkg): 
  | Component<any>
| Promise<Component<any>>;

Defined in: packages/lib/src/remote.ts

Parameters
ParameterType
pkgProgramPackage
Returns

| Component<any> | Promise<Component<any>>


DeployReceipt

type DeployReceipt = 
  | Receipt
  | {
  code: "missing-capability" | "compile" | "evaluate" | "invalid-tree";
  message: string;
  missing?: readonly string[];
  ok: false;
};

Defined in: packages/lib/src/remote.ts

A deploy's receipt (0063 §3): a handle's receipt, or why the version was refused, by the check that refused it. missing is the kinds a host showing the intent does not draw.


ProgramPackage

type ProgramPackage = object;

Defined in: packages/lib/src/remote.ts

A version of a program, sent as source (0036 §4): what a deploy carries. requires is the kinds it draws.

Properties

PropertyModifierType
idreadonlystring
requiresreadonlyreadonly string[]
sourcereadonlystring
versionreadonlystring

RemoteHandle

type RemoteHandle<I> = IntentHandle<I> & object;

Defined in: packages/lib/src/remote.ts

The handle remoteHandle gives: an intent's handle (0056), and a deploy (0063).

Type Declaration

NameTypeDescription
deploy()(pkg) => Promise<DeployReceipt>Deploys a new version of the program, when the side with the intent allows it. It goes live only if every open view draws what it requires, it compiles, and its trial evaluation in each view's context draws only kinds it requires; the layers stay, and with them the decisions.

Type Parameters

Type ParameterDefault type
Iunknown

RemoteView

type RemoteView = object;

Defined in: packages/lib/src/remote.ts

A view of an intent served on the other end of a transport (serveViews).

Methods

act()
act(
   at, 
   offered, 
   taken?
): Promise<Receipt>;

Defined in: packages/lib/src/remote.ts

Sends the action the tree offers at at (a JSON pointer to the prop), taken as taken when the host gives it a value (a field's text for a decide); resolves to its receipt.

Parameters
ParameterType
atstring
offeredHostAction
taken?HostAction
Returns

Promise<Receipt>

close()
close(): void;

Defined in: packages/lib/src/remote.ts

Returns

void

latest()
latest(): ViewCommit | undefined;

Defined in: packages/lib/src/remote.ts

The latest commit, or none before the first comes.

Returns

ViewCommit | undefined

subscribe()
subscribe(fn): () => void;

Defined in: packages/lib/src/remote.ts

Called with each commit, in order. Returns the unsubscribe.

Parameters
ParameterType
fn(commit) => void
Returns

() => void


Transport

type Transport = object;

Defined in: packages/lib/src/remote.ts

Where messages go and come from: a MessagePort, a WebSocket, a worker's postMessage.

Methods

close()?
optional close(): void;

Defined in: packages/lib/src/remote.ts

Returns

void

onMessage()
onMessage(fn): () => void;

Defined in: packages/lib/src/remote.ts

Calls fn with each message that comes. Returns the unsubscribe.

Parameters
ParameterType
fn(message) => void
Returns

() => void

send()
send(message): void;

Defined in: packages/lib/src/remote.ts

Parameters
ParameterType
messageunknown
Returns

void


ViewCommit

type ViewCommit = object;

Defined in: packages/lib/src/remote.ts

A committed tree and what its evaluation read, as a remote view gets them.

Properties

PropertyType
observedObservation
tree| Rendered | Rendered[] | null

Functions

remoteHandle()

function remoteHandle<I>(transport): Promise<RemoteHandle<I>>;

Defined in: packages/lib/src/remote.ts

The handle of an intent served on the other end of transport (serveHandle). Resolves once the first state has come, so snapshot() always has one. A write resolves when its receipt comes, and the snapshot is then at the receipt's at or later; close() (or the other side's stop) gives closed to every call still waiting and to every call after.

Type Parameters

Type ParameterDefault type
Iunknown

Parameters

ParameterType
transportTransport

Returns

Promise<RemoteHandle<I>>


remoteView()

function remoteView(transport, opts?): RemoteView;

Defined in: packages/lib/src/remote.ts

Opens a view of the intent served on transport, in the context it names (or the intent's own); content when this side draws host content (a slot's node, as React does, 0045 §5); kinds, the kinds this side draws, which a deployed version must keep to (0063 §4). Without kinds, a deploy does not check against this view.

Parameters

ParameterType
transportTransport
opts{ content?: boolean; context?: string; kinds?: readonly string[]; }
opts.content?boolean
opts.context?string
opts.kinds?readonly string[]

Returns

RemoteView


serveHandle()

function serveHandle(
   intent, 
   transport, 
   opts?
): () => void;

Defined in: packages/lib/src/remote.ts

Serves intent's handle on transport: each call a host makes is taken here, in the order it comes, and answered with a receipt, after the state it led to. setInput is not allowed unless opts.setInput: the side that holds the intent sets the root's input (0054 §7). Returns the stop: it closes the handle; the intent stays.

Parameters

ParameterType
intentIntent<any>
transportTransport
opts{ deploy?: DeployOptions; setInput?: boolean; }
opts.deploy?DeployOptions
opts.setInput?boolean

Returns

() => void

Example

import { choices, createIntent, host, useChoose } from '@reintent/core';
import { remoteHandle, serveHandle, type Transport } from '@reintent/lib/remote';

// Two ends of a channel, each message passed as JSON (a worker's port would do).
const ends = (): [Transport, Transport] => {
  const fns: ((m: unknown) => void)[][] = [[], []];
  const end = (me: number): Transport => ({
    send: (m) => {
      const copy = JSON.parse(JSON.stringify(m));
      queueMicrotask(() => fns[1 - me].forEach((f) => f(copy)));
    },
    onMessage: (f) => (fns[me].push(f), () => {}),
  });
  return [end(0), end(1)];
};
const [here, there] = ends();

const Card = () =>
  host('p', {
    children: useChoose(choices('warm', 'formal'), { of: 'card', name: 'tone' }),
  });
const intent = createIntent({ program: <Card /> });
serveHandle(intent, there);

const handle = await remoteHandle(here);
const receipt = await handle.decide({ of: 'card', name: 'tone' }, 'formal');
console.log(receipt.ok); // → true
console.log(handle.snapshot().history.notes); // → [ 'card#tone = "formal"' ]
console.log(intent.layers.choices['card#tone'].decision?.value); // → 'formal'

serveViews()

function serveViews(
   intent, 
   transport, 
   opts?
): () => void;

Defined in: packages/lib/src/remote.ts

Serves intent's display on transport: each view a remote side opens is a connection of its own, in the context it names (one of contexts, or the intent's own), committed one way. An action a view sends is taken only if the tree committed to that view now offers the same action at the same place, on a node of the same kind and key (0060 §9); otherwise its receipt says refused. Returns the stop.

Parameters

ParameterType
intentIntent<any>
transportTransport
opts{ contexts?: Record<string, Provide>; }
opts.contexts?Record<string, Provide>

Returns

() => void

Last updated on

Edit on GitHub