@tanstack/query-core 4.0.10 → 4.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/mutationCache.js.map +1 -1
- package/build/cjs/mutationObserver.js +10 -0
- package/build/cjs/mutationObserver.js.map +1 -1
- package/build/cjs/queryCache.js.map +1 -1
- package/build/cjs/queryClient.js +2 -6
- package/build/cjs/queryClient.js.map +1 -1
- package/build/cjs/queryObserver.js +8 -0
- package/build/cjs/queryObserver.js.map +1 -1
- package/build/esm/index.js +19 -6
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats.json +235 -229
- package/build/types/packages/query-core/src/mutationCache.d.ts +6 -1
- package/build/types/packages/query-core/src/queryCache.d.ts +6 -1
- package/build/types/packages/query-core/src/types.d.ts +6 -6
- package/build/umd/index.development.js +19 -6
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/mutationCache.ts +7 -0
- package/src/mutationObserver.ts +9 -0
- package/src/queryCache.ts +7 -0
- package/src/queryClient.ts +10 -14
- package/src/queryObserver.ts +8 -0
- package/src/tests/queryObserver.test.tsx +19 -0
- package/src/types.ts +6 -6
package/package.json
CHANGED
package/src/mutationCache.ts
CHANGED
|
@@ -48,6 +48,12 @@ interface NotifyEventMutationObserverRemoved {
|
|
|
48
48
|
observer: MutationObserver<any, any, any>
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
interface NotifyEventMutationObserverOptionsUpdated {
|
|
52
|
+
type: 'observerOptionsUpdated'
|
|
53
|
+
mutation?: Mutation<any, any, any, any>
|
|
54
|
+
observer: MutationObserver<any, any, any, any>
|
|
55
|
+
}
|
|
56
|
+
|
|
51
57
|
interface NotifyEventMutationUpdated {
|
|
52
58
|
type: 'updated'
|
|
53
59
|
mutation: Mutation<any, any, any, any>
|
|
@@ -59,6 +65,7 @@ type MutationCacheNotifyEvent =
|
|
|
59
65
|
| NotifyEventMutationRemoved
|
|
60
66
|
| NotifyEventMutationObserverAdded
|
|
61
67
|
| NotifyEventMutationObserverRemoved
|
|
68
|
+
| NotifyEventMutationObserverOptionsUpdated
|
|
62
69
|
| NotifyEventMutationUpdated
|
|
63
70
|
|
|
64
71
|
type MutationCacheListener = (event: MutationCacheNotifyEvent) => void
|
package/src/mutationObserver.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
MutationObserverResult,
|
|
9
9
|
MutationObserverOptions,
|
|
10
10
|
} from './types'
|
|
11
|
+
import { shallowEqualObjects } from './utils'
|
|
11
12
|
|
|
12
13
|
// TYPES
|
|
13
14
|
|
|
@@ -63,7 +64,15 @@ export class MutationObserver<
|
|
|
63
64
|
setOptions(
|
|
64
65
|
options?: MutationObserverOptions<TData, TError, TVariables, TContext>,
|
|
65
66
|
) {
|
|
67
|
+
const prevOptions = this.options
|
|
66
68
|
this.options = this.client.defaultMutationOptions(options)
|
|
69
|
+
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
70
|
+
this.client.getMutationCache().notify({
|
|
71
|
+
type: 'observerOptionsUpdated',
|
|
72
|
+
mutation: this.currentMutation,
|
|
73
|
+
observer: this,
|
|
74
|
+
})
|
|
75
|
+
}
|
|
67
76
|
}
|
|
68
77
|
|
|
69
78
|
protected onUnsubscribe(): void {
|
package/src/queryCache.ts
CHANGED
|
@@ -55,6 +55,12 @@ interface NotifyEventQueryObserverResultsUpdated {
|
|
|
55
55
|
query: Query<any, any, any, any>
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
interface NotifyEventQueryObserverOptionsUpdated {
|
|
59
|
+
type: 'observerOptionsUpdated'
|
|
60
|
+
query: Query<any, any, any, any>
|
|
61
|
+
observer: QueryObserver<any, any, any, any, any>
|
|
62
|
+
}
|
|
63
|
+
|
|
58
64
|
type QueryCacheNotifyEvent =
|
|
59
65
|
| NotifyEventQueryAdded
|
|
60
66
|
| NotifyEventQueryRemoved
|
|
@@ -62,6 +68,7 @@ type QueryCacheNotifyEvent =
|
|
|
62
68
|
| NotifyEventQueryObserverAdded
|
|
63
69
|
| NotifyEventQueryObserverRemoved
|
|
64
70
|
| NotifyEventQueryObserverResultsUpdated
|
|
71
|
+
| NotifyEventQueryObserverOptionsUpdated
|
|
65
72
|
|
|
66
73
|
type QueryCacheListener = (event: QueryCacheNotifyEvent) => void
|
|
67
74
|
|
package/src/queryClient.ts
CHANGED
|
@@ -570,13 +570,11 @@ export class QueryClient {
|
|
|
570
570
|
)
|
|
571
571
|
// It is ok not having defaults, but it is error prone to have more than 1 default for a given key
|
|
572
572
|
if (matchingDefaults.length > 1) {
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
)
|
|
579
|
-
}
|
|
573
|
+
this.logger.error(
|
|
574
|
+
`[QueryClient] Several query defaults match with key '${JSON.stringify(
|
|
575
|
+
queryKey,
|
|
576
|
+
)}'. The first matching query defaults are used. Please check how query defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydefaults.`,
|
|
577
|
+
)
|
|
580
578
|
}
|
|
581
579
|
}
|
|
582
580
|
|
|
@@ -617,13 +615,11 @@ export class QueryClient {
|
|
|
617
615
|
)
|
|
618
616
|
// It is ok not having defaults, but it is error prone to have more than 1 default for a given key
|
|
619
617
|
if (matchingDefaults.length > 1) {
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
)
|
|
626
|
-
}
|
|
618
|
+
this.logger.error(
|
|
619
|
+
`[QueryClient] Several mutation defaults match with key '${JSON.stringify(
|
|
620
|
+
mutationKey,
|
|
621
|
+
)}'. The first matching mutation defaults are used. Please check how mutation defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetmutationdefaults.`,
|
|
622
|
+
)
|
|
627
623
|
}
|
|
628
624
|
}
|
|
629
625
|
|
package/src/queryObserver.ts
CHANGED
|
@@ -155,6 +155,14 @@ export class QueryObserver<
|
|
|
155
155
|
|
|
156
156
|
this.options = this.client.defaultQueryOptions(options)
|
|
157
157
|
|
|
158
|
+
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
159
|
+
this.client.getQueryCache().notify({
|
|
160
|
+
type: 'observerOptionsUpdated',
|
|
161
|
+
query: this.currentQuery,
|
|
162
|
+
observer: this,
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
|
|
158
166
|
if (
|
|
159
167
|
typeof this.options.enabled !== 'undefined' &&
|
|
160
168
|
typeof this.options.enabled !== 'boolean'
|
|
@@ -799,4 +799,23 @@ describe('queryObserver', () => {
|
|
|
799
799
|
|
|
800
800
|
unsubscribe()
|
|
801
801
|
})
|
|
802
|
+
|
|
803
|
+
test('setOptions should notify cache listeners', async () => {
|
|
804
|
+
const key = queryKey()
|
|
805
|
+
|
|
806
|
+
const observer = new QueryObserver(queryClient, {
|
|
807
|
+
queryKey: key,
|
|
808
|
+
})
|
|
809
|
+
|
|
810
|
+
const spy = jest.fn()
|
|
811
|
+
const unsubscribe = queryClient.getQueryCache().subscribe(spy)
|
|
812
|
+
observer.setOptions({ enabled: false })
|
|
813
|
+
|
|
814
|
+
expect(spy).toHaveBeenCalledTimes(1)
|
|
815
|
+
expect(spy).toHaveBeenCalledWith(
|
|
816
|
+
expect.objectContaining({ type: 'observerOptionsUpdated' }),
|
|
817
|
+
)
|
|
818
|
+
|
|
819
|
+
unsubscribe()
|
|
820
|
+
})
|
|
802
821
|
})
|
package/src/types.ts
CHANGED
|
@@ -535,18 +535,18 @@ export interface MutationOptions<
|
|
|
535
535
|
data: TData,
|
|
536
536
|
variables: TVariables,
|
|
537
537
|
context: TContext | undefined,
|
|
538
|
-
) => Promise<unknown> |
|
|
538
|
+
) => Promise<unknown> | unknown
|
|
539
539
|
onError?: (
|
|
540
540
|
error: TError,
|
|
541
541
|
variables: TVariables,
|
|
542
542
|
context: TContext | undefined,
|
|
543
|
-
) => Promise<unknown> |
|
|
543
|
+
) => Promise<unknown> | unknown
|
|
544
544
|
onSettled?: (
|
|
545
545
|
data: TData | undefined,
|
|
546
546
|
error: TError | null,
|
|
547
547
|
variables: TVariables,
|
|
548
548
|
context: TContext | undefined,
|
|
549
|
-
) => Promise<unknown> |
|
|
549
|
+
) => Promise<unknown> | unknown
|
|
550
550
|
retry?: RetryValue<TError>
|
|
551
551
|
retryDelay?: RetryDelayValue<TError>
|
|
552
552
|
networkMode?: NetworkMode
|
|
@@ -574,18 +574,18 @@ export interface MutateOptions<
|
|
|
574
574
|
data: TData,
|
|
575
575
|
variables: TVariables,
|
|
576
576
|
context: TContext,
|
|
577
|
-
) => Promise<unknown> |
|
|
577
|
+
) => Promise<unknown> | unknown
|
|
578
578
|
onError?: (
|
|
579
579
|
error: TError,
|
|
580
580
|
variables: TVariables,
|
|
581
581
|
context: TContext | undefined,
|
|
582
|
-
) => Promise<unknown> |
|
|
582
|
+
) => Promise<unknown> | unknown
|
|
583
583
|
onSettled?: (
|
|
584
584
|
data: TData | undefined,
|
|
585
585
|
error: TError | null,
|
|
586
586
|
variables: TVariables,
|
|
587
587
|
context: TContext | undefined,
|
|
588
|
-
) => Promise<unknown> |
|
|
588
|
+
) => Promise<unknown> | unknown
|
|
589
589
|
}
|
|
590
590
|
|
|
591
591
|
export type MutateFunction<
|