Render and steer on a device¶
SurfaceModel holds one surface on one device. It keeps the surface current from the server's
stream, applies the device's own moves at once, and tells a renderer every time the surface changes.
A renderer subscribes and draws; a steering device also calls move and say.
Install it¶
ankka-surface has no release yet. In a checkout of ankka-surface, build the package and depend on it by path:
cd device && npm ci && npm run build
{ "dependencies": { "ankka-surface": "file:../ankka-surface/device" } }
The package is ESM and TypeScript, has no runtime dependencies, and runs in a browser.
Describe the device¶
A model is made for one surface on one device. baseUrl is the endpoint's prefix as the device
reaches it, id the surface's id, device this device's own id, deviceClass how it renders, and
room whether others can see it:
const options: SurfaceModelOptions = {
baseUrl: "/api/surfaces",
id: "ada",
device: "kitchen-tv",
deviceClass: "tv",
room: true,
};
new SurfaceModel(options) makes the model. Its other options are on
Device package; the one most often set is
railLength, which matches the vocabulary's when the vocabulary does not keep three.
Draw, connect, open, move and say¶
This function is the whole life of a steering device in a dozen lines. It subscribes a renderer, connects, opens the surface if nobody has, makes a move, and says something:
async function steer(model: SurfaceModel, draw: (text: string) => void): Promise<void> {
model.subscribe((surface) => {
draw(surface === null ? "nothing to show" : `${surface.shape} ${surface.anchor?.id ?? ""}`.trim());
});
await model.connect();
if (model.surface === null) await model.open("ada");
await model.move({ shape: "item", anchor: { kind: "market", id: "m1" }, request: "show me the market" });
const said = await model.say("sort by newest");
if (!said.understood) draw(said.reason ?? "not understood");
}
Against a surface nobody had opened, it draws, in order:
nothing to show connect found no surface
home open answered version 1
home show answered version 2
item m1 the move, applied locally at once
item m1 the server's answer, version 3
nothing sorts by newest here the interpreter's reason
What each call does:
subscribegives the listener the surface, ornullwhen there is none, and where the change came from:fetch,local,answer,streamorremoved. It returns the way to stop listening.connectfetches the surface, opens its stream, and shows this device on it. A surface never opened leavessurfacenull and opens nothing.openopens the surface with its owner, and streams and shows ifconnectdid not.moveapplies the move locally at once and sends it. The server's answer replaces the local result. A refusal restores what was held and throwsSurfaceError, with thestatusandbody.saysends the words to the interpreter and is not applied locally. The answer says whether they were understood and, when not, why. The newest say wins: the answer to an older one is returned and not held.
Draw for the device¶
The renderer decides what each shape looks like on this device's class, and hides what is personal when the device is in a room. The package names that seam as a type and ships no renderer:
type Renderer<T> = (surface: Surface, deviceClass: DeviceClass, room: boolean) => T;
Draw a word the renderer does not know, a shape or an anchor kind added since it was written, as nothing rather than fail.
Offer the way back¶
Each entry of surface.rail holds a request and the shape, anchor and modifiers it left. Going back
to one is a move to them, with no request, so going back does not push the rail:
const entry = model.surface!.rail[1]!;
await model.move({ shape: entry.shape, ...(entry.anchor ? { anchor: entry.anchor } : { clearAnchor: true }), modifiers: entry.modifiers });
That block is written by hand. The move sets the entry's modifiers and leaves any others in place;
add their keys to clearModifiers for an exact return.
Leave cleanly¶
disconnect hides this device from the surface, ignoring a failure, and closes the streams. Call it
when the page goes away:
window.addEventListener("pagehide", () => { void model.disconnect(); });
close deletes the surface for every device. Call it only when the person means to end it.