Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any standard mechanism for detecting if a JavaScript is executing as a WebWorker?

A WebWorker executes with a scope completely separate from the 'window' context of traditional JavaScript. Is there a standard way for a script to determine if it is, itself, being executed as a WebWorker?

The first 'hack' I can think of would be to detect if there is a 'window' property in the scope of the worker. If absent, this might mean we are executing as a WebWorker.

Additional options would be to detect properties not present in a standard 'window' context. For Chrome 14, this list currently includes:

FileReaderSync
FileException
WorkerLocation
importScripts
openDatabaseSync
webkitRequestFileSystemSync
webkitResolveLocalFileSystemSyncURL

Detecting WorkerLocation seems like a viable candidate, but this still feels a bit hackish. Is there a better way?

EDIT: Here is the JSFiddle I used to determine properties present in the executing WebWorker that are now in 'window'.

like image 882
Matt Avatar asked Sep 21 '11 22:09

Matt


People also ask

Which JavaScript method is used to instantiate a web worker?

You need to use the postMessage() method in the onmessage event handler in worker. js : // src/worker. js onmessage = e => { const message = e.

Which is a JavaScript running in the background without affecting the performance of the page?

A web worker is a JavaScript running in the background, without affecting the performance of the page.

How do we check the availability of web workers in the browser?

We use them in the following way: we check for the availability of the Worker() constructor in the browser, and if it is available, we instantiate a worker object, with the script URL as the argument. This script will be executed on a separate thread.

What is importScripts?

importScripts() tells your main service worker script to pause its current execution, download additional code from a given URL, and run it to completion in the current global scope. Once that's done, the main service worker script resumes execution.


2 Answers

The spec says:

The DOM APIs (Node objects, Document objects, etc) are not available to workers in this version of this specification.

This suggests checking for the absence of document is a good way to check you're in a worker. Alternatively you could try checking for the presence of WorkerGlobalScope?

like image 195
AshleysBrain Avatar answered Oct 14 '22 07:10

AshleysBrain


Although post a bit old, adding a couple of generic alternatives What is used in Asynchronous.js library (a library for generic handling of asynchronous/parallel processes, author) is the following:

// other declarations here
,isNode = ("undefined" !== typeof global) && ('[object global]' === Object.prototype.toString.call(global))
// http://nodejs.org/docs/latest/api/all.html#all_cluster
,isNodeProcess = isNode && !!process.env.NODE_UNIQUE_ID
,isWebWorker = !isNode && ('undefined' !== typeof WorkerGlobalScope) && ("function" === typeof importScripts) && (navigator instanceof WorkerNavigator)
,isBrowser = !isNode && !isWebWorker && ("undefined" !== typeof navigator) && ("undefined" !== typeof document)
,isBrowserWindow = isBrowser && !!window.opener
,isAMD = "function" === typeof( define ) && define.amd
,supportsMultiThread = isNode || "function" === typeof Worker
,isThread = isNodeProcess || isWebWorker
// rest declarations here..
like image 44
Nikos M. Avatar answered Oct 14 '22 08:10

Nikos M.