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(unmountAtom): use atomStateMap.get instead of ensureAtomState #3010

Conversation

nightohl
Copy link
Contributor

@nightohl nightohl commented Mar 2, 2025

Related Bug Reports or Discussions

Fixes #

Summary

When unmounting an atom, we don't need to create atom state since we only care about already mounted atoms.
but ensureAtomState make new atom whenever there isn't atom.
I thought this process is kinda unnecessary

  • Prevents unnecessary atom state creation
  • Makes the unmounting logic clearer
  • Returns early if atom state doesn't exist

Check List

  • pnpm run fix for formatting and linting code and docs

Sorry, something went wrong.

When unmounting an atom, we don't need to ensure or create atom state
since we only care about already mounted atoms. This change:

- Prevents unnecessary atom state creation
- Makes the unmounting logic clearer
- Returns early if atom state doesn't exist

BREAKING CHANGE: None
Copy link

vercel bot commented Mar 2, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 2, 2025 2:17am

Copy link

codesandbox-ci bot commented Mar 2, 2025

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.

@@ -838,7 +838,11 @@ const buildStore = (
const unmountAtom =
buildingBlockFunctions[8] ||
((atom) => {
const atomState = ensureAtomState(atom)
Copy link
Member

Choose a reason for hiding this comment

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

I think we want to keep this, as this is the single point of accessing the atomStateMap. There's possibility to extend it, and I don't feel like adding more lines for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dai-shi
Got it! I thought it could be intentional too.

Maybe it's a small overhead, but I thought it was causing unnecessary work right now, and as a beginner, I couldn't determine if atomOnInit hooks might cause any unintended problems or not.

That seems pretty reasonable if it's just for the purpose of ensuring and adhering to the converntions.

Thank you for your reply!

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, for the normal use case, atom state should have been created in advance.

@dai-shi dai-shi closed this Mar 2, 2025
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.

None yet

2 participants