Skip to content

Make InProgressMethods immutable so it's safer to use as react state#2703

Merged
robertbrignull merged 2 commits intomainfrom
robertbrignull/InProgressMethods_immutable
Aug 14, 2023
Merged

Make InProgressMethods immutable so it's safer to use as react state#2703
robertbrignull merged 2 commits intomainfrom
robertbrignull/InProgressMethods_immutable

Conversation

@robertbrignull
Copy link
Copy Markdown
Contributor

A small modification that makes setPackagedMethods return 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 InProgressMethods be 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:

setInProgressMethods((oldInProgressMethods) => {
    oldInProgressMethods.setPackagedMethods(packageName, methods);
    return oldInProgressMethods;
);

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 InProgressMethods immutable it means you don't need to know that you must call fromExisting first 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

  • CHANGELOG.md has been updated to incorporate all user visible changes made by this pull request.
  • Issues have been created for any UI or other user-facing changes made by this pull request.
  • [Maintainers only] If this pull request makes user-facing changes that require documentation changes, open a corresponding docs pull request in the github/codeql repo and add the ready-for-doc-review label there.

@robertbrignull robertbrignull requested a review from a team as a code owner August 14, 2023 09:53
Copy link
Copy Markdown
Member

@koesie10 koesie10 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for making this safe to use in React state

Comment thread extensions/ql-vscode/src/data-extensions-editor/shared/in-progress-methods.ts Outdated
…ress-methods.ts

Co-authored-by: Koen Vlaswinkel <koesie10@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@charisk charisk left a comment

Choose a reason for hiding this comment

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

Thanks!

@robertbrignull robertbrignull merged commit 408d38d into main Aug 14, 2023
@robertbrignull robertbrignull deleted the robertbrignull/InProgressMethods_immutable branch August 14, 2023 11:26
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.

3 participants