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: remove mutations of the redux state in reducers #2487

Merged
merged 1 commit into from Jan 12, 2024

Conversation

antonbauhofer
Copy link
Member

Summary of changes

This fixes some of our reducers, so that they don't mutate the state.

Context and reason for change

Before this change, there were a few places in our code, where reducers were mutating the state. This is strongly discouraged according to the Redux guide.

How can the changes be tested

  • yarn test:unit
  • check that the app performance is still acceptable, by opening a large file in OpossumUI

Fixes #2412

Comment on lines +605 to +610
paths: [...attributionData.resourcesWithAttributedChildren.paths],
pathsToIndices: {
...attributionData.resourcesWithAttributedChildren.pathsToIndices,
},
attributedChildren: {
...attributionData.resourcesWithAttributedChildren.attributedChildren,
},
Copy link
Member Author

Choose a reason for hiding this comment

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

This is called in several places and not always is every one of these parts mutated. To improve performance, we could just make a shallow copy here and then make copies of the respective parts, where they are mutated.
If we decide to do that, I think we should generally establish that pattern and also fix it at other places, though.

@antonbauhofer antonbauhofer marked this pull request as draft January 11, 2024 14:32
@@ -19,7 +19,7 @@ export function createAppStore() {
// TECH DEBT: we should not be putting sets into the store
// https://redux.js.org/style-guide/#do-not-put-non-serializable-values-in-state-or-actions
serializableCheck: false,
// TECH DEBT: we are mutating the redux state inside reducers
// We set this to false because it significantly reduces performance in development mode.
Copy link
Member Author

Choose a reason for hiding this comment

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

Signed-off-by: Anton Bauhofer <anton.bauhofer@tngtech.com>
@antonbauhofer antonbauhofer marked this pull request as ready for review January 11, 2024 16:15
Copy link
Collaborator

@mstykow mstykow left a comment

Choose a reason for hiding this comment

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

Very nice. Thank you @antonbauhofer! 👍

@mstykow mstykow self-assigned this Jan 11, 2024
@antonbauhofer
Copy link
Member Author

I did a performance test with a large OpossumUI file and the performance does not seem to be significantly impacted by these changes. Also, I loosely checked that no other state mutations, not covered by tests, are present.

@antonbauhofer antonbauhofer merged commit b168c9e into main Jan 12, 2024
5 checks passed
@antonbauhofer antonbauhofer deleted the fix-avoid-state-mutations branch January 12, 2024 08:28
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.

State is mutated inside Redux reducers
2 participants