Skip to content

RPC helpers

import { withTimeout, abortable, once, listenN, RpcTimeoutError } from "@tynd/core/client";

Utilities layered on top of createBackend / createEmitter — they don’t change the public API shape, they just make common call-site patterns terser.

Reject with RpcTimeoutError if the promise hasn’t settled within ms ms.

try {
const user = await withTimeout(api.getUser("alice"), 2000);
} catch (err) {
if (err instanceof RpcTimeoutError) retryLater();
else throw err;
}

Race a promise against an AbortSignal. Rejects with the signal’s reason (or a generic AbortError) when abort() fires.

const ctrl = new AbortController();
document.querySelector("#cancel")?.addEventListener("click", () => ctrl.abort());
try {
const result = await abortable(api.compute(), ctrl.signal);
} catch (err) {
if ((err as DOMException).name === "AbortError") {
// user cancelled
}
}

once(event, handler) / listenN(event, n, handler)

Section titled “once(event, handler) / listenN(event, n, handler)”

Auto-unsubscribe after the first (once) or nth (listenN) invocation. Both return a manual unsubscribe for early opt-out.

once("ready", () => hideSplash());
const off = listenN<Progress>("progress", 100, (p) => render(p));
// off() can stop the subscription before 100 events land.

Both subscribe to backend emitter events by name, untyped. createBackend already exposes a typed .once() for the same events. Native OS events (window:focused, tray:click, clipboard:change, …) don’t reach these helpers: use the plugin client’s on* method, or osOn from @tynd/core/client/bridge.

listenN throws a RangeError when n is not a finite number of at least 1.

Typed proxy for a JavaScript plugin declared with definePlugin. Calling client.method(a, b) sends the RPC $plugin$<name>$<method> with [a, b]. Generator methods return streaming handles, as with streaming RPC.

plugin/client.ts
import { createPluginClient } from "@tynd/core/client";
import type Counter from "./backend";
export const counter = createPluginClient<typeof Counter>("counter");
await counter.increment();

Convert between Uint8Array and standard base64. They use the native Uint8Array.toBase64 / fromBase64 when the engine has them, and a chunked btoa / atob fallback otherwise.

import { base64ToBytes, bytesToBase64 } from "@tynd/core/client";
const b64 = bytesToBase64(new Uint8Array([1, 2, 3])); // "AQID"
const bytes = base64ToBytes(b64);