Documentation / @etherfold/browser / sharedWorkerHost
Function: sharedWorkerHost() ​
sharedWorkerHost(
create):HostAccess
Defined in: packages/browser/src/host/sharedWorker.ts:104
A HOST IN A SHAREDWORKER, as the tab reaches it.
const indexer = connectToIndexerHost(
sharedWorkerHost(
() => new SharedWorker(new URL('./indexer.worker.ts', import.meta.url), {type: 'module', name: 'my-app-indexer'}),
),
);ONE ARGUMENT is the whole difference from dedicatedWorkerHost: the app writes the line that constructs the worker (the URL has to be a literal its bundler can see, so that the entry -- and the processor it imports -- is BUILT and type-checked with the rest of the app), and the code it writes against the port afterwards does not know which of the two it got.
NAME the worker. It is half of its identity, so leaving it unnamed means every SharedWorker an origin loads from this script URL is the same one -- which is what you want for two tabs of one app and not what you want for two apps that happen to share a bundle.
close() does NOT take the host down, and cannot ​
There is no terminate() on a SharedWorker and there should not be: the instance belongs to every tab attached to it, so one tab letting go closes ITS OWN port and leaves the fold running for the others. The browser ends the worker when its LAST client is gone, which is what makes "the last tab closes" an ordinary resume rather than a lifecycle to manage (ADR-0027: the cursor is written in the same transaction as the block it describes, so the next tab reads it and carries on).
That also makes the restart path differ in one respect worth knowing: a dedicated worker is TERMINATED before its successor is opened, so a host merely suspected of being dead cannot become a second writer. Here there is nothing to terminate and nothing to guard against -- a SharedWorker is a singleton by construction, so reopen reaches the instance that is already running where one is, and starts a fresh one only where the browser has already ended it.
Parameters ​
create ​
() => SharedWorker