@evolu/web
Version:
Evolu for web
30 lines • 1.42 kB
TypeScript
/**
* SharedWebWorker (WebWorker + BroadcastChannel + Web Locks)
*
* A SharedWebWorker is a Web Worker that is shared across multiple browser
* tabs. This implementation provides a shared worker-like experience even in
* browsers that do not support the native SharedWorker API.
*
* Unlike a true SharedWorker (which uses MessagePorts for direct, tab-specific
* communication), this approach uses BroadcastChannel for cross-tab messaging
* and Web Locks to ensure only one tab owns and runs the actual Worker
* instance.
*
* All tabs communicate via BroadcastChannel, so every message is broadcast to
* all tabs, and each tab must filter/process only relevant messages. This is
* less efficient than MessagePorts, but it works everywhere and is "good
* enough" for most use cases.
*
* See the protocol and coordination logic below for details.
*
* @module SharedWebWorker
*/
import { SimpleName, Worker } from "@evolu/common";
/**
* Creates a shared Web Worker using BroadcastChannel and Web Locks. This allows
* multiple tabs to share a single Web Worker instance. The first tab to acquire
* the lock becomes the owner and runs the worker. Other tabs act as proxies,
* forwarding messages to the owner.
*/
export declare const createSharedWebWorker: <Input, Output>(name: SimpleName, createWebWorker: () => globalThis.Worker) => Worker<Input, Output>;
//# sourceMappingURL=SharedWebWorker.d.ts.map