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

fix: onMount setSelf does not notify listeners #2815

Merged
merged 2 commits into from
Nov 17, 2024

Conversation

dmaskasky
Copy link
Collaborator

@dmaskasky dmaskasky commented Nov 14, 2024

Related Bug Reports or Discussions

onMount setSelf does not notify listeners (#2810)

  • adds failing test: when changing the atom value with onMount setSelf, subscribers are not called
  • fixes failing test

Why aren't listeners called when calling setAtom in onMount?

Because the listener is added only after flushPending is called.

Fix: swap the order of flush pending and listeners.add(listener).

Why aren't listeners called when calling setAtom in onUnmount?

Because the listener is deleted before flushPending is called.

There's a logic cycle here:

  1. the listener is deleted before calling unmountAtom, because unmountAtom only processes if atomState.m.l.size === 0
  2. unmountAtom adds the onUnmount pending function
  3. flushPending calls onUnmount pending function
  4. setAtom fires in onUnmount pending function, which updates the atom
  5. subscriber doesn't fire because it was removed in 1

👉 NOTE: This is expected behavior.

Check List

  • pnpm run prettier for formatting code and docs

Copy link

vercel bot commented Nov 14, 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 Nov 17, 2024 9:55am

Copy link

codesandbox-ci bot commented Nov 14, 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.

@dmaskasky dmaskasky changed the base branch from main to fix/flush-pending-async November 14, 2024 00:18
Copy link

github-actions bot commented Nov 14, 2024

LiveCodes Preview in LiveCodes

Latest commit: 94b0b83
Last updated: Nov 17, 2024 9:54am (UTC)

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

See documentations for usage instructions.

@dmaskasky dmaskasky changed the title Fix/onmount setatom subscribe Fix: onMount setSelf does not notify listeners Nov 14, 2024
@dmaskasky dmaskasky changed the title Fix: onMount setSelf does not notify listeners fix/onmount setatom subscribe Nov 14, 2024
@dmaskasky dmaskasky changed the title fix/onmount setatom subscribe fix/onmount setom subscribe Nov 14, 2024
@dmaskasky dmaskasky changed the title fix/onmount setom subscribe fix/onmount setAtom subscribe Nov 14, 2024
@dmaskasky dmaskasky changed the title fix/onmount setAtom subscribe fix: onMount setSelf does not notify listeners Nov 14, 2024
@dai-shi dai-shi deleted the branch pmndrs:main November 14, 2024 02:20
@dai-shi dai-shi closed this Nov 14, 2024
@dai-shi dai-shi reopened this Nov 14, 2024
@dai-shi dai-shi changed the base branch from fix/flush-pending-async to main November 14, 2024 02:30
@dmaskasky dmaskasky force-pushed the fix/onmount-setatom-subscribe branch from 6d4221c to 371a9bf Compare November 14, 2024 18:42
@dmaskasky dmaskasky requested a review from dai-shi November 14, 2024 18:52
@dmaskasky dmaskasky force-pushed the fix/onmount-setatom-subscribe branch from 371a9bf to 57f6583 Compare November 15, 2024 23:22
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.

Very good. This is something I missed it.
The test is reasonable and the fix is surprisingly simple.

@dai-shi dai-shi added this to the v2.10.3 milestone Nov 17, 2024
@dai-shi dai-shi merged commit c41a6de into pmndrs:main Nov 17, 2024
40 checks passed
@dmaskasky dmaskasky deleted the fix/onmount-setatom-subscribe branch November 17, 2024 10:09
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