-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 optimistic rendering when deleting multiple records #9198
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This pull request modifies the optimistic rendering behavior when deleting multiple records in the Apollo cache management system.
- Replaces array splice operations with filter in
triggerUpdateRecordOptimisticEffectByBatch.ts
for more predictable record removal - Changes record existence check from
findIndex > -1
tofind + isDefined
for clearer intent - Improves edge case handling when removing records from the Apollo cache edges array
- Maintains consistent behavior with single record deletion through
triggerUpdateRecordOptimisticEffect.ts
The changes make the code more functional and less prone to mutation-related bugs, though with potential performance tradeoffs for large datasets.
1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile
rootQueryNextEdges = rootQueryNextEdges.filter( | ||
(cachedEdge) => | ||
readField('id', cachedEdge.node) !== updatedRecord.id, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: filter creates a new array on every iteration - for large deletions this could impact performance. Consider collecting IDs to remove first, then filtering once
@@ -105,7 +103,10 @@ export const triggerUpdateRecordOptimisticEffectByBatch = ({ | |||
} | |||
|
|||
if (updatedRecordShouldBeRemovedFromRootQueryEdges) { | |||
rootQueryNextEdges.splice(updatedRecordIndexInRootQueryEdges, 1); | |||
rootQueryNextEdges = rootQueryNextEdges.filter( | |||
(cachedEdge) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
splicing was causing an issue here because it modified the indexes of elements in rootQueryNextEdges, which was problematic when there were multiple iterations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Fixes https://discord.com/channels/1130383047699738754/1319676302944370730/1319676302944370730