Skip to content

Commit

Permalink
improve error handling for flushPending
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Nov 14, 2024
1 parent 9462d55 commit 004e873
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
17 changes: 15 additions & 2 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,27 @@ const addPendingFunction = (pending: Pending, fn: () => void) => {
}

const flushPending = (pending: Pending) => {
let error: unknown | undefined
const call = (fn: () => void) => {
try {
fn()
} catch (e) {
if (!error) {
error = e
}
}
}
while (pending[1].size || pending[2].size) {
pending[0].clear()
const atomStates = new Set(pending[1].values())
pending[1].clear()
const functions = new Set(pending[2])
pending[2].clear()
atomStates.forEach((atomState) => atomState.m?.l.forEach((l) => l()))
functions.forEach((fn) => fn())
atomStates.forEach((atomState) => atomState.m?.l.forEach(call))
functions.forEach(call)
}
if (error) {
throw error
}
}

Expand Down
36 changes: 27 additions & 9 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,9 @@ describe('should mount and trigger listeners even when an error is thrown', () =
get(a)
try {
get(e)
} catch {}
} catch {
// expect error
}
})
})
store.sub(b, () => {})
Expand All @@ -687,7 +689,9 @@ describe('should mount and trigger listeners even when an error is thrown', () =
setTimeout(() => {
try {
setSelf()
} catch {}
} catch {
// expect error
}
})
},
(get, set) => {
Expand Down Expand Up @@ -736,7 +740,9 @@ describe('should mount and trigger listeners even when an error is thrown', () =
setTimeout(() => {
try {
set(b)
} catch {}
} catch {
// expect error
}
})
})
const listener = vi.fn()
Expand All @@ -763,7 +769,9 @@ describe('should mount and trigger listeners even when an error is thrown', () =
store.sub(a, listener)
try {
store.set(b)
} catch {}
} catch {
// expect error
}
expect(listener).toHaveBeenCalledOnce()
})

Expand All @@ -781,13 +789,17 @@ describe('should mount and trigger listeners even when an error is thrown', () =
setTimeout(() => {
try {
setAtom()
} catch {}
} catch {
// expect error
}
})
return () => {
setTimeout(() => {
try {
setAtom()
} catch {}
} catch {
// expect error
}
})
}
}
Expand Down Expand Up @@ -816,7 +828,9 @@ describe('should mount and trigger listeners even when an error is thrown', () =
}
try {
store.sub(b, () => {})
} catch {}
} catch {
// expect error
}
expect(a.onMount).toHaveBeenCalledOnce()
})

Expand All @@ -835,7 +849,9 @@ describe('should mount and trigger listeners even when an error is thrown', () =
const unsub = store.sub(b, () => {})
try {
unsub()
} catch {}
} catch {
// expect error
}
expect(aUnmount).toHaveBeenCalledOnce()
})

Expand All @@ -854,7 +870,9 @@ describe('should mount and trigger listeners even when an error is thrown', () =
store.sub(a, listener)
try {
store.set(b)
} catch {}
} catch {
// expect error
}
expect(listener).toHaveBeenCalledOnce()
})
})

0 comments on commit 004e873

Please sign in to comment.