workerize-loader icon indicating copy to clipboard operation
workerize-loader copied to clipboard

Transferable support

Open qm3ster opened this issue 6 years ago • 6 comments

Hi! Do you think you could include a way to pass Transferable objects correctly? It would be great to be able to return a MessagePort so that the function can return a stream instead of a single value.

qm3ster avatar Aug 25 '18 21:08 qm3ster

Absolutely! We added it to greenlet in only a few bytes, the same solution should work reasonably well here. It would only auto-detect transferables passed directly as arguments, not within nested objects:

https://github.com/developit/greenlet/blob/master/greenlet.js#L25-L29

developit avatar Aug 26 '18 20:08 developit

look forward to it..

cpoopc avatar Apr 07 '19 16:04 cpoopc

Hi I'm a bit late to the party but it seems unclear to me... Are transferable objects supported by workerize or not?

NicolasRannou avatar Mar 24 '20 09:03 NicolasRannou

They are not currently supported.

developit avatar May 07 '20 15:05 developit

Absolutely! We added it to greenlet in only a few bytes, the same solution should work reasonably well here. It would only auto-detect transferables passed directly as arguments, not within nested objects:

https://github.com/developit/greenlet/blob/master/greenlet.js#L25-L29

May I ask why only direct arguments? Why not detecting ArrayBuffers nested inside an object or an array?

shayke avatar May 18 '20 09:05 shayke

I was thinking of a way to specify a loader option for how to extract the transferable from the arguments and return value of a worker method, but I realized that it would be impossible to determine the best option for each worker method with just one loader option. Instead, the approach I took was to call setup functions on both the worker and the main thread side to determine how to extract the transferables for each worker method. I've made a helper module to achieve this.

https://github.com/naoak/workerize-transferable

naoak avatar Dec 15 '20 18:12 naoak