@kargjonas/sabus
v0.2.0
Published
Minimal SharedArrayBuffer worker bus for low-overhead cross-worker data sharing.
Maintainers
Readme
sabus
Minimal SharedArrayBuffer worker bus for low-overhead cross-worker data sharing.
Check the
examples/directory. Each example has a focusedREADMEwith context and usage notes.
Install
Install from command line:
npm install @kargjonas/sabusQuick start
Host:
import { SharedRuntime, Type } from "sabus";
const schema = {
count: Type.Int32,
} as const;
const host = SharedRuntime.host();
const counter = host.createSharedObject("counter", schema);
const worker = new Worker(new URL("./worker.ts", import.meta.url), { type: "module" });
await host.attachWorker("reader", worker);
await counter.write({ count: 1 });
console.log(counter.read()?.count); // 1Worker (worker.ts):
import { SharedRuntime, Type } from "sabus";
const schema = {
count: Type.Int32,
} as const;
const runtime = await SharedRuntime.worker();
const counter = runtime.openSharedObject("counter", schema);
counter.subscribe(() => {
const latest = counter.read();
if (latest) console.log("count:", latest.count, "seq:", latest.seq);
});For worker-side usage and more complete patterns, check examples/.
What it provides
SharedRuntimeto coordinate shared objects across host and workers.SharedObjectwith FIFO write lock and atomic latest-read behavior.TypedSharedObjectfor schema-based typed reads and writes.- Bulk array writes (
TypedArray#set) for all schema array fields, includingType.Rgba8. schemahelpers (Type,computeLayout,readSnapshot,writeFields).
Requirements
- Environment with
SharedArrayBuffersupport. - Browser usage needs cross-origin isolation headers (
COOP/COEP).
Development
npm run typecheck
npm test
npm run buildExample app:
npm run dev