Skip to content
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

Prevent optimistic cache evictions from evicting non-optimistic data. #8829

Merged
merged 3 commits into from
Sep 27, 2021

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Sep 23, 2021

After thinking further about the discussion in #7321, I believe the inability to roll back optimistic evictions is simply a bug that we can fix, rather than something the developer should have to worry about (e.g. by passing optimistic: true, as I previously proposed in #7321 (comment)).

Normally, cache.optimisticData points to the topmost currently active optimistic Layer, and cache.data points to the root/non-optimistic layer. While running an optimistic update function, we temporarily set cache.data to be === cache.optimisticData, in an attempt to confine the optimistic update to one optimistic layer. Eviction was (until now) an exception to this confinement, because optimistic Layer objects still have a chain of layer.parent.parent... objects that eventually terminates at the Root layer. Before now, eviction was blindly following that entire chain, ignoring the current value of cache.data.

This commit uses cache.data as the stopping point for eviction, which has the effect of confining optimistic evictions to the one layer that they should be updating, hopefully solving #7321 without requiring any intervention by the developer. ✨

After thinking further about the discussion in #7321, I believe the
inability to roll back optimistic evictions is simply a bug that we can
fix, rather than something the developer should have to worry about
(e.g. by passing `optimistic: true`, as I previously proposed in
#7321 (comment)).

Normally, `cache.optimisticData` points to the topmost currently active
optimistic `Layer`, and `cache.data` points to the root/non-optimistic
layer. While running an optimistic `update` function, we temporarily set
`cache.data` to be `=== cache.optimisticData`, in an attempt to confine
the optimistic update to one optimistic layer. Eviction was (until now)
an exception to this confinement, because optimistic `Layer` objects
still have a chain of `layer.parent.parent...` objects that eventually
terminates at the `Root` layer. Before now, eviction was blindly
following that entire chain, ignoring the current value of `cache.data`.

This commit uses `cache.data` as the stopping point for eviction, which
has the effect of confining optimistic evictions to the one layer that
they should be updating, hopefully solving #7321 without requiring any
intervention by the developer.
@benjamn benjamn force-pushed the better-confinement-for-optimistic-evictions branch from 12285a7 to 538fc9c Compare September 23, 2021 15:00
Copy link
Member

@hwillson hwillson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome @benjamn - thanks!

@benjamn benjamn merged commit 4f5c78c into main Sep 27, 2021
@benjamn benjamn deleted the better-confinement-for-optimistic-evictions branch September 27, 2021 13:39
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useMutation doesn't roll back optimistic cache.evict
2 participants