@tanstack/query-core 4.0.8 → 4.1.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/query-core",
3
- "version": "4.0.8",
3
+ "version": "4.1.0",
4
4
  "description": "TODO",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -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
@@ -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
 
@@ -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
- if (process.env.NODE_ENV !== 'production') {
574
- this.logger.error(
575
- `[QueryClient] Several query defaults match with key '${JSON.stringify(
576
- queryKey,
577
- )}'. 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.`,
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
- if (process.env.NODE_ENV !== 'production') {
621
- this.logger.error(
622
- `[QueryClient] Several mutation defaults match with key '${JSON.stringify(
623
- mutationKey,
624
- )}'. 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.`,
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
 
@@ -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
  })