Skip to main content

flywave-mapview.workerloader

Home > @flywave/flywave-mapview > WorkerLoader

WorkerLoader class

Set of Worker loading and initialization helpers: - starting Worker from URL with fallback to XHR+blob WorkerLoader.startWorker() - waiting for proper worker initialization, see WorkerLoader.waitWorkerInitialized()

Signature:

export declare class WorkerLoader 

Properties

Property

Modifiers

Type

Description

dependencyUrlMapping

static

Record<string, string>

directlyFallbackToBlobBasedLoading

static

boolean

sourceLoaderCache

static

Map<string, Promise<string>>

Methods

Method

Modifiers

Description

fetchScriptSourceToBlobUrl(scriptUrl)

static

Fetch script source as Blob url.

Reuses results, if there are many simultaneous requests.

startWorker(scriptUrl, timeout)

static

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)

startWorkerBlob(scriptUrl, timeout)

static

Start 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.

startWorkerImmediately(scriptUrl, timeout)

static

Start worker, loading it immediately from scriptUrl. Waits (using [[waitWorkerInitialized]]) for successful worker start.

waitWorkerInitialized(worker, timeout)

static

Waits 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.