Skip to main content

flywave-mapview.workerloader.startworker

Home > @flywave/flywave-mapview > WorkerLoader > startWorker

WorkerLoader.startWorker() method

Starts 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 Blobs. By default browsers, allow 'blob:' for workers, but this may change.

Following snippet setups CSP, so workers can be started from blob urls:

Tested on: * Chrome 67 / Linux, Window, OSX, Android * Firefox 60 / Linux, Windows, OSX * Edge 41 / Windows * Safari 11 / OSX * Samsung Internet 7.2

See * https://benohead.com/cross-domain-cross-browser-web-workers/ * MapBox * https://stackoverflow.com/questions/21913673/execute-web-worker-from-different-origin * https://github.com/mapbox/mapbox-gl-js/issues/2658 * https://github.com/mapbox/mapbox-gl-js/issues/559 * https://github.com/mapbox/mapbox-gl-js/issues/6058

Findings:

* Chrome reports CSP by exception when constructing [[Worker]] instance. * Firefox reports CSP errors when loading in first event: https://bugzilla.mozilla.org/show\_bug.cgi?id=1241888 * Firefox 62, Chrome 67 obeys <meta http-equiv="Content-Security-Policy"> with worker-src blob: but doesn't obey worker-src URL when used * Chrome 67 doesn't obey CSP worker-src URL despite it's documented as supported (https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/worker-src)

Signature:

static startWorker(scriptUrl: string, timeout?: number): Promise<Worker>;

Parameters

Parameter

Type

Description

scriptUrl

string

web worker script URL

timeout

number

(Optional) timeout in milliseconds, in which worker should set initial message (default 10 seconds)

Returns:



Promise&lt;Worker&gt;