Can you name the web worker?

If you run two web workers who use the same JavaScript file, there is no way to distinguish them in Firefox about:debugging#workers. You get two identical records (listed in the Other Workers section).

For debugging, it would be convenient to assign names to web employees. For example, in Java there is Thread # setName . Is there something equivalent in the JavaScript web workers API?

+4
source share
3 answers

Overall, I would recommend combining both approaches provided by the8472 and Patrick Evans to get a better debugging experience:

,

let worker = new Worker(url);

-

let worker = new Worker(`${url}?name=SomeContext`, { name: 'Some useful context' });

, Firefox 54 . URL- - . , API ( Firefox , ) , :

name: DOMString DedicatedWorkerGlobalScope, , .

, (, about:debugging#workers Firefox), URL- .

, , URL-, . , . , ( ).

0

Worker() , name, :

let workerOne = new Worker(URL, {
  'name' : 'nameOfWorkerOne'
}),
    workerTwo = new Worker(URL, {
  'name' : 'nameOfWorkerTwo'
});

, . , ?

MDN, . , , options .

name , . .

+5

I don’t see anything in the api, but you can put a unique request parameter on the URL to distinguish each.

name="random unique name"; 
new Worker('worker.js?name='+name);

The query string will be displayed in the list.

enter image description here

+1
source

Source: https://habr.com/ru/post/1680634/


All Articles