Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not mutate input #5240

Merged
merged 4 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/query-core/src/mutationCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
>(
filters: MutationFilters,
): Mutation<TData, TError, TVariables, TContext> | undefined {
if (typeof filters.exact === 'undefined') {
filters.exact = true
}
const defaultedFilters = { exact: true, ...filters }

return this.#mutations.find((mutation) => matchMutation(filters, mutation))
return this.#mutations.find((mutation) =>
matchMutation(defaultedFilters, mutation),
)
}

findAll(filters: MutationFilters = {}): Mutation[] {
Expand Down
10 changes: 4 additions & 6 deletions packages/query-core/src/queryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,11 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(
filters: WithRequired<QueryFilters, 'queryKey'>,
): Query<TQueryFnData, TError, TData> | undefined {
if (typeof filters.exact === 'undefined') {
filters.exact = true
}
const defaultedFilters = { exact: true, ...filters }

return this.getAll().find((query) => matchQuery(filters, query)) as
| Query<TQueryFnData, TError, TData>
| undefined
return this.getAll().find((query) =>
matchQuery(defaultedFilters, query),
) as Query<TQueryFnData, TError, TData> | undefined
}

findAll(filters: QueryFilters = {}): Query[] {
Expand Down
6 changes: 2 additions & 4 deletions packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,12 @@ export class QueryClient {
filters: QueryFilters = {},
cancelOptions: CancelOptions = {},
): Promise<void> {
if (typeof cancelOptions.revert === 'undefined') {
cancelOptions.revert = true
}
const defaultedCancelOptions = { revert: true, ...cancelOptions }

const promises = notifyManager.batch(() =>
this.#queryCache
.findAll(filters)
.map((query) => query.cancel(cancelOptions)),
.map((query) => query.cancel(defaultedCancelOptions)),
)

return Promise.all(promises).then(noop).catch(noop)
Expand Down
1 change: 0 additions & 1 deletion packages/vue-query/src/__tests__/mutationCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('MutationCache', () => {
})

expect(MutationCacheOrigin.prototype.find).toBeCalledWith({
exact: true,
mutationKey: ['baz'],
})
})
Expand Down
1 change: 0 additions & 1 deletion packages/vue-query/src/__tests__/queryCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('QueryCache', () => {

expect(QueryCacheOrigin.prototype.find).toBeCalledWith({
queryKey: ['foo', 'bar'],
exact: true, //Exact is true, as `find` in QueryCacheOrigin sets exact to true in the passed filters if exact is undefined
})
})
})
Expand Down