Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/_internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ export type MutatorOptions<Data = any, MutationData = Data> = {
| ((currentData: Data | undefined, displayedData: Data | undefined) => Data)
rollbackOnError?: boolean | ((error: unknown) => boolean)
throwOnError?: boolean
includeSpecialKeys?: boolean
}

export type MutatorConfig = {
Expand Down
16 changes: 15 additions & 1 deletion src/_internal/utils/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
Arguments,
Key
} from '../types'
import type { SWRInfiniteCacheValue } from '../../infinite/types'

type KeyFilter = (key?: Arguments) => boolean
type MutateState<Data> = State<Data, any> & {
Expand Down Expand Up @@ -60,6 +61,8 @@ export async function internalMutate<Data>(
const rollbackOnErrorOption = options.rollbackOnError
let optimisticData = options.optimisticData

const includeSpecialKeys = options.includeSpecialKeys === true

const rollbackOnError = (error: unknown): boolean => {
return typeof rollbackOnErrorOption === 'function'
? rollbackOnErrorOption(error)
Expand All @@ -74,12 +77,23 @@ export async function internalMutate<Data>(
const matchedKeys: Key[] = []
const it = cache.keys()
for (const key of it) {
const shouldSkipSpecialKeys =
!includeSpecialKeys && /^\$(inf|sub)\$/.test(key)
if (
// Skip the special useSWRInfinite and useSWRSubscription keys.
!/^\$(inf|sub)\$/.test(key) &&
!shouldSkipSpecialKeys &&
keyFilter((cache.get(key) as { _k: Arguments })._k)
) {
matchedKeys.push(key)

if (includeSpecialKeys && /^\$inf\$/.test(key)) {
const [get, set] = createCacheHelper<any, SWRInfiniteCacheValue>(
cache,
key
)
const current = get()
set({ ...current, _i: true })
}
}
}
return Promise.all(matchedKeys.map(mutateByKey))
Expand Down
Loading