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

fix: split second double incrementing bug cache issue #411

Merged

Conversation

reecejohnson
Copy link
Contributor

@reecejohnson reecejohnson commented Jul 6, 2023

There is an issue in the mirror and collect counters where we are managing state between the Apollo cache and internal Apollo reactive variables.

The problem was that we had a network-only call to get a fresh copy of the publication in the responders for both mirror and collect. This updated the Apollo cache with the incremented value a few MS before the pending txs were marked as settled. In the type policy we were combining the count in the publication stats for mirror/collect along with the count of pending txs still pending. This leaves us with a split second where there count is 1 more than it should be before the pending txs are marked as settled.

@desfero and I found two potential fixes:

  1. Settle the txs before calling commit on the responders. Remove stat calculation from the type policy and handle all cache updates in the responders themselves. This removes any deviation from the correct total amount of mirrors/collects value. This is what is shown in this PR.
  2. Use queueMicrotask in the responder to resolve the commit promise early and make the cache updates run so closely together that they cannot be seen as visual changes in the UI. We noted (on my machine) that there was a single MS diff between the two calls meaning that React did not render the first and incorrect update to the UI. This does not rule out the fact this may happen under other conditions.

I am leaving this PR open as a discussion point before we agree on the final fix.

@height
Copy link

height bot commented Jul 6, 2023

This pull request has been linked to 1 task:

💡Tip: Add "Close T-12080" to the pull request title or description, to a commit message, or in a comment to mark this task as "Done" when the pull request is merged.

@changeset-bot
Copy link

changeset-bot bot commented Jul 6, 2023

⚠️ No Changeset found

Latest commit: 99dfc4c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

};
},
},
});
Copy link
Member

@cesarenaldi cesarenaldi Jul 14, 2023

Choose a reason for hiding this comment

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

The challenge with these programmatic cache updates and the reason shy we started moving away from them and using more field policies is that they rely on the cache to be primed with the node we are aiming to update (a Publication in this case). This works in the immediate of a UI but does not work well in another thread that
might not have this exact item in cache (e.g. another tab) or during hard refreshes where the SDK bootstrap by being pure JS logic tries to update cache before any active query is able to fetch the relevant item.

@vercel
Copy link

vercel bot commented Aug 1, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
lens-sdk-example-web-wagmi ✅ Ready (Inspect) Visit Preview Aug 2, 2023 10:07am

@reecejohnson reecejohnson marked this pull request as ready for review August 2, 2023 10:11
@reecejohnson reecejohnson changed the title fix: inital fix for split second double incrementing bug fix: split second double incrementing bug cache issue Aug 2, 2023
@reecejohnson reecejohnson self-assigned this Aug 2, 2023
@reecejohnson reecejohnson merged commit 9a2840a into main Aug 8, 2023
1 check passed
@reecejohnson reecejohnson deleted the T-12080/mirror-collects-counter-is-having-intermediate-step branch August 8, 2023 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants