chrisvander / zustand-computed

A Zustand middleware to create computed states.
MIT License
82 stars 8 forks source link

Feature request: Use selectors / dependencies #2

Closed dabdine closed 1 year ago

dabdine commented 1 year ago

Hello! Thanks for creating this library.

I would to be able to utilize selectors / dependencies as a third argument to computed() to determine when to run the compute function. As an example, in our use case, our store has a cursor x/y coordinate that is updated on mouse move. Thus, each pixel the mouse is moved forces a rerun of the compute function, which can be extremely costly.

If I have time I'll push something. For now, i'm just tossing the idea up.

chrisvander commented 1 year ago

Great thought. Hadn't thought of ignoring computations altogether if accessed values aren't used. Perhaps use of a Proxy object when passing state into the "computeState" function would let it listen to the accessed keys and then use shallow comparisons on those to skip the computation function.

I'll take a look in the morning with fresh eyes!

chrisvander commented 1 year ago

This is addressed by #3 and was released as version 1.3.0. Let me know if it works for your use case! Great suggestion!

dabdine commented 1 year ago

@chrisvander awesome! I'll check it out, thanks!