Allow more control over behavior of reactive effect when triggered #6044
Unanswered
visceroid
asked this question in
Help/Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've come across this use case and couldn't find a way to implement it in Vue:
For async operations, Vue does dependency tracking only for the first call stack, so that's a problem for no.3. I managed to do it by wrapping another watchEffect around the expensive function in the later call stacks (the asynchronous part), using a construct much like how @vueuse implements computedAsync. The next time this inner watchEffect triggers it will stop itself, and trigger the outer watchEffect, setting up async computing through the same process.
However, no.4 is hard to implement, mainly because Vue does tracking along with the actual computing, and there's no way I know of that can maintain dependencies (from previous run) without running the expensive logic again (and touching the dependencies). For debouncing, I need the dependency changes to keep triggering the effect, notifying the debouncer of changes, but delay the rerun until no new change has arrived for some time.
I tried various methods, including letting the triggered runs await on a promise, then resolve the promise in the debounced callback, they all won't work, because it only tracks the first call stack before await, and you either do nothing - lose all dependencies, or dive into the expensiveFn - no debouncing. In short, when the effect triggers, the only way to keep/re-establish the dependencies is to (synchronously) run the expensiveFn again. The closest I can achieve is a fixed delay after the first trigger (ignoring subsequent triggers), then rerun.
I think that when triggering an effect, providing additional control over whether actual computing should run can separate the two aspects of reactive effects: track/trigger and computation better. The current onTrack and onTrigger callbacks are only for debugging and they don't affect how the effect actually runs and tracks dependencies.
Or is this actually doable with current Vue? Would appreciate ideas, thanks.
Beta Was this translation helpful? Give feedback.
All reactions