Question

To my knowledge you can only pass a string or a object which can be seralize as JSON.

Then what is the best way to pass some image files between workers and main page if I am using WebWorker to download them in the background?

Était-ce utile?

La solution

you can only pass a string or a object which can be seralize as JSON.

Your premise is wrong. You can pass every kind of object that is supported by the structured clone algorithm, this can for example be circular-linked data which cannot be represented as JSON. You also can pass ArrayBuffers, ArrayBufferViews, PixelDataArrays, Blobs etc, all the data types that are not known to JSON.

The second parameter of the postMessage method of Workers does even allow you to transfer binary data (such as ArrayBuffers) directly to the worker - it won't even copy anything, though it will neuter the reference that you have.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top