Skip to content

Commit

Permalink
fix(async): status AsyncStatusesAbortedPending #985
Browse files Browse the repository at this point in the history
  • Loading branch information
artalar committed Dec 11, 2024
1 parent e284d32 commit 85e60ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
33 changes: 33 additions & 0 deletions packages/async/src/withStatusesAtom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,39 @@ test('do not reject on abort', async () => {
;`👍` //?
})

test('isEverSettled after abort', async () => {
const fetchData = reatomAsync(async () => sleep()).pipe(withAbort(), withStatusesAtom())
const ctx = createTestCtx()

expect(ctx.get(fetchData.statusesAtom)).toBe(asyncStatusesInitState)
await fetchData(ctx)
expect(ctx.get(fetchData.statusesAtom).isFulfilled).toBe(true)
expect(ctx.get(fetchData.statusesAtom)).toEqual({
isPending: false,
isFulfilled: true,
isRejected: false,
isSettled: true,

isFirstPending: false,
isEverPending: true,
isEverSettled: true,
} satisfies AsyncStatusesFulfilled)

fetchData(ctx).catch(noop)
fetchData(ctx).catch(noop)
await null
expect(ctx.get(fetchData.statusesAtom)).toEqual({
isPending: true,
isFulfilled: false,
isRejected: false,
isSettled: false,

isFirstPending: false,
isEverPending: true,
isEverSettled: true,
} satisfies AsyncStatusesAbortedPending)
})

test('do not reject on resource abort', async () => {
const fetchData = reatomResource(async (ctx) => {}).pipe(withStatusesAtom())
const ctx = createTestCtx()
Expand Down
27 changes: 7 additions & 20 deletions packages/async/src/withStatusesAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface AsyncStatusesAbortedPending {

isFirstPending: false
isEverPending: true
isEverSettled: false
isEverSettled: boolean
}

export interface AsyncStatusesAbortedFulfill {
Expand All @@ -70,9 +70,7 @@ export interface AsyncStatusesAbortedReject {
isEverSettled: true
}

export type AsyncStatusesAbortedSettle =
| AsyncStatusesAbortedFulfill
| AsyncStatusesAbortedReject
export type AsyncStatusesAbortedSettle = AsyncStatusesAbortedFulfill | AsyncStatusesAbortedReject

export interface AsyncStatusesFulfilled {
isPending: false
Expand Down Expand Up @@ -107,10 +105,7 @@ export interface AsyncStatusesAnotherPending {
isEverSettled: true
}

export type AsyncStatusesPending =
| AsyncStatusesFirstPending
| AsyncStatusesAbortedPending
| AsyncStatusesAnotherPending
export type AsyncStatusesPending = AsyncStatusesFirstPending | AsyncStatusesAbortedPending | AsyncStatusesAnotherPending

export type AsyncStatuses =
| AsyncStatusesNeverPending
Expand Down Expand Up @@ -162,10 +157,7 @@ export const withStatusesAtom =
`${anAsync.__reatom.name}.statusesAtom._lastSettledStatusAtom`,
)

const statusesAtom = atom<AsyncStatuses>(
asyncStatusesInitState,
`${anAsync.__reatom.name}.statusesAtom`,
)
const statusesAtom = atom<AsyncStatuses>(asyncStatusesInitState, `${anAsync.__reatom.name}.statusesAtom`)

// @ts-expect-error computer dump types
statusesAtom.__reatom.computer = (ctx, state: AsyncStatuses) => {
Expand All @@ -192,10 +184,7 @@ export const withStatusesAtom =
anAsync.statusesAtom = Object.assign(statusesAtom, {
reset: action((ctx) => {
relatedPromisesAtom(ctx, new Set())
return statusesAtom(
ctx,
asyncStatusesInitState,
) as AsyncStatusesNeverPending
return statusesAtom(ctx, asyncStatusesInitState) as AsyncStatusesNeverPending
}),
})

Expand Down Expand Up @@ -257,10 +246,8 @@ export const withStatusesAtom =

isFirstPending: false,
isEverPending: true,
isEverSettled: false,
} satisfies
| AsyncStatusesAbortedPending
| AsyncStatusesFirstAborted
isEverSettled: state.isEverSettled,
} as AsyncStatusesAbortedPending | AsyncStatusesFirstAborted | AsyncStatusesAbortedPending
}
}

Expand Down

0 comments on commit 85e60ef

Please sign in to comment.