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

feat(core): add atom hooks for atom mount, unmount and improve dev store #2895

Merged
merged 3 commits into from
Jan 3, 2025

Conversation

dmaskasky
Copy link
Collaborator

@dmaskasky dmaskasky commented Dec 30, 2024

Summary

Adds hooks u and h to atomState that are called in-place immediately after atom mount changes or the atomState has updated.

Check List

  • pnpm run prettier for formatting code and docs

@dmaskasky dmaskasky requested a review from dai-shi December 30, 2024 07:19
Copy link

vercel bot commented Dec 30, 2024

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 Jan 3, 2025 0:54am

@dmaskasky dmaskasky marked this pull request as draft December 30, 2024 07:19
Copy link

codesandbox-ci bot commented Dec 30, 2024

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.

Copy link

pkg-pr-new bot commented Dec 30, 2024

Open in Stackblitz

More templates

npm i https://pkg.pr.new/jotai@2895

commit: fd8dcb2

Copy link

github-actions bot commented Dec 30, 2024

LiveCodes Preview in LiveCodes

Latest commit: fd8dcb2
Last updated: Jan 3, 2025 12:53am (UTC)

Playground Link
React demo https://livecodes.io?x=id/GSM5H36UU

See documentations for usage instructions.

src/vanilla/store.ts Outdated Show resolved Hide resolved
Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks for the suggestion. This is much cleaner.

I'll merge this when we are ready to release another patch.

@dai-shi dai-shi marked this pull request as ready for review December 30, 2024 23:59
@dai-shi
Copy link
Member

dai-shi commented Dec 30, 2024

/ecosystem-ci run

@dai-shi dai-shi changed the title feat: add hook for atom mount and unmount feat(core): add hook for atom mount and unmount and improve dev store Dec 31, 2024
Copy link

Ecosystem CI Output

---- Jotai Ecosystem CI Results ----
{
  "bunshi": "PASS",
  "jotai-devtools": "PASS",
  "jotai-effect": "PASS",
  "jotai-scope": "PASS"
}

@@ -100,6 +100,8 @@ type AtomState<Value = AnyValue> = {
n: number
/** Object to store mounted state of the atom. */
m?: Mounted // only available if the atom is mounted
/** Listener to notify when the atom is mounted or unmounted. */
h?: () => void
Copy link
Member

Choose a reason for hiding this comment

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

If you want to change this with another character, we can reconsider.

Copy link
Collaborator Author

@dmaskasky dmaskasky Jan 2, 2025

Choose a reason for hiding this comment

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

How about

h?: (action: 'm' | 'u', batch: Batch) => void

Copy link
Member

Choose a reason for hiding this comment

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

'm' | 'u' doesn't look too bad even though it increases the bundle size. I'm hesitant to expose batch here, and looking for an alternative.

Copy link
Member

Choose a reason for hiding this comment

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

We may change 'h' in the future, but as this is considered internal, it's fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm hesitant to expose batch here, and looking for an alternative.

Its pretty central to syncBatch scheduling.

Copy link
Member

Choose a reason for hiding this comment

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

I'm working on it, and not sure if it's successful, but I feel like reverting this PR... Will see.

Copy link
Member

Choose a reason for hiding this comment

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

Will see.

It failed.

@dmaskasky dmaskasky changed the title feat(core): add hook for atom mount and unmount and improve dev store feat(core): add atom hooks for atom mount, unmount, and update and improve dev store Jan 2, 2025
@dmaskasky dmaskasky mentioned this pull request Jan 2, 2025
1 task
src/vanilla/store.ts Outdated Show resolved Hide resolved
@@ -100,6 +100,8 @@ type AtomState<Value = AnyValue> = {
n: number
/** Object to store mounted state of the atom. */
m?: Mounted // only available if the atom is mounted
/** Listener to notify when the atom is mounted or unmounted. */
h?: () => void
Copy link
Member

Choose a reason for hiding this comment

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

We may change 'h' in the future, but as this is considered internal, it's fine.

src/vanilla/store.ts Outdated Show resolved Hide resolved
@dai-shi dai-shi changed the title feat(core): add atom hooks for atom mount, unmount, and update and improve dev store feat(core): add atom hooks for atom mount, unmount and improve dev store Jan 3, 2025
Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

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

Much cleaner without Proxy hack.

@dai-shi dai-shi added this to the v2.11.1 milestone Jan 3, 2025
@dai-shi dai-shi merged commit c1f9c3d into pmndrs:main Jan 3, 2025
42 checks passed
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.

2 participants