Reputation: 2115
i'm trying to pass data from webworker to main context but i can't make it work. My code is:
alg.OnProcessEnd = (arg: { array: Uint32Array, max: number }) => {
(<any>self).postMessage(arg, [arg.array]);
};
but chrome claims that "TypeError: Failed to execute 'postMessage' on 'WorkerGlobalScope': Value at index 0 does not have a transferable type."
so my question is: How to send properly data from webworker where I can't make copy (image data array).
Upvotes: 2
Views: 3215
Reputation: 18252
The second parameter should be the buffer not the array itself
alg.OnProcessEnd = (arg: { array: Uint32Array, max: number }) => {
(<any>self).postMessage(arg, [arg.array.buffer]);
};
Upvotes: 0
Reputation: 8383
You need to serialize the Uint32Array
first, transfer it over, then deserialize it.
One way to do that is by transforming it into a JSON array by doing:
var jsonArray = '[' + Array.prototype.join.apply(arg.array, [',']) + ']';
Then you can transfer that over and recreate the Uint32Array
object by doing:
var uint32Array = new Uint32Array( JSON.parse(jsonArray) );
Edit:
As transferable object: You were missing the buffer
property.
(<any>self).postMessage(arg, [arg.array.buffer]);
Upvotes: 4