Messages sent through employee.postMessage () in a queue?

After creating the worker, I can send him messages via postMessage. For example:

var worker = new Worker('helper.js');
worker.postMessage({...});

Inside helper.js, the employee needs to add a listener using onmessage = function (event) { ... };

My question is: if one or more messages are sent to the worker while the working script is still loading, is it guaranteed to be received in the queue and delivery in the end, or is it possible that they might get lost?

+4
source share
2 answers

Spectrum http://www.w3.org/TR/2015/WD-workers-20150924/#communicating-with-a-dedicated-worker says

The implicit MessagePort used by dedicated workers has a port message queue implicitly enabled when it was created.

Besides

+2

, script , onmessage ( , Chrome v56).

:

worker.js:

setTimeout(function() {
  onmessage = function() {
    console.log('received message');
  }
}, 0);

Main- script.js

var worker = new Worker('worker.js');
worker.postMessage('first message'); // this won't go through
setTimeout(function() {
  worker.postMessage('second message'); // this will
}, 1000);

onmessage .

plunkr: http://embed.plnkr.co/G54gk9Cz6XhZ3E6ZB3Nf/

importScripts , , , onmessage, .

+2

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


All Articles