The event is automatically silenced when the web worker exits

I use a web performer in my project: one simple question is when we complete an event, the web worker is automatically untied from the worker or not:

worker = new Worker("scripts/workers/clockTime-worker.js");
worker.addEventListener("message", onMessage);

function onMessage(evt) {
   //------
}

worker.terminate();

- message event is automatically disconnected from the worker or not?

+4
source share
1 answer

No, the message event is still bound to the work object.

You can remove the event using the removeEventListener () property of the worker object, or since you probably did with the worker, set the work object to null to make sure it has garbage collected.

Here is a test you can use to provide this on your platform:

var worker = new Worker( URL.createObjectURL( new Blob([''])));
var e = new MessageEvent('message');

function test() { console.log('Event fired') }

worker.addEventListener('message', test);
worker.dispatchEvent(e); //Event fired

worker.terminate();
worker.dispatchEvent(e); //Event fired

I get the same results in chrome and firefox.

+1
source

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


All Articles