Keep the user interface reactive.
/* index.js */
// instantiate worker
const worker = new Worker('webWorker.js');
// send a message to the worker
const message = { sayHi: 'Hello worker!' };
worker.postMessage(message);
// listen to messages from the worker
worker.onmessage = event => console.log(event.data.acknowledgment);
/* webWorker.js */
// listen to messages from main thread
onmessage = event => {
console.log(event.data.someData);
// send a message to main thread
const response = { acknowledgment: true };
postMessage(response);
};
Webpack plugin: Use worker-loader to optimize your workers.