-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
watch onInvalidate callback is triggered many times #5151
Comments
This is a tricky one, feels like a definition gap. If we change the behavior now, we risk breaking existing code. We might still want to if we decide the proposed behavior was the originally intended one anyway. For now, one could work around this like so: const count = ref(0);
watch(count, (value, oldValue, onInvalidate) => {
if (value > 2) {
return;
}
const controller = new AbortController();
// fetch would be called only 2 times
console.log('fetch!');
fetch('/test', { signal: controller.signal });
let invalidated = false
onInvalidate(() => {
if (invalidated) return
invalidated = true
console.log('aborted!');
controller.abort();
});
}); |
Thanks for work around and answer! |
Another work around BTW, this behavior is werid. I'm using vue 3, and thought const count = ref(0);
watch(count, (value, oldValue, onInvalidate) => {
// ------ Add this line ------
onInvalidate(()=>{})
// ---------------------------
if (value > 2) {
return;
}
const controller = new AbortController();
// fetch would be called only 2 times
console.log('fetch!');
fetch('/test', { signal: controller.signal });
onInvalidate(() => {
// but onInvalidate would be calld every time watch triggers
console.log('aborted!');
controller.abort();
});
}); |
Version
3.2.26
Reproduction link
sfc.vuejs.org/
Steps to reproduce
What is expected?
Callback that we pass to onInvalidate calls only once. In reproduction repo I expect that
abort
would be called the same times asfetch
What is actually happening?
Callback that we pass to onInvalidate is called multiple times
In the screenshot fetch was called 2 times, but abort was called 18 times
There is enchancement issue that would fix this behaviour, I think.
#3341
The text was updated successfully, but these errors were encountered: