Skip to content

Device package

ankka-surface on npm is the device half: ESM, TypeScript, no runtime dependency, no framework. It runs in a browser and its tests run on Node 24. It speaks the HTTP API and nothing else.

Exports

Export Kind What
SurfaceModel class one surface on one device
applyMove function the fold the server applies, pure
SurfaceError class a refusal from the server
eventData function a server-sent event's data, parsed as JSON
Surface, Move, Anchor, Pending, RailEntry, Showing, Focus types the records
Open, Say, Show, Hide, SetFocus, Said, Heard types the bodies and answers
SurfaceModelOptions, Listener, FocusListener, Source types what the model takes and gives
DeviceClass, Renderer<T> types how a device renders, and the render seam
Hearing, Ear types the listening seam

SurfaceModelOptions

Option Default What
baseUrl required the endpoint's prefix as the device reaches it, such as /api/surfaces; a trailing slash is dropped
id required the surface's id
device required this device's own id
deviceClass required how this device renders: phone, laptop, tv or a class the vocabulary names
room false whether others can see this device
railLength 3 the vocabulary's rail length, for the local fold
focus false open the focus lane: stream it and let this device set it
focusIntervalMs 120 the most often this device sends its focus
reconnectDelayMs 1000 how long after the server ends a stream before fetching and streaming again
now the current time the clock for rail entries made by the local fold
fetch the global fetch for tests; called on the window, as a browser requires
EventSource the global EventSource for tests

SurfaceModel

Member What it does
surface the surface held, or null when there is none: never opened, or closed
connected true while the surface's stream is open
focused the focus held, or null when the lane is off or nothing has been read
subscribe(listener) calls listener(surface, source) on every change to the surface held; returns the way to stop
subscribeFocus(listener) calls listener(focus, source) on every change to the focus held; returns the way to stop
connect() fetches the surface, opens its stream, calls show, and with the lane on fetches and streams the focus; a 404 leaves surface null and opens nothing
disconnect() calls hide, ignoring a failure, and closes both streams
open(owner, shape?) opens the surface, holds the answer, and streams and shows if not connected
move(move) applies the move locally, sends it, and holds the answer; a refusal or a network failure restores what was held and throws
say(text, utterance?) sends the words to the interpreter; holds the answer only if it is the newest say's and at a higher version
hear(text) the interpreter's answer for the words; nothing persisted or held
focus(fields) holds the focus locally at once and sends it throttled; throws when the lane is off
show() records this device as showing the surface
hide() removes this device from the surface's showing
close() deletes the surface for every device, closes both streams, and holds null

Source, the second argument of a listener, is where the change came from: fetch, local (the device's own move or focus, or a restore after a refusal), answer, stream, or removed.

When the server ends the surface's stream, the model waits reconnectDelayMs, fetches the surface and streams again, retrying after the same delay while the server is unreachable. When the browser's EventSource drops and reconnects by itself, the model fetches the surface once the stream is open again.

applyMove

function applyMove(surface: Surface, move: Move, at: string, railLength?: number): Surface;

Applies move to surface by the fold the server applies and returns a new surface; the input is not changed. It leaves version alone, because the server owns versions. It throws on a move that names nothing. at is the time of a rail entry the move's request makes.

SurfaceError

Thrown for any answer that is not 2xx. status is the HTTP status; body is the answer's text, the JSON refusal the HTTP API lists; message names the call and both.

The seams

type Renderer<T> = (surface: Surface, deviceClass: DeviceClass, room: boolean) => T;
type Hearing =
  | { kind: "partial"; text: string }
  | { kind: "final"; text: string; alternatives: string[] }
  | { kind: "end" };
type Ear = { start(): void; stop(): void; abort(): void };

The package ships no renderer and no ear. Renderer is what a device fills with whatever it draws with; Ear is what a consumer wraps a speech recogniser in.