Make InProgressMethods immutable so it's safer to use as react state#2703
Merged
robertbrignull merged 2 commits intomainfrom Aug 14, 2023
Merged
Make InProgressMethods immutable so it's safer to use as react state#2703robertbrignull merged 2 commits intomainfrom
robertbrignull merged 2 commits intomainfrom
Conversation
koesie10
approved these changes
Aug 14, 2023
Member
koesie10
left a comment
There was a problem hiding this comment.
LGTM, thanks for making this safe to use in React state
…ress-methods.ts Co-authored-by: Koen Vlaswinkel <koesie10@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A small modification that makes
setPackagedMethodsreturn a new object instead of mutating the existing object. The current code is correct, but I worry the chance of the class accidentally being used incorrectly is a bit too high.The danger of having
InProgressMethodsbe mutable is that we use it as react state, and react tracks state changes only by object identity. This means if a developer do something like:then react would not see that the object was modified and would not trigger a re-render of the component. When the next render happened for whatever other reason, the change in state would be rendered. This leads to bugs and unpredictable visual behaviour.
By making
InProgressMethodsimmutable it means you don't need to know that you must callfromExistingfirst to create a new object, and it'll be harder to accidentally mutate an object being used as react state.It's worth nothing we do also have this problem with other objects and also with arrays. This one stuck out to me because it's a custom object with an interface, so the mutation of lack of it is hidden from the developer. We may also want to look at ways of reducing the chance of this bug in other places. Maybe a CodeQL query?
Checklist
ready-for-doc-reviewlabel there.