Documentation / @etherfold/browser / dedicatedWorkerHost
Function: dedicatedWorkerHost() ​
dedicatedWorkerHost(
create):HostAccess
Defined in: packages/browser/src/host/dedicatedWorker.ts:76
A HOST IN A DEDICATED WORKER, as the tab reaches it.
The app writes the line that constructs the Worker, and that is deliberate: the URL has to be a literal its bundler can see, so that the worker entry -- and the processor that entry imports -- is BUILT, type-checked and de-duplicated with the rest of the app. new Worker(new URL('./indexer.worker.ts', import.meta.url), {type: 'module'}) is the form every current bundler understands, and a URL this package built for the caller would be a string no bundler traces.
const indexer = connectToIndexerHost(
dedicatedWorkerHost(() => new Worker(new URL('./indexer.worker.ts', import.meta.url), {type: 'module'})),
);It takes a FACTORY, and not a worker ​
Browsers evict workers, so a port has to be able to start another one (ADR-0082) -- and obtaining a port is the whole of what a hosting shape is, so obtaining one AGAIN belongs here rather than anywhere else. Handed an instance, this could report a death and reject the calls and then do nothing, with no signal anywhere that the restart half was missing; handed the five words that BUILD one, every port can restart. The line an app writes is the same line, with an arrow in front of it.
close() terminates only a host that is KNOWN TO BE QUIET ​
This used to terminate unconditionally, and the reasoning was that a dedicated worker belongs to the tab that made it, so killing it is free and killing before a restart is what stops a second writer.
The second half was never load-bearing -- ADR-0075's writer token is what stops a second writer, and it stops one that survived a failed kill too -- and the first half is measurably false. Ending a worker that has a readwrite and a readonly transaction in flight can leave its IndexedDB database PERMANENTLY unable to run any transaction on WebKit, recovered by no reload and no new tab, with deleteDatabase blocked (work/notes/findings/webkit-does-not-abort-a-terminated-workers-indexeddb-transaction.md). A host killed for being unresponsive is, by construction, a host that was busy.
So a kill now needs a reason to believe the host is idle, and the port establishes that by asking it to stop before letting go. When it cannot -- a host that answers nothing is exactly the case -- the worker is ABANDONED rather than killed. That leaks a thread, and the leak is bounded by a fact worth stating: a dedicated worker cannot outlive the document that created it, so the cost is one idle worker until the page goes away, against a local database the user cannot get back.
Parameters ​
create ​
() => Worker