-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
140 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react' | ||
import { QueryClient } from '@tanstack/query-core' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import { Provider, useAtom } from 'jotai' | ||
import { atomWithMutation, atomWithMutationState } from '../src' | ||
|
||
it('atomWithMutationState multiple', async () => { | ||
const client = new QueryClient() | ||
let resolve1: (() => void) | undefined | ||
const mutateAtom1 = atomWithMutation<number, number>( | ||
() => ({ | ||
mutationKey: ['test-atom'], | ||
mutationFn: async (a) => { | ||
await new Promise<void>((r) => { | ||
resolve1 = r | ||
}) | ||
return a | ||
}, | ||
}), | ||
() => client | ||
) | ||
let resolve2: (() => void) | undefined | ||
const mutateAtom2 = atomWithMutation<number, number>( | ||
() => ({ | ||
mutationKey: ['test-atom'], | ||
mutationFn: async (a) => { | ||
await new Promise<void>((r) => { | ||
resolve2 = r | ||
}) | ||
return a | ||
}, | ||
}), | ||
() => client | ||
) | ||
|
||
const mutationStateAtom = atomWithMutationState( | ||
() => ({ filters: { mutationKey: ['test-atom'] } }), | ||
() => client | ||
) | ||
|
||
function App() { | ||
const [{ mutate: mutate1 }] = useAtom(mutateAtom1) | ||
const [{ mutate: mutate2 }] = useAtom(mutateAtom2) | ||
const [mutations] = useAtom(mutationStateAtom) | ||
|
||
return ( | ||
<div> | ||
<p>mutationCount: {mutations.length}</p> | ||
<button | ||
onClick={() => { | ||
mutate1(1) | ||
mutate2(2) | ||
}}> | ||
mutate | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
const { findByText, getByText } = render( | ||
<Provider> | ||
<App /> | ||
</Provider> | ||
) | ||
|
||
await findByText('mutationCount: 0') | ||
fireEvent.click(getByText('mutate')) | ||
await findByText('mutationCount: 2') | ||
resolve1?.() | ||
await findByText('mutationCount: 1') | ||
resolve2?.() | ||
await findByText('mutationCount: 0') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { | ||
DefaultError, | ||
Mutation, | ||
MutationCache, | ||
MutationFilters, | ||
MutationState, | ||
QueryClient, | ||
} from '@tanstack/query-core' | ||
import { Getter, atom } from 'jotai' | ||
import { queryClientAtom } from './queryClientAtom' | ||
|
||
type MutationStateOptions<TResult = MutationState> = { | ||
filters?: MutationFilters | ||
select?: ( | ||
mutation: Mutation<unknown, DefaultError, unknown, unknown> | ||
) => TResult | ||
} | ||
|
||
function getResult<TResult = MutationState>( | ||
mutationCache: MutationCache, | ||
options: MutationStateOptions<TResult> | ||
): Array<TResult> { | ||
return mutationCache | ||
.findAll({ ...options.filters, status: 'pending' }) | ||
.map( | ||
(mutation): TResult => | ||
(options.select | ||
? options.select( | ||
mutation as Mutation<unknown, DefaultError, unknown, unknown> | ||
) | ||
: mutation.state) as TResult | ||
) | ||
} | ||
|
||
export const atomWithMutationState = <TResult = MutationState>( | ||
getOptions: (get: Getter) => MutationStateOptions<TResult>, | ||
getQueryClient: (get: Getter) => QueryClient = (get) => get(queryClientAtom) | ||
) => { | ||
const resultsAtom = atom<TResult[]>([]) | ||
const observableAtom = atom((get) => { | ||
const queryClient = getQueryClient(get) | ||
|
||
const mutationCache = queryClient.getMutationCache() | ||
resultsAtom.onMount = (set) => { | ||
mutationCache.subscribe(() => { | ||
set(getResult(getQueryClient(get).getMutationCache(), getOptions(get))) | ||
}) | ||
} | ||
}) | ||
|
||
return atom((get) => { | ||
get(observableAtom) | ||
return get(resultsAtom) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters