The message protocol between WorkerComponent on the main thread and exposeWorkerApi inside a worker.
This module imports nothing, so a worker entry point bundled by @mnaoumov/obsidian-dev-toolkit’s workers feature can import it: a worker has no obsidian module and no Node builtins. Its message shapes are a wire format between the plugin’s main bundle and its worker bundle, which may come from different copies of this library, so fields are only ever added and a change of meaning moves to a new WORKER_PROTOCOL value.
| Function |
Description |
| exposeWorkerApi |
Exposes handlers to the main thread. Call it once, at the top level of a worker entry point. A handler that throws or rejects answers with a SerializedWorkerError, which the caller receives as a WorkerCallError. An unknown method answers with an error too, so a caller never waits for nothing. |
| invokeWorkerHandler |
Calls one handler by name, as the worker does. WorkerComponent uses it for its main-thread fallback, so both paths behave alike. |
| isTransferredValue |
Tells whether a value is a TransferredValue. |
| isWorkerRequestMessage |
Tells whether a message is a request of this protocol. |
| isWorkerResponseMessage |
Tells whether a message is a response of this protocol. |
| serializeWorkerError |
Reduces a thrown value to plain data. |
| unwrapTransferredValue |
Splits a result into the value to post and the objects to transfer. |
| withTransfer |
Marks a result to transfer some of its objects (an ArrayBuffer, a MessagePort, an OffscreenCanvas…) instead of copying them. A transferred object is unusable on the sending side afterwards. |
| Variable |
Description |
| WORKER_PROTOCOL |
The tag every message of this protocol carries. Messages without it are ignored on both sides, so a worker can exchange its own messages too. |