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

Rewrite updateMany to ensure stable sorting order #2464

Merged
merged 2 commits into from
Jun 29, 2022

Conversation

markerikson
Copy link
Collaborator

This PR:

  • Rewrites the updateMany implementation in sorted_state_adapter to ensure that applying updates to fields unrelated to the sort comparator leaves the sorting order unchanged
  • Adds a test to verify that applying multiple updates to one item by ID are all applied

Fixes #1853 , fixes #2297

There was also a previous PR at #1860 , but I tackled this fresh and ended up fixing both issues with a simpler rewrite.

The core problem is that the old code always did delete state.entities[update.id], and later re-added it. Later, the sorting code does const entities = Object.values(state.entities). Since JS engines mostly use insertion order for key iteration, deleting an re-adding an item to state.entities caused it to sort after other items that have the same comparator values.

Also, deleting the item meant that additional updates for that ID weren't actually applied.

I dropped the somewhat confusing takeUpdatedModel() function, and opted to just directly mutate the existing item instead, only deleting it if the ID changed. (This means there's a very minor limitation that if you're trying to apply multiple updates to the same item in one shot, and one of those updates in the middle changes the ID, the later ones won't apply... but that seems like a rare enough edge case to not worry about here.)

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 75aced4:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration
@examples-query-react/basic Configuration
reduxjs/redux-toolkit Configuration
reduxjs/redux-toolkit Configuration
redux-toolkit-updateMany-test (1.7.1) (forked) Issue #1853

@netlify
Copy link

netlify bot commented Jun 29, 2022

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit 75aced4
🔍 Latest deploy log https://app.netlify.com/sites/redux-starter-kit-docs/deploys/62bbae44e0ab05000977783b
😎 Deploy Preview https://deploy-preview-2464--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant