@tanstack/query-core 4.20.4 → 4.20.9

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.20.4",
3
+ "version": "4.20.9",
4
4
  "description": "The framework agnostic core that powers TanStack Query",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -62,6 +62,7 @@ export class QueryClient {
62
62
  private defaultOptions: DefaultOptions
63
63
  private queryDefaults: QueryDefaults[]
64
64
  private mutationDefaults: MutationDefaults[]
65
+ private mountCount: number
65
66
  private unsubscribeFocus?: () => void
66
67
  private unsubscribeOnline?: () => void
67
68
 
@@ -72,6 +73,7 @@ export class QueryClient {
72
73
  this.defaultOptions = config.defaultOptions || {}
73
74
  this.queryDefaults = []
74
75
  this.mutationDefaults = []
76
+ this.mountCount = 0
75
77
 
76
78
  if (process.env.NODE_ENV !== 'production' && config.logger) {
77
79
  this.logger.error(
@@ -81,6 +83,9 @@ export class QueryClient {
81
83
  }
82
84
 
83
85
  mount(): void {
86
+ this.mountCount++
87
+ if (this.mountCount !== 1) return
88
+
84
89
  this.unsubscribeFocus = focusManager.subscribe(() => {
85
90
  if (focusManager.isFocused()) {
86
91
  this.resumePausedMutations()
@@ -96,8 +101,14 @@ export class QueryClient {
96
101
  }
97
102
 
98
103
  unmount(): void {
104
+ this.mountCount--
105
+ if (this.mountCount !== 0) return
106
+
99
107
  this.unsubscribeFocus?.()
108
+ this.unsubscribeFocus = undefined
109
+
100
110
  this.unsubscribeOnline?.()
111
+ this.unsubscribeOnline = undefined
101
112
  }
102
113
 
103
114
  isFetching(filters?: QueryFilters): number
@@ -23,6 +23,7 @@ describe('queryClient', () => {
23
23
 
24
24
  afterEach(() => {
25
25
  queryClient.clear()
26
+ queryClient.unmount()
26
27
  })
27
28
 
28
29
  describe('defaultOptions', () => {
@@ -1466,6 +1467,75 @@ describe('queryClient', () => {
1466
1467
  mutationCacheResumePausedMutationsSpy.mockRestore()
1467
1468
  onlineManager.setOnline(undefined)
1468
1469
  })
1470
+
1471
+ test('should notify queryCache and mutationCache after multiple mounts and single unmount', async () => {
1472
+ const testClient = createQueryClient()
1473
+ testClient.mount()
1474
+ testClient.mount()
1475
+ testClient.unmount()
1476
+
1477
+ const queryCacheOnFocusSpy = jest.spyOn(
1478
+ testClient.getQueryCache(),
1479
+ 'onFocus',
1480
+ )
1481
+ const queryCacheOnOnlineSpy = jest.spyOn(
1482
+ testClient.getQueryCache(),
1483
+ 'onOnline',
1484
+ )
1485
+ const mutationCacheResumePausedMutationsSpy = jest.spyOn(
1486
+ testClient.getMutationCache(),
1487
+ 'resumePausedMutations',
1488
+ )
1489
+
1490
+ onlineManager.setOnline(true)
1491
+ expect(queryCacheOnOnlineSpy).toHaveBeenCalledTimes(1)
1492
+ expect(mutationCacheResumePausedMutationsSpy).toHaveBeenCalledTimes(1)
1493
+
1494
+ focusManager.setFocused(true)
1495
+ expect(queryCacheOnFocusSpy).toHaveBeenCalledTimes(1)
1496
+ expect(mutationCacheResumePausedMutationsSpy).toHaveBeenCalledTimes(2)
1497
+
1498
+ queryCacheOnFocusSpy.mockRestore()
1499
+ queryCacheOnOnlineSpy.mockRestore()
1500
+ mutationCacheResumePausedMutationsSpy.mockRestore()
1501
+ focusManager.setFocused(undefined)
1502
+ onlineManager.setOnline(undefined)
1503
+ })
1504
+
1505
+ test('should not notify queryCache and mutationCache after multiple mounts/unmounts', async () => {
1506
+ const testClient = createQueryClient()
1507
+ testClient.mount()
1508
+ testClient.mount()
1509
+ testClient.unmount()
1510
+ testClient.unmount()
1511
+
1512
+ const queryCacheOnFocusSpy = jest.spyOn(
1513
+ testClient.getQueryCache(),
1514
+ 'onFocus',
1515
+ )
1516
+ const queryCacheOnOnlineSpy = jest.spyOn(
1517
+ testClient.getQueryCache(),
1518
+ 'onOnline',
1519
+ )
1520
+ const mutationCacheResumePausedMutationsSpy = jest.spyOn(
1521
+ testClient.getMutationCache(),
1522
+ 'resumePausedMutations',
1523
+ )
1524
+
1525
+ onlineManager.setOnline(true)
1526
+ expect(queryCacheOnOnlineSpy).not.toHaveBeenCalled()
1527
+ expect(mutationCacheResumePausedMutationsSpy).not.toHaveBeenCalled()
1528
+
1529
+ focusManager.setFocused(true)
1530
+ expect(queryCacheOnFocusSpy).not.toHaveBeenCalled()
1531
+ expect(mutationCacheResumePausedMutationsSpy).not.toHaveBeenCalled()
1532
+
1533
+ queryCacheOnFocusSpy.mockRestore()
1534
+ queryCacheOnOnlineSpy.mockRestore()
1535
+ mutationCacheResumePausedMutationsSpy.mockRestore()
1536
+ focusManager.setFocused(undefined)
1537
+ onlineManager.setOnline(undefined)
1538
+ })
1469
1539
  })
1470
1540
 
1471
1541
  describe('setMutationDefaults', () => {