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

[Bug] atom state not found in flush undefined (introduced in 0.16.11) #517

Closed
sebastienbarre opened this issue Jun 14, 2021 · 8 comments · Fixed by #518
Closed

[Bug] atom state not found in flush undefined (introduced in 0.16.11) #517

sebastienbarre opened this issue Jun 14, 2021 · 8 comments · Fixed by #518

Comments

@sebastienbarre
Copy link

I had been using 0.16.7 for a while, and decided to upgrade my dependencies tonight.
This updated my app to 0.16.11, where Jotai started failing left and right for me with multiple errors popping up in the console:

[Bug] atom state not found in flush undefined

I reverted back to 0.16.7 and started climbing the release versions to isolate the culprit.
0.16.10 still works fine for me... it seems 0.16.11 is problematic in my situation.
Let me know if you need more details.

@dai-shi
Copy link
Member

dai-shi commented Jun 14, 2021

Thanks for reporting.

The fix in v0.16.11 is very important. Here's the diff.

diff --git a/src/core/vanilla.ts b/src/core/vanilla.ts
index 699c731..9ee10a4 100644
--- a/src/core/vanilla.ts
+++ b/src/core/vanilla.ts
@@ -596,7 +596,9 @@ const commitAtomState = <Value>(
 }
 
 export const flushPending = (state: State): void => {
-  state.p.forEach((prevDependencies, atom) => {
+  const pending = [...state.p]
+  state.p.clear()
+  pending.forEach(([atom, prevDependencies]) => {
     const atomState = getAtomState(state, atom)
     if (atomState) {
       if (prevDependencies) {
@@ -611,7 +613,6 @@ export const flushPending = (state: State): void => {
     const mounted = state.m.get(atom)
     mounted?.l.forEach((listener) => listener())
   })
-  state.p.clear()
 }
 
 export const subscribeAtom = (

I assume the problem is caused by creating an array from map iterator.


OK, I think I get it.

The cjs build: var pending = [].concat(state.p); <--- WRONG transpilation

The esm build: const pending = [...state.p];

@dai-shi
Copy link
Member

dai-shi commented Jun 14, 2021

Can you try the codesandbox build in #518 and see if it fixes your issue?

@dai-shi dai-shi mentioned this issue Jun 14, 2021
49 tasks
@sebastienbarre
Copy link
Author

I can't test my app in a codesandbox, but I can try to update to v1.0.0 in a few

@sebastienbarre
Copy link
Author

This worked, thank you, v1.0.0 does not trigger the aforementioned errors.

@dai-shi
Copy link
Member

dai-shi commented Jun 14, 2021

Cool! and what a relief.

By "try codesandbox build", I mean you can install the build with npm or yarn to your local app.

https://ci.codesandbox.io/status/pmndrs/jotai/pr/518/builds/139548 It's a bit hard to find the link and the instructions. 😕

@sndrrth
Copy link

sndrrth commented Apr 9, 2024

This bug happens again for me, when updating from 2.6.5 to 2.7.0 (or 2.8.0).

[Bug] no atom state to flush 

@dai-shi
Copy link
Member

dai-shi commented Apr 9, 2024

I'm not sure if it's related with the original issue, but what you observe can be probably caused by #2396.
Can you create a test case please?

@sndrrth
Copy link

sndrrth commented Apr 9, 2024

I managed to create a minimal reproduction and started a new discussion for that #2494

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 a pull request may close this issue.

3 participants