Reputation: 5542
If I create a SharedArrayBuffer
, and then create a 'view' of said buffer via a TypedArray
, and then send that TypedArray
to a Worker
via postMessage
, is the worker able to access the full data of the TypedArray
?
Upvotes: 0
Views: 35
Reputation: 5542
Yes, the full underlying buffer is shared with the web worker:
<!-- test.html -->
<script>
// Main thread
const sharedBuffer = new SharedArrayBuffer(100);
const fullBuffer = new Uint8Array(sharedBuffer);
fullBuffer.set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); // Fill the start of the buffer with some data
const partOfBuffer = new Uint8Array(sharedBuffer, 5); // Create a view that leaves off the first few numbers
const worker = new Worker('worker.js');
worker.postMessage(partOfBuffer);
</script>
// worker.js
onmessage = function(e) {
const partOfBuffer = e.data;
console.log(new Uint8Array(partOfBuffer.buffer)); // logs the full buffer data - i.e. including the first few numbers
}
Upvotes: 0