Skip to content

Commit

Permalink
refactor initAtom.onMount and unskip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David Maskasky committed Mar 7, 2024
1 parent ff99659 commit fc3e978
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
6 changes: 3 additions & 3 deletions __tests__/atomEffect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,7 @@ it('should abort the previous promise', async () => {
expect(completedRuns).toEqual([0, 2])
})

// TODO: enable this test when https://github.com/pmndrs/jotai/pull/2347 releases
it.skip('should not infinite loop with nested atomEffects', async () => {
it('should not infinite loop with nested atomEffects', async () => {
const metrics = {
mounted: 0,
runCount1: 0,
Expand Down Expand Up @@ -913,9 +912,10 @@ it('should trigger the error boundary when an error is thrown', async () => {
}
render(<TestComponent />, { wrapper })
await waitFor(() => assert(didThrow))
expect(didThrow).toBe(true)
})

it.only('should trigger an error boundary when an error is thrown in a cleanup', async () => {
it('should trigger an error boundary when an error is thrown in a cleanup', async () => {
expect.assertions(1)

const refreshAtom = atom(0)
Expand Down
35 changes: 15 additions & 20 deletions src/atomEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,28 @@ export function atomEffect(

const refreshAtom = atom(0)

const initAtom = atom(null, (get, set, mounted: boolean) => {
const initAtom = atom(null, (get, set) => {
const ref = get(refAtom)
ref.mounted = mounted
if (mounted) {
ref.get = get
ref.set = set
ref.refresh = () => {
try {
ref.refreshing = true
set(refreshAtom, (c) => c + 1)
} finally {
ref.refreshing = false
}
ref.mounted = true
ref.get = get
ref.set = set
ref.refresh = () => {
try {
ref.refreshing = true
set(refreshAtom, (c) => c + 1)
} finally {
ref.refreshing = false
}
set(refreshAtom, (c) => c + 1)
} else {
}
set(refreshAtom, (c) => c + 1)
return () => {
ref.mounted = false
throwIfPendingError(ref)
ref.cleanup?.()
ref.cleanup = undefined
}
})
initAtom.onMount = (init) => {
init(true)
return () => {
init(false)
}
}
initAtom.onMount = (mount) => mount()
const effectAtom = atom((get) => {
get(refreshAtom)
const ref = get(refAtom)
Expand Down

0 comments on commit fc3e978

Please sign in to comment.