dai-shi / react-hooks-worker

React custom hooks for web workers
MIT License
709 stars 17 forks source link

Is Transferable Object supported? #38

Closed Wolf-Tungsten closed 2 years ago

Wolf-Tungsten commented 2 years ago

Transferable Object: https://developer.mozilla.org/en-US/docs/Glossary/Transferable_objects

After reading the source code, I don't think the transferable object is currently supported.

dai-shi commented 2 years ago

Good point. Right, it doesn't expose transferList. I wonder how to design api for this. We'd want it for both directions, right?

Wolf-Tungsten commented 2 years ago

As I understand it, the main difficulty at the moment is that it is not possible to determine whether the returned results need to be TRANSFERRED. But fortunately in this context we treat the worker task as a function, which means that even if we TRANSFER the result every time, it won't cause any problems.

My understanding of transferable objects and react hooks is not deep enough, so I'm not quite sure if the above idea will be a problem, so please correct me.

dai-shi commented 2 years ago

Yeah, it should be possible. I'm thinking about the api. One idea is to accept an optional getTransferList(...) function in useWorker and exposeWorker.

doelgonzo commented 2 years ago

Seems like this would be a nice add. I am doing api fetches in my workers, and the transfer memory usage is higher, not to mention I transfer a TON of data. This would definitely help. https://joji.me/en-us/blog/performance-issue-of-using-massive-transferable-objects-in-web-worker/ https://developers.redhat.com/blog/2014/05/20/communicating-large-objects-with-web-workers-in-javascript

I think having a getTransferList would work, then from the react side we'd just listen to whenever that transferlist changes, no? Just spitballing ideas here

dai-shi commented 2 years ago

Do you need to change the transferlist from the react side? I will draft a PR later.

dai-shi commented 2 years ago

Can you try #42? https://ci.codesandbox.io/status/dai-shi/react-hooks-worker/pr/42 ☝️ Please find "Local install instructions"

doelgonzo commented 2 years ago

I'll try it over the weekend. I need to listen to the transfer list from the react side, not write to it. So like, it would be a "when completed, update these things" then have React, err... react to it