Handles
Act on an intent the same way whether it runs here, in a worker, on a server or in a native host's JavaScript engine.
A handle
Application code that acts on an intent (an undo button, a dialog that asks System Two to reprogram, a review of a proposal) holds a handle. handleOf(intent) gives one for an intent in the same thread. remoteHandle(transport) gives the same type, an IntentHandle, for an intent served elsewhere. Code written against the handle works with either.
import { createIntent, handleOf } from '@reintent/core';
const handle = handleOf(createIntent({ program: <Card />, model }));
const receipt = await handle.decide({ of: 'card', name: 'tone' }, 'warm');
handle.snapshot().history.canUndo; // true
await handle.history.undo();- Every write resolves to a
Receipt. A local handle takes the step at once and resolves at once; a remote one resolves when the other side answers.{ ok: true, at }means the intent took the step. - After the receipt, the snapshot has the step.
snapshot()is the state as data, aHandleState: the program's version, the input, the layers, the history, the requests in flight.atcounts the changes the handle has seen. Once a receipt withathas come,snapshot().atis that or later.subscribe(fn)says when the snapshot may have changed. - A refusal is a receipt.
refused: the intent did not take the step (an input that is not data, a proposal written on another version, a program that fails its trial).not-allowed: the side with the intent does not let a remote handle do it.unknown-proposal: the proposal was already reviewed.closed: the handle was closed, or the intent was disposed (it throwsIntentDisposedto code that uses it directly). - A write does not wait for the screen. A receipt means the intent committed the step. Whether a host has drawn it yet is the host connection's business (Standalone hosts).
Several writes as one step
handle.commit(note, fn) writes what fn writes as one step of the history, with note for it, as intent.commit does: one sentence said in place of another is one step, and one undo takes it back. fn gets a HandleTx, the transaction without program: decisions, constraints, preferences and sentences. handle.review(p).accept(migrate) writes what migrate writes in the same step as the program. handle.rejectAll(id) says that none of a choice's possibilities fits.
await handle.commit('“It is for dinner.”', (tx) => {
tx.untell('She loves a joke.');
tx.tell('It is for dinner.');
});
await handle.review(p).accept((tx) => tx.decide({ of: 'card', name: 'signatures' }, 6));A remote handle runs fn on its own side and sends what it wrote as data, so fn reads what it needs before the call, not the intent's state.
Proposals as data
handle.reprogram({ prompt }) asks System Two, as intent.reprogram does, and resolves to a ProposalView: what a person reads (summary, text, the tried λ as tree), with the program kept on the intent's side. handle.review(p).accept() finds the proposal by its id. handle.onProposal(fn) hears every proposal the intent makes, including the ones a <World> asks for. HandleReprogram is what reprogram takes.
const p = await handle.reprogram({ prompt: 'write a poem in it' });
show(p.summary, p.tree);
const r = await handle.review(p).accept();
if (!r.ok) say(r.message); // e.g. the program changed meanwhileAn intent elsewhere
serveHandle(intent, transport) serves a handle on a Transport: { send, onMessage, close }, a worker's port or a WebSocket. The messages are JSON. The side with the intent sends a step's state before its receipt, so the receipt finds the snapshot already new.
// worker.ts: the intent and the model live here
import { serveHandle } from '@reintent/lib/remote';
serveHandle(createIntent({ program: <Card />, model }), portOf(self));
// page.ts
import { remoteHandle } from '@reintent/lib/remote';
const handle = await remoteHandle(portOf(worker));
await handle.tell('it is her birthday');setInput from a remote handle gives not-allowed unless the side with the intent opens it: serveHandle(intent, transport, { setInput: true }).
Its display, elsewhere
The handle is the intent's operations and state. What it draws goes on a connection of its own, one way: serveViews(intent, transport, { contexts }) on the side with the intent commits each view a remote side opens, and the requests of each committed evaluation are sent there and then, without waiting to hear that it was drawn. remoteView(transport, { context }) on the host's side gives a RemoteView: each ViewCommit, the tree and what its evaluation read, as it comes.
// worker.ts
serveViews(intent, portOf(self), { contexts: { printed: PRINTED, phone: PHONE } });
// page.ts
const printed = remoteView(portOf(worker), { context: 'printed' });
printed.subscribe(({ tree, observed }) => draw(tree, observed));
await printed.act('/children/1/props/onClick', offered);A context crosses by its name: the side with the intent says which contexts it serves. An action goes back with where its tree offered it, and is taken only if the tree committed to that view now offers the same action there: a click on a button that has since gone is refused, not taken as whatever is there now.
A new version, over the air
A host that stays installed can be given a new version of the program while it runs: the decisions a person made stay. The side with the intent allows it and says how a ProgramPackage becomes a component (DeployOptions). A view says which kinds it draws, and the RemoteHandle that remoteHandle gives deploys:
// worker.ts
serveHandle(intent, portOf(self), { deploy: { compile: (pkg) => compileSource(pkg.source) } });
serveViews(intent, portOf(self));
// page.ts
const view = remoteView(portOf(worker), { kinds: ['stack', 'text', 'choice'] });
const handle = await remoteHandle(portOf(worker));
await handle.deploy({ id: 'trip', version: '2', requires: ['stack', 'text', 'map'], source });
// → { ok: false, code: 'missing-capability', message: 'trip@2 needs map', missing: ['map'] }Before the version goes live it is checked: every open view draws the kinds it requires, it compiles, and a trial evaluation on a copy of the layers, in each view's context, draws only kinds it requires (and keeps to the catalog, when the options give one). The DeployReceipt of a version that fails a check says which one, and nothing changes. A version that passes replaces the program; the layers stay, so a choice with the same identity keeps its decision. To push a version to several hosts, deploy it through each one's handle: each installs it or refuses it, with its own receipt.
In React
@reintent/react uses the handle itself: useReintent() gives a root's operations, a refusal turned into a rejected promise, and useReintent(select) reads its state. The same code works for an intent here and one elsewhere. A root whose intent is in a worker is drawn with <ReintentView remote view>: remote is its handle, and view a RemoteViewSource, such as remoteView gives.
import { useReintent } from '@reintent/react';
function Undo() {
const { history } = useReintent();
const canUndo = useReintent((s) => s.history.canUndo);
return <button disabled={!canUndo} onClick={() => void history.undo()}>Undo</button>;
}useReintent(handle, select) reads a handle held outside a root, such as one remoteHandle gave.
The sandboxes on these pages work this way: a root a step's App.tsx mounts runs in a worker with its model, and only App.tsx runs on the page, so its useReintent() reaches the root through a remote handle and draws a remote view.
Last updated on
Edit on GitHub