Static
dependencyStatic
directlyStatic
sourceStatic
fetchStatic
startStarts worker by first attempting load from scriptUrl
using native Worker
constructor.
Then waits (using [[waitWorkerInitialized]]) for first message that indicates successful
initialization.
If scriptUrl
's origin is different than baseUrl
, then in case of error falls back to
[[startWorkerBlob]].
We must resolve/reject promise at some time, so it is expected that any sane application will
be able to load worker code in some amount of time.
By default, this method timeouts after 10 seconds (configurable using timeout
argument).
This method is needed as browsers in general forbid to load worker if it's not on 'same origin' regardless of Content-Security-Policy.
For blob-based fallback work, one need to ensure that Content Security Policy (CSP) allows
loading web worker code from Blob
s. By default browsers, allow 'blob:' for workers, but
this may change.
Following snippet setups CSP, so workers can be started from blob urls:
<head>
<meta http-equiv="Content-Security-Policy" content="child-src blob:">
</head>
Tested on:
See
Findings:
<meta http-equiv="Content-Security-Policy">
with
worker-src blob:
but doesn't obey worker-src URL
when usedworker-src URL
despite it's documented as supported
(https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/worker-src)web worker script URL
timeout in milliseconds, in which worker should set initial message (default 10 seconds)
Static
startStart worker "via blob" by first loading worker script code with [[fetch]], creating Blob
and attempting to start worker from blob url. Waits (using [[waitWorkerInitialized]]) for
successful worker start.
web worker script URL
Static
startStatic
waitWaits for successful Web Worker start.
Expects that worker script sends initial message.
If first event is message
then assumes that worker has been loaded sussesfully and promise
resolves to worker
object passed as argument.
If first event is 'error', then it is assumed that worker failed to load and promise is rejected.
(NOTE: The initial 'message' - if received - is immediately replayed using worker's
dispatchEvent
, so application code can also consume it as confirmation of successful
worker initialization.
We must resolve/reject promise at some time, so it is expected that any sane application will be able to load worker code in some amount of time.
[[Worker]] instance to be checked
timeout in milliseconds, in which worker should set initial message
Promise
that resolves to worker
on success
Generated using TypeDoc
Set of
Worker
loading and initialization helpers: