Master de II. ULL. 1er cuatrimestre. 2020/2021
1
2
➜ jsday-canarias-2019-examples-multithreading git:(master) ✗ pwd
/Users/casianorodriguezleon/campus-virtual/2021/learning/jsday-canarias-2019-examples-multithreading
1
2
3
4
5
6
7
8
9
10
/**
* Create a new worker from JavaScript code instead of a URL
* @param {string} workerCode The raw JavaScript code for the worker
* @returns Returns the created worker
*/
function createWorker(workerCode) {
const blob = new Blob([workerCode], {type: 'application/javascript'});
const workerURL = URL.createObjectURL(blob);
return new Worker(workerURL);
}
The postMessage() method of the Worker interface sends a message to the worker’s inner scope. This accepts a single parameter, which is the data to send to the worker. The data may be any value or JavaScript object handled by the structured clone algorithm, which includes cyclical references.
The Worker can send back information to the thread that spawned it using the DedicatedWorkerGlobalScope.postMessage method.
1
worker.postMessage(message, [transfer]);
message
The object to deliver to the worker; this will be in the data field in the event delivered to the DedicatedWorkerGlobalScope.onmessage handler. This may be any value or JavaScript object handled by the structured clone algorithm, which includes cyclical references.
If the message parameter is not provided, a TypeError will be thrown. If the data to be passed to the worker is unimportant, null or undefined can be passed explicitly.
transfer Optional
An optional array of Transferable objects to transfer ownership of. If the ownership of an object is transferred, it becomes unusable (neutered) in the context it was sent from and becomes available only to the worker it was sent to.
Transferable objects are instances of classes like ArrayBuffer, MessagePort or ImageBitmap objects that can be transferred. null is not an acceptable value for transfer.
Void.
See the folder nodejs-threads inside the repo ULL-MII-SYTWS-1920/jsday-canarias-2019-examples-multithreading
/Users/casianorodriguezleon/campus-virtual/2021/learning/jsday-canarias-2019-examples-multithreading