Writing an adapter
An adapter connects a UI framework to the runtime. The runtime owns the protocol, the connection, idle tracking, logs, state and media; the adapter answers what only the framework knows: the tree and focus.
ts
import { createToets } from '@toets/sdk';
export function startToets(options = {}) {
const toets = createToets({ payload: discoverPayload(), framework: 'my-framework', ...options });
if (!toets.active) return toets; // inert: install nothing
toets.handle('tree.get', ({ rootId }) => ({ root: mapTree(rootId), viewport: viewport() }));
// Platform focus, or what the app reported with reportFocus() once it reports.
const focused = () =>
toets.reportedFocus ? toElement(toets.reportedFocus.target) : platformFocus();
toets.handle('focus.get', () => ({ node: mapNode(focused()) }));
toets.setFocusSource({ current: () => nodeIdOf(focused()) });
framework.onFocusChange(() => toets.focusChanged());
toets.onStop(installFrameworkIdleHooks(toets.idle)); // returns a cleanup
toets.start();
return toets;
}Rules
- Inert first. Do nothing (no patches, no listeners) unless
toets.active. Register cleanups withonStop; they run when the session ends. - Domains follow handlers.
treeis announced whentree.getis registered,focuswhentree.getandfocus.getare. Register beforestart(). Without a reliable focus source, do not register focus handlers: wrong focus data is worse than none (toets falls back to driver focus). - One focus source. Once
toets.reportedFocusis set (the app calledreportFocus()), take focus only from it, infocus.get, the tree'sfocusedflags and the focus source. Resolve what the app passes the way the framework's apps hold elements (elements, refs,{ current }objects,null). Never report where focus would go: toets presses and reads focus again. - Nodes: ids unique and stable per element (a
WeakMapfrom element to id), own text only, collapse structural wrappers, at most onefocused. ThrowRpcError(RpcErrorCode.NotFound)for an unknownrootId. - Idle hooks:
toets.idle.begin(kind, description, maxMs)returns a release function; add probes withtoets.idle.addProbe(() => busyResources). UsesetNativeTimeoutfor the adapter's own scheduling so it never counts as app work. - Old engines: build for ES2015, avoid
globalThis(usegetGlobal()), no heavy dependencies. - Prove it: run the conformance suite with a fixture app (
@toets/protocoldocs,conformance.md).
Non-JS SDKs (Kotlin, Swift, BrightScript) implement the protocol spec directly and use the same conformance suite through toets-conformance.