Skip to content

Commit

Permalink
Merge pull request #1 from Key5n/progress
Browse files Browse the repository at this point in the history
fix and add test
  • Loading branch information
Key5n committed Jun 17, 2024
2 parents b4681da + 52cf67d commit 27f0dbd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
25 changes: 18 additions & 7 deletions src/_internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,24 @@ export interface ScopedMutator {
* @typeParam Data - The type of the data related to the key
* @typeParam MutationData - The type of the data returned by the mutator
*/
export type KeyedMutator<Data> = <MutationData = Data>(
data?:
| MutationData
| Promise<MutationData | undefined>
| MutatorCallback<MutationData>,
opts?: boolean | MutatorOptions<Data, MutationData>
) => Promise<MutationData | undefined>
export type KeyedMutator<Data> = {
(
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
opts?: boolean | MutatorOptions<Data, Data>
): Promise<Data | undefined>
<MutationData = Data>(
data:
| MutationData
| Promise<MutationData | undefined>
| MutatorCallback<MutationData>,
opts: Omit<MutatorOptions<Data, MutationData>, 'populateCache'> & {
populateCache: (
result: MutationData,
currentData: Data | undefined
) => Data
}
): Promise<Data | MutationData | undefined>
}

export type SWRConfiguration<
Data = any,
Expand Down
11 changes: 2 additions & 9 deletions src/infinite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import type {
BareFetcher,
SWRHook,
MutatorCallback,
Middleware,
GlobalState
} from '../_internal'
Expand Down Expand Up @@ -238,15 +237,9 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
config
)

const mutate = useCallback(
const mutate = useCallback<SWRInfiniteKeyedMutator<Data[]>>(
// eslint-disable-next-line func-names
function <MutationData = Data[]>(
data?:
| MutationData
| Promise<MutationData | undefined>
| MutatorCallback<MutationData>,
opts?: boolean | SWRInfiniteMutatorOptions<Data[], MutationData>
) {
function (data?: any, opts?: any) {
// When passing as a boolean, it's explicitly used to disable/enable
// revalidation.
const options =
Expand Down
25 changes: 21 additions & 4 deletions src/infinite/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,27 @@ interface SWRInfiniteRevalidateFn<Data = any> {
(data: Data, key: Arguments): boolean
}

export type SWRInfiniteKeyedMutator<Data> = <MutationData = Data>(
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
opts?: boolean | SWRInfiniteMutatorOptions<Data, MutationData>
) => Promise<Data | MutationData | undefined>
export type SWRInfiniteKeyedMutator<Data> = {
(
data?: Data | Promise<Data | undefined> | MutatorCallback<Data>,
opts?: boolean | SWRInfiniteMutatorOptions<Data, Data>
): Promise<Data | undefined>
<MutationData = Data>(
data:
| MutationData
| Promise<MutationData | undefined>
| MutatorCallback<MutationData>,
opts: Omit<
SWRInfiniteMutatorOptions<Data, MutationData>,
'populateCache'
> & {
populateCache: (
result: MutationData,
currentData: Data | undefined
) => Data
}
): Promise<Data | MutationData | undefined>
}

export interface SWRInfiniteMutatorOptions<Data = any, MutationData = Data>
extends Omit<MutatorOptions<Data, MutationData>, 'revalidate'> {
Expand Down
7 changes: 7 additions & 0 deletions test/type/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,18 @@ export function useMutatorTypes() {

mutate(async () => '1')
mutate(async () => '1', { populateCache: false })
mutate(async () => 1, {
populateCache: (result: number) => `${result}`
})

// @ts-expect-error
mutate(async () => 1)
// @ts-expect-error
mutate(async () => 1, { populateCache: false })
// @ts-expect-error
mutate(async () => 1, {
populateCache: (result: number) => result
})
}

export function useConfigMutate() {
Expand Down

0 comments on commit 27f0dbd

Please sign in to comment.