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

Shared Value array proxy doesn't support .slice

Open wuguishifu opened this issue 1 year ago • 1 comments

I'm trying to take the last N elements of a Shared Value array, but calling .slice throws an error.

Reproducable code:

const array = useSharedValue<number[]>([1, 2, 3]);
array.value.slice(1); // throws "Error: array.value.slice is not a function (it is undefined)"

I believe this is because the .slice function was not implemented in #36. Is there any way for me to either extend this functionality into the proxy in my code base or to use something else?

For additional context, I'm trying to add data to this array from inside a Vision Camera Skia frame processor, and then also in the frame processor I'm rendering the last 1000 data points in a graph. To get around using .slice, I've been calling Object.values(array.value) and then running .slice on the output array. However, for large arrays (1000+ items), this because very inefficient (20+ ms). I'm also open to suggestions for alternate strategies for doing this if it would be too difficult to extend the functionality.

Thanks!

wuguishifu avatar May 12 '24 01:05 wuguishifu

Yea we could just add slice(..) to our array proxy.

mrousavy avatar May 13 '24 10:05 mrousavy