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

refactor(hmr): simplify fetchUpdate #9881

Merged
merged 8 commits into from Aug 29, 2022

Conversation

sapphi-red
Copy link
Member

Description

Refactored fetchUpdate function in client/client.ts.

Splitted from #9773.

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added the p1-chore Doesn't change code behavior (priority) label Aug 28, 2022
@sapphi-red sapphi-red marked this pull request as draft August 28, 2022 12:44
Comment on lines -404 to -417
const modulesToUpdate = new Set<string>()
if (isSelfUpdate) {
// self update - only update self
modulesToUpdate.add(path)
} else {
// dep update
for (const { deps } of mod.callbacks) {
deps.forEach((dep) => {
if (acceptedPath === dep) {
modulesToUpdate.add(dep)
}
})
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

modulesToUpdate was always new Set([]) or new Set([acceptedPath]).

Because:

  • in if (isSelfUpdate) block
    • path === acceptedPath is true so it's calling modulesToUpdate.add(acceptedPath)
  • in else block
    • it's only calling modulesToUpdate.add(dep) when acceptedPath === dep. So it's calling modulesToUpdate.add(acceptedPath).

@sapphi-red
Copy link
Member Author

For review, it might be easier to see each commit, because I made each commit equivalent before and after the change.

@sapphi-red sapphi-red marked this pull request as ready for review August 28, 2022 13:07
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

Thanks for the step-by-step commits @sapphi-red, that helped a lot!

Some historic bits for reference, the current implementation needed the Set when it was first created. It was first changed here, and then again here. After that second change, this part of the codebase remained untouched.

@patak-dev patak-dev merged commit 8872aba into vitejs:main Aug 29, 2022
@bluwy
Copy link
Member

bluwy commented Aug 29, 2022

Just read through the commits, and it's beautiful. Reminds me of the days we do equation simplification in math classes 😄

@sapphi-red sapphi-red deleted the refactor/hmr-fetch-update branch August 29, 2022 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p1-chore Doesn't change code behavior (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants