My goal is to call a specific function onMessage() in the main thread from a web worker. I also need to pass an array of transferable objects such as Uint16Array buffer, Float32Array buffer, along with a boolean object, integer, or string value while posting a message.
For instance:
const cornersArray = new Uint16Array(5000), // should go as Transferable object
trailsArray = new Float32Array(7000); // should go as Transferable object
const state = 1, // integer
quality = 'High', // string
isTracking = true; // boolean
this.Worker.postMessage({
event: 'ShowTrails',
data: {
cornersArray: cornersArray,
trailsArray: trailsArray,
state: state,
quality: quality,
isTracking: isTracking
}
}, [cornersArray.buffer, trailsArray.buffer]);
The problem arises when trying to pass these objects that are not detachable, causing errors in postMessage(). As a beginner in using web workers, I am seeking assistance on how to correctly pass this data and make it work. Any help would be greatly appreciated!
Thanks!