-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Custom equality for watch #13242
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
Comments
I could claim that |
Thanks for bringing this up — I've run into the same limitation when trying to combine structural equality with abortable side-effects using The core issue is that the A few workarounds I've tried:
Totally agree that being able to pass a custom If the concern is backward compatibility, perhaps this could be added as an opt-in feature — something like: watch(source, callback, {
equals: (a, b) => isEqual(a, b),
}) This would preserve existing behavior but give advanced users more control. Thanks again for documenting the issue so clearly — following this one closely! |
I know that custom equality for watch has been brought up few times in the past and the general consensus is that it should just be done inside watch callback. But if one wants to use
onCleanup
, this approach does not work well:onCleanup
of previous run is called even if custom equality determines that nothing should be done.I have the following use case, where I want to reactivity fetch data from the server if and only if fetch URL changes (slightly abridged):
The issue is that
params
andquery
are objects and even if their contents do not change really, but they are just recreated (e.g., inside a template with something like:query="{id: value}"
), the watch triggers. Fine, I do deep comparison withisEqual
but the issue is thatonCleanup
aborts the previous request even ifisEqual
determines that nothing has changed and nothing should be done (so existing in-flight non-aborted-yet fetch should continue to run and finish and update doc ref).Now, there are few approaches I could take, like doing
JSON.stringify
on query and params to get a simple string as a reactive value so that existing strict equality would work correctly.Or I could move
abortController
to external-to-the-watch variable and be smart to call it only if params change. But then I also have to call it when any outside scope gets destroyed and I have to keep track of that.I think the easiest and cleanest and more readable would be if I could provide my own equality function. Something like:
Then it would be easy and clear to anyone reading the code, what is the idea here.
The text was updated successfully, but these errors were encountered: