react-native-worklets-core icon indicating copy to clipboard operation
react-native-worklets-core copied to clipboard

[Suggestion] Can proxying be optional?

Open bglgwyng opened this issue 2 years ago • 6 comments

Thank you for creating such a great library! Recently, while working on my project, I realized that using proxied objects is not always the best approach. I understand that there are pros and cons to using proxied objects versus plain objects. However, in my case, using proxied objects led to more mistakes and took me a long time to figure out why. For example, when using a proxied array, it failed the Array.isArray test, and I couldn't use functions like Object.entries or Object.keys on it. This made it more challenging to write code than with ordinary functions.

I acknowledge that there may be many cases where proxying is useful, which I may not have encountered yet. However, I was wondering if it would be possible to make proxying optional in your library. I am not familiar with the specifics of your implementation, so I am uncertain if this is feasible or not. Nevertheless, if it is possible, having the option to turn off proxying would be beneficial.

bglgwyng avatar Apr 26 '23 07:04 bglgwyng

Are you talking about Shared Values in Worklets? Those are Proxies?

mrousavy avatar Jun 26 '23 08:06 mrousavy

Yes. I was referring to this change https://github.com/chrfalch/react-native-worklets/commit/54ea83c57dcf1f8a8eecdfa1ec27eb1beb69e991. Actually, I noticed that it uses Proxy from your response here(see the last paragraph).

bglgwyng avatar Jun 27 '23 04:06 bglgwyng

I've also had this issue recently when trying to use .slice on a shared value array. Were you ever able to figure out how to get around this or how to extend the proxied object with implementations you needed?

wuguishifu avatar May 12 '24 01:05 wuguishifu

You can reconstruct an array and use it in the JS thread. I agree that it's not satisfying.

bglgwyng avatar May 12 '24 04:05 bglgwyng

Thanks for the suggestion. I tried using Object.values(array) but it was running a bit too slow for my frame processor so I ended up handling all of my skia logic natively instead of trying to pass the values back to JS land and use a shared value.

wuguishifu avatar May 13 '24 07:05 wuguishifu

Ah yes well you can copy the values using spread, but other than that I personally always prefer to use the native Array / Object proxies. They have locking mechanism, which is useful for Skia Frame Processors for example.

mrousavy avatar May 13 '24 09:05 mrousavy