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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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 14, 2024 6:42pm

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: 371a9bf
Last updated: Nov 14, 2024 6:42pm (UTC)

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

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
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