@tanstack/query-core 4.13.0 → 4.14.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.13.0",
3
+ "version": "4.14.0",
4
4
  "description": "The framework agnostic core that powers TanStack Query",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -13,6 +13,7 @@ export { focusManager } from './focusManager'
13
13
  export { onlineManager } from './onlineManager'
14
14
  export {
15
15
  hashQueryKey,
16
+ replaceEqualDeep,
16
17
  isError,
17
18
  isServer,
18
19
  parseQueryArgs,
package/src/query.ts CHANGED
@@ -600,16 +600,14 @@ function getDefaultState<
600
600
  ? (options.initialData as InitialDataFunction<TData>)()
601
601
  : options.initialData
602
602
 
603
- const hasInitialData = typeof options.initialData !== 'undefined'
603
+ const hasData = typeof data !== 'undefined'
604
604
 
605
- const initialDataUpdatedAt = hasInitialData
605
+ const initialDataUpdatedAt = hasData
606
606
  ? typeof options.initialDataUpdatedAt === 'function'
607
607
  ? (options.initialDataUpdatedAt as () => number | undefined)()
608
608
  : options.initialDataUpdatedAt
609
609
  : 0
610
610
 
611
- const hasData = typeof data !== 'undefined'
612
-
613
611
  return {
614
612
  data,
615
613
  dataUpdateCount: 0,
@@ -71,6 +71,12 @@ export class QueryClient {
71
71
  this.defaultOptions = config.defaultOptions || {}
72
72
  this.queryDefaults = []
73
73
  this.mutationDefaults = []
74
+
75
+ if (process.env.NODE_ENV !== 'production' && config.logger) {
76
+ this.logger.error(
77
+ `Passing a custom logger has been deprecated and will be removed in the next major version.`,
78
+ )
79
+ }
74
80
  }
75
81
 
76
82
  mount(): void {
@@ -155,6 +155,17 @@ export class QueryObserver<
155
155
 
156
156
  this.options = this.client.defaultQueryOptions(options)
157
157
 
158
+ if (
159
+ process.env.NODE_ENV !== 'production' &&
160
+ typeof options?.isDataEqual !== 'undefined'
161
+ ) {
162
+ this.client
163
+ .getLogger()
164
+ .error(
165
+ `The isDataEqual option has been deprecated and will be removed in the next major version. You can achieve the same functionality by passing a function as the structuralSharing option`,
166
+ )
167
+ }
168
+
158
169
  if (!shallowEqualObjects(prevOptions, this.options)) {
159
170
  this.client.getQueryCache().notify({
160
171
  type: 'observerOptionsUpdated',
@@ -457,7 +468,7 @@ export class QueryObserver<
457
468
  // Keep previous data if needed
458
469
  if (
459
470
  options.keepPreviousData &&
460
- !state.dataUpdateCount &&
471
+ !state.dataUpdatedAt &&
461
472
  prevQueryResult?.isSuccess &&
462
473
  status !== 'error'
463
474
  ) {
@@ -517,11 +528,6 @@ export class QueryObserver<
517
528
  if (options.select && typeof placeholderData !== 'undefined') {
518
529
  try {
519
530
  placeholderData = options.select(placeholderData)
520
- placeholderData = replaceData(
521
- prevResult?.data,
522
- placeholderData,
523
- options,
524
- )
525
531
  this.selectError = null
526
532
  } catch (selectError) {
527
533
  if (process.env.NODE_ENV !== 'production') {
@@ -534,7 +540,7 @@ export class QueryObserver<
534
540
 
535
541
  if (typeof placeholderData !== 'undefined') {
536
542
  status = 'success'
537
- data = placeholderData as TData
543
+ data = replaceData(prevResult?.data, placeholderData, options) as TData
538
544
  isPlaceholderData = true
539
545
  }
540
546
  }
@@ -50,7 +50,13 @@ describe('queriesObserver', () => {
50
50
  unsubscribe()
51
51
  expect(observerResult).toMatchObject([{ data: 1 }, { data: 2 }])
52
52
 
53
- expect(mockLogger.error).toHaveBeenCalledTimes(1)
53
+ expect(mockLogger.error).toHaveBeenCalledTimes(2)
54
+ expect(mockLogger.error).toHaveBeenCalledWith(
55
+ 'Passing a custom logger has been deprecated and will be removed in the next major version.',
56
+ )
57
+ expect(mockLogger.error).toHaveBeenCalledWith(
58
+ 'Passing a custom logger has been deprecated and will be removed in the next major version.',
59
+ )
54
60
  })
55
61
 
56
62
  test('should update when a query updates', async () => {
@@ -160,7 +160,7 @@ describe('queryClient', () => {
160
160
  // No defaults, no warning
161
161
  const noDefaults = queryClient.getQueryDefaults(keyABCD)
162
162
  expect(noDefaults).toBeUndefined()
163
- expect(mockLogger.error).not.toHaveBeenCalled()
163
+ expect(mockLogger.error).toHaveBeenCalledTimes(1)
164
164
 
165
165
  // If defaults for key ABCD are registered **before** the ones of key ABC (more generic)…
166
166
  queryClient.setQueryDefaults(keyABCD, defaultsOfABCD)
@@ -169,7 +169,7 @@ describe('queryClient', () => {
169
169
  const goodDefaults = queryClient.getQueryDefaults(keyABCD)
170
170
  expect(goodDefaults).toBe(defaultsOfABCD)
171
171
  // The warning is still raised since several defaults are matching
172
- expect(mockLogger.error).toHaveBeenCalledTimes(1)
172
+ expect(mockLogger.error).toHaveBeenCalledTimes(2)
173
173
 
174
174
  // Let's create another queryClient and change the order of registration
175
175
  const newQueryClient = createQueryClient()
@@ -180,7 +180,7 @@ describe('queryClient', () => {
180
180
  const badDefaults = newQueryClient.getQueryDefaults(keyABCD)
181
181
  expect(badDefaults).not.toBe(defaultsOfABCD)
182
182
  expect(badDefaults).toBe(defaultsOfABC)
183
- expect(mockLogger.error).toHaveBeenCalledTimes(2)
183
+ expect(mockLogger.error).toHaveBeenCalledTimes(4)
184
184
  })
185
185
 
186
186
  test('should warn in dev if several mutation defaults match a given key', () => {
@@ -216,7 +216,10 @@ describe('queryClient', () => {
216
216
  // No defaults, no warning
217
217
  const noDefaults = queryClient.getMutationDefaults(keyABCD)
218
218
  expect(noDefaults).toBeUndefined()
219
- expect(mockLogger.error).not.toHaveBeenCalled()
219
+ expect(mockLogger.error).toHaveBeenNthCalledWith(
220
+ 1,
221
+ 'Passing a custom logger has been deprecated and will be removed in the next major version.',
222
+ )
220
223
 
221
224
  // If defaults for key ABCD are registered **before** the ones of key ABC (more generic)…
222
225
  queryClient.setMutationDefaults(keyABCD, defaultsOfABCD)
@@ -225,7 +228,7 @@ describe('queryClient', () => {
225
228
  const goodDefaults = queryClient.getMutationDefaults(keyABCD)
226
229
  expect(goodDefaults).toBe(defaultsOfABCD)
227
230
  // The warning is still raised since several defaults are matching
228
- expect(mockLogger.error).toHaveBeenCalledTimes(1)
231
+ expect(mockLogger.error).toHaveBeenCalledTimes(2)
229
232
 
230
233
  // Let's create another queryClient and change the order of registration
231
234
  const newQueryClient = createQueryClient()
@@ -236,7 +239,7 @@ describe('queryClient', () => {
236
239
  const badDefaults = newQueryClient.getMutationDefaults(keyABCD)
237
240
  expect(badDefaults).not.toBe(defaultsOfABCD)
238
241
  expect(badDefaults).toBe(defaultsOfABC)
239
- expect(mockLogger.error).toHaveBeenCalledTimes(2)
242
+ expect(mockLogger.error).toHaveBeenCalledTimes(4)
240
243
  })
241
244
  })
242
245
 
@@ -505,6 +505,24 @@ describe('queryObserver', () => {
505
505
  expect(results[1]).toMatchObject({ status: 'success', data: 'data' })
506
506
  })
507
507
 
508
+ test('should structurally share placeholder data', async () => {
509
+ const key = queryKey()
510
+ const observer = new QueryObserver(queryClient, {
511
+ queryKey: key,
512
+ enabled: false,
513
+ queryFn: () => 'data',
514
+ placeholderData: {},
515
+ })
516
+
517
+ const firstData = observer.getCurrentResult().data
518
+
519
+ observer.setOptions({ placeholderData: {} })
520
+
521
+ const secondData = observer.getCurrentResult().data
522
+
523
+ expect(firstData).toBe(secondData)
524
+ })
525
+
508
526
  test('the retrier should not throw an error when reject if the retrier is already resolved', async () => {
509
527
  const key = queryKey()
510
528
  let count = 0
@@ -672,7 +690,7 @@ describe('queryObserver', () => {
672
690
  },
673
691
  })
674
692
 
675
- expect(mockLogger.error).toHaveBeenNthCalledWith(1, new Error('error'))
693
+ expect(mockLogger.error).toHaveBeenNthCalledWith(2, new Error('error'))
676
694
  })
677
695
 
678
696
  test('should not use replaceEqualDeep for select value when structuralSharing option is true and placeholderdata is defined', () => {