@tanstack/query-core 4.24.10 → 5.0.0-alpha.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.
Files changed (183) hide show
  1. package/build/lib/focusManager.d.ts +1 -3
  2. package/build/lib/focusManager.esm.js +19 -36
  3. package/build/lib/focusManager.esm.js.map +1 -1
  4. package/build/lib/focusManager.js +19 -38
  5. package/build/lib/focusManager.js.map +1 -1
  6. package/build/lib/focusManager.mjs +19 -36
  7. package/build/lib/focusManager.mjs.map +1 -1
  8. package/build/lib/hydration.esm.js +21 -23
  9. package/build/lib/hydration.esm.js.map +1 -1
  10. package/build/lib/hydration.js +21 -25
  11. package/build/lib/hydration.js.map +1 -1
  12. package/build/lib/hydration.mjs +21 -23
  13. package/build/lib/hydration.mjs.map +1 -1
  14. package/build/lib/index.d.ts +1 -2
  15. package/build/lib/index.esm.js +1 -1
  16. package/build/lib/index.js +2 -8
  17. package/build/lib/index.js.map +1 -1
  18. package/build/lib/index.mjs +1 -1
  19. package/build/lib/infiniteQueryBehavior.d.ts +3 -7
  20. package/build/lib/infiniteQueryBehavior.esm.js +52 -75
  21. package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
  22. package/build/lib/infiniteQueryBehavior.js +50 -77
  23. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  24. package/build/lib/infiniteQueryBehavior.mjs +52 -75
  25. package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
  26. package/build/lib/infiniteQueryObserver.d.ts +4 -4
  27. package/build/lib/infiniteQueryObserver.esm.js +18 -26
  28. package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
  29. package/build/lib/infiniteQueryObserver.js +18 -28
  30. package/build/lib/infiniteQueryObserver.js.map +1 -1
  31. package/build/lib/infiniteQueryObserver.mjs +18 -26
  32. package/build/lib/infiniteQueryObserver.mjs.map +1 -1
  33. package/build/lib/mutation.d.ts +11 -22
  34. package/build/lib/mutation.esm.js +73 -105
  35. package/build/lib/mutation.esm.js.map +1 -1
  36. package/build/lib/mutation.js +73 -107
  37. package/build/lib/mutation.js.map +1 -1
  38. package/build/lib/mutation.mjs +73 -105
  39. package/build/lib/mutation.mjs.map +1 -1
  40. package/build/lib/mutationCache.d.ts +4 -6
  41. package/build/lib/mutationCache.esm.js +23 -32
  42. package/build/lib/mutationCache.esm.js.map +1 -1
  43. package/build/lib/mutationCache.js +23 -34
  44. package/build/lib/mutationCache.js.map +1 -1
  45. package/build/lib/mutationCache.mjs +23 -32
  46. package/build/lib/mutationCache.mjs.map +1 -1
  47. package/build/lib/mutationObserver.d.ts +4 -9
  48. package/build/lib/mutationObserver.esm.js +43 -72
  49. package/build/lib/mutationObserver.esm.js.map +1 -1
  50. package/build/lib/mutationObserver.js +43 -74
  51. package/build/lib/mutationObserver.js.map +1 -1
  52. package/build/lib/mutationObserver.mjs +43 -72
  53. package/build/lib/mutationObserver.mjs.map +1 -1
  54. package/build/lib/notifyManager.esm.js +7 -17
  55. package/build/lib/notifyManager.esm.js.map +1 -1
  56. package/build/lib/notifyManager.js +7 -19
  57. package/build/lib/notifyManager.js.map +1 -1
  58. package/build/lib/notifyManager.mjs +7 -17
  59. package/build/lib/notifyManager.mjs.map +1 -1
  60. package/build/lib/onlineManager.d.ts +1 -3
  61. package/build/lib/onlineManager.esm.js +16 -30
  62. package/build/lib/onlineManager.esm.js.map +1 -1
  63. package/build/lib/onlineManager.js +16 -32
  64. package/build/lib/onlineManager.js.map +1 -1
  65. package/build/lib/onlineManager.mjs +16 -30
  66. package/build/lib/onlineManager.mjs.map +1 -1
  67. package/build/lib/queriesObserver.d.ts +3 -10
  68. package/build/lib/queriesObserver.esm.js +47 -71
  69. package/build/lib/queriesObserver.esm.js.map +1 -1
  70. package/build/lib/queriesObserver.js +49 -75
  71. package/build/lib/queriesObserver.js.map +1 -1
  72. package/build/lib/queriesObserver.mjs +47 -71
  73. package/build/lib/queriesObserver.mjs.map +1 -1
  74. package/build/lib/query.d.ts +14 -21
  75. package/build/lib/query.esm.js +140 -194
  76. package/build/lib/query.esm.js.map +1 -1
  77. package/build/lib/query.js +139 -195
  78. package/build/lib/query.js.map +1 -1
  79. package/build/lib/query.mjs +140 -194
  80. package/build/lib/query.mjs.map +1 -1
  81. package/build/lib/queryCache.d.ts +12 -7
  82. package/build/lib/queryCache.esm.js +21 -45
  83. package/build/lib/queryCache.esm.js.map +1 -1
  84. package/build/lib/queryCache.js +20 -46
  85. package/build/lib/queryCache.js.map +1 -1
  86. package/build/lib/queryCache.mjs +21 -45
  87. package/build/lib/queryCache.mjs.map +1 -1
  88. package/build/lib/queryClient.d.ts +18 -46
  89. package/build/lib/queryClient.esm.js +137 -216
  90. package/build/lib/queryClient.esm.js.map +1 -1
  91. package/build/lib/queryClient.js +136 -217
  92. package/build/lib/queryClient.js.map +1 -1
  93. package/build/lib/queryClient.mjs +137 -216
  94. package/build/lib/queryClient.mjs.map +1 -1
  95. package/build/lib/queryObserver.d.ts +4 -29
  96. package/build/lib/queryObserver.esm.js +176 -258
  97. package/build/lib/queryObserver.esm.js.map +1 -1
  98. package/build/lib/queryObserver.js +176 -260
  99. package/build/lib/queryObserver.js.map +1 -1
  100. package/build/lib/queryObserver.mjs +176 -258
  101. package/build/lib/queryObserver.mjs.map +1 -1
  102. package/build/lib/removable.d.ts +3 -3
  103. package/build/lib/removable.esm.js +10 -14
  104. package/build/lib/removable.esm.js.map +1 -1
  105. package/build/lib/removable.js +10 -16
  106. package/build/lib/removable.js.map +1 -1
  107. package/build/lib/removable.mjs +10 -14
  108. package/build/lib/removable.mjs.map +1 -1
  109. package/build/lib/retryer.d.ts +5 -5
  110. package/build/lib/retryer.esm.js +27 -44
  111. package/build/lib/retryer.esm.js.map +1 -1
  112. package/build/lib/retryer.js +27 -46
  113. package/build/lib/retryer.js.map +1 -1
  114. package/build/lib/retryer.mjs +27 -44
  115. package/build/lib/retryer.mjs.map +1 -1
  116. package/build/lib/subscribable.esm.js +4 -7
  117. package/build/lib/subscribable.esm.js.map +1 -1
  118. package/build/lib/subscribable.js +4 -9
  119. package/build/lib/subscribable.js.map +1 -1
  120. package/build/lib/subscribable.mjs +4 -7
  121. package/build/lib/subscribable.mjs.map +1 -1
  122. package/build/lib/tests/utils.d.ts +3 -12
  123. package/build/lib/types.d.ts +111 -99
  124. package/build/lib/utils.d.ts +8 -18
  125. package/build/lib/utils.esm.js +39 -132
  126. package/build/lib/utils.esm.js.map +1 -1
  127. package/build/lib/utils.js +42 -144
  128. package/build/lib/utils.js.map +1 -1
  129. package/build/lib/utils.mjs +39 -132
  130. package/build/lib/utils.mjs.map +1 -1
  131. package/build/umd/index.development.js +868 -1398
  132. package/build/umd/index.development.js.map +1 -1
  133. package/build/umd/index.production.js +1 -1
  134. package/build/umd/index.production.js.map +1 -1
  135. package/package.json +1 -1
  136. package/src/focusManager.ts +17 -24
  137. package/src/index.ts +1 -11
  138. package/src/infiniteQueryBehavior.ts +54 -94
  139. package/src/infiniteQueryObserver.ts +10 -12
  140. package/src/mutation.ts +68 -92
  141. package/src/mutationCache.ts +27 -27
  142. package/src/mutationObserver.ts +60 -97
  143. package/src/onlineManager.ts +14 -14
  144. package/src/queriesObserver.ts +50 -54
  145. package/src/query.ts +106 -110
  146. package/src/queryCache.ts +42 -41
  147. package/src/queryClient.ts +155 -434
  148. package/src/queryObserver.ts +155 -192
  149. package/src/removable.ts +13 -13
  150. package/src/retryer.ts +5 -5
  151. package/src/tests/focusManager.test.tsx +25 -25
  152. package/src/tests/hydration.test.tsx +167 -81
  153. package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
  154. package/src/tests/infiniteQueryObserver.test.tsx +6 -2
  155. package/src/tests/mutationCache.test.tsx +127 -127
  156. package/src/tests/mutationObserver.test.tsx +1 -31
  157. package/src/tests/mutations.test.tsx +62 -43
  158. package/src/tests/onlineManager.test.tsx +12 -4
  159. package/src/tests/queriesObserver.test.tsx +41 -77
  160. package/src/tests/query.test.tsx +175 -243
  161. package/src/tests/queryCache.test.tsx +170 -93
  162. package/src/tests/queryClient.test.tsx +229 -378
  163. package/src/tests/queryObserver.test.tsx +23 -147
  164. package/src/tests/utils.test.tsx +84 -29
  165. package/src/tests/utils.ts +9 -18
  166. package/src/types.ts +187 -140
  167. package/src/utils.ts +31 -124
  168. package/build/lib/logger.d.ts +0 -8
  169. package/build/lib/logger.esm.js +0 -4
  170. package/build/lib/logger.esm.js.map +0 -1
  171. package/build/lib/logger.js +0 -8
  172. package/build/lib/logger.js.map +0 -1
  173. package/build/lib/logger.mjs +0 -4
  174. package/build/lib/logger.mjs.map +0 -1
  175. package/build/lib/logger.native.d.ts +0 -6
  176. package/build/lib/logger.native.esm.js +0 -12
  177. package/build/lib/logger.native.esm.js.map +0 -1
  178. package/build/lib/logger.native.js +0 -16
  179. package/build/lib/logger.native.js.map +0 -1
  180. package/build/lib/logger.native.mjs +0 -12
  181. package/build/lib/logger.native.mjs.map +0 -1
  182. package/src/logger.native.ts +0 -11
  183. package/src/logger.ts +0 -9
@@ -5,9 +5,9 @@ import type { QueryClient } from './queryClient'
5
5
  import { Subscribable } from './subscribable'
6
6
  import type {
7
7
  MutateOptions,
8
- MutationObserverBaseResult,
9
8
  MutationObserverResult,
10
9
  MutationObserverOptions,
10
+ RegisteredError,
11
11
  } from './types'
12
12
  import { shallowEqualObjects } from './utils'
13
13
 
@@ -17,17 +17,11 @@ type MutationObserverListener<TData, TError, TVariables, TContext> = (
17
17
  result: MutationObserverResult<TData, TError, TVariables, TContext>,
18
18
  ) => void
19
19
 
20
- interface NotifyOptions {
21
- listeners?: boolean
22
- onError?: boolean
23
- onSuccess?: boolean
24
- }
25
-
26
20
  // CLASS
27
21
 
28
22
  export class MutationObserver<
29
23
  TData = unknown,
30
- TError = unknown,
24
+ TError = RegisteredError,
31
25
  TVariables = void,
32
26
  TContext = unknown,
33
27
  > extends Subscribable<
@@ -35,15 +29,11 @@ export class MutationObserver<
35
29
  > {
36
30
  options!: MutationObserverOptions<TData, TError, TVariables, TContext>
37
31
 
38
- private client: QueryClient
39
- private currentResult!: MutationObserverResult<
40
- TData,
41
- TError,
42
- TVariables,
43
- TContext
44
- >
45
- private currentMutation?: Mutation<TData, TError, TVariables, TContext>
46
- private mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>
32
+ #client: QueryClient
33
+ #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =
34
+ undefined!
35
+ #currentMutation?: Mutation<TData, TError, TVariables, TContext>
36
+ #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>
47
37
 
48
38
  constructor(
49
39
  client: QueryClient,
@@ -51,10 +41,10 @@ export class MutationObserver<
51
41
  ) {
52
42
  super()
53
43
 
54
- this.client = client
44
+ this.#client = client
55
45
  this.setOptions(options)
56
46
  this.bindMethods()
57
- this.updateResult()
47
+ this.#updateResult()
58
48
  }
59
49
 
60
50
  protected bindMethods(): void {
@@ -66,11 +56,11 @@ export class MutationObserver<
66
56
  options?: MutationObserverOptions<TData, TError, TVariables, TContext>,
67
57
  ) {
68
58
  const prevOptions = this.options
69
- this.options = this.client.defaultMutationOptions(options)
59
+ this.options = this.#client.defaultMutationOptions(options)
70
60
  if (!shallowEqualObjects(prevOptions, this.options)) {
71
- this.client.getMutationCache().notify({
61
+ this.#client.getMutationCache().notify({
72
62
  type: 'observerOptionsUpdated',
73
- mutation: this.currentMutation,
63
+ mutation: this.#currentMutation,
74
64
  observer: this,
75
65
  })
76
66
  }
@@ -78,25 +68,14 @@ export class MutationObserver<
78
68
 
79
69
  protected onUnsubscribe(): void {
80
70
  if (!this.listeners.length) {
81
- this.currentMutation?.removeObserver(this)
71
+ this.#currentMutation?.removeObserver(this)
82
72
  }
83
73
  }
84
74
 
85
75
  onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {
86
- this.updateResult()
87
-
88
- // Determine which callbacks to trigger
89
- const notifyOptions: NotifyOptions = {
90
- listeners: true,
91
- }
92
-
93
- if (action.type === 'success') {
94
- notifyOptions.onSuccess = true
95
- } else if (action.type === 'error') {
96
- notifyOptions.onError = true
97
- }
76
+ this.#updateResult()
98
77
 
99
- this.notify(notifyOptions)
78
+ this.#notify(action)
100
79
  }
101
80
 
102
81
  getCurrentResult(): MutationObserverResult<
@@ -105,101 +84,85 @@ export class MutationObserver<
105
84
  TVariables,
106
85
  TContext
107
86
  > {
108
- return this.currentResult
87
+ return this.#currentResult
109
88
  }
110
89
 
111
90
  reset(): void {
112
- this.currentMutation = undefined
113
- this.updateResult()
114
- this.notify({ listeners: true })
91
+ this.#currentMutation = undefined
92
+ this.#updateResult()
93
+ this.#notify()
115
94
  }
116
95
 
117
96
  mutate(
118
- variables?: TVariables,
97
+ variables: TVariables,
119
98
  options?: MutateOptions<TData, TError, TVariables, TContext>,
120
99
  ): Promise<TData> {
121
- this.mutateOptions = options
100
+ this.#mutateOptions = options
122
101
 
123
- if (this.currentMutation) {
124
- this.currentMutation.removeObserver(this)
125
- }
102
+ this.#currentMutation?.removeObserver(this)
126
103
 
127
- this.currentMutation = this.client.getMutationCache().build(this.client, {
128
- ...this.options,
129
- variables:
130
- typeof variables !== 'undefined' ? variables : this.options.variables,
131
- })
104
+ this.#currentMutation = this.#client
105
+ .getMutationCache()
106
+ .build(this.#client, {
107
+ ...this.options,
108
+ })
132
109
 
133
- this.currentMutation.addObserver(this)
110
+ this.#currentMutation.addObserver(this)
134
111
 
135
- return this.currentMutation.execute()
112
+ return this.#currentMutation.execute(variables)
136
113
  }
137
114
 
138
- private updateResult(): void {
139
- const state = this.currentMutation
140
- ? this.currentMutation.state
141
- : getDefaultState<TData, TError, TVariables, TContext>()
142
-
143
- const result: MutationObserverBaseResult<
144
- TData,
145
- TError,
146
- TVariables,
147
- TContext
148
- > = {
115
+ #updateResult(): void {
116
+ const state =
117
+ this.#currentMutation?.state ??
118
+ getDefaultState<TData, TError, TVariables, TContext>()
119
+
120
+ this.#currentResult = {
149
121
  ...state,
150
- isLoading: state.status === 'loading',
122
+ isPending: state.status === 'pending',
151
123
  isSuccess: state.status === 'success',
152
124
  isError: state.status === 'error',
153
125
  isIdle: state.status === 'idle',
154
126
  mutate: this.mutate,
155
127
  reset: this.reset,
156
- }
157
-
158
- this.currentResult = result as MutationObserverResult<
159
- TData,
160
- TError,
161
- TVariables,
162
- TContext
163
- >
128
+ } as MutationObserverResult<TData, TError, TVariables, TContext>
164
129
  }
165
130
 
166
- private notify(options: NotifyOptions) {
131
+ #notify(action?: Action<TData, TError, TVariables, TContext>): void {
167
132
  notifyManager.batch(() => {
168
133
  // First trigger the mutate callbacks
169
- if (this.mutateOptions && this.hasListeners()) {
170
- if (options.onSuccess) {
171
- this.mutateOptions.onSuccess?.(
172
- this.currentResult.data!,
173
- this.currentResult.variables!,
174
- this.currentResult.context!,
134
+ if (this.#mutateOptions && this.hasListeners()) {
135
+ if (action?.type === 'success') {
136
+ this.#mutateOptions.onSuccess?.(
137
+ action.data,
138
+ this.#currentResult.variables!,
139
+ this.#currentResult.context!,
175
140
  )
176
- this.mutateOptions.onSettled?.(
177
- this.currentResult.data!,
141
+ this.#mutateOptions.onSettled?.(
142
+ action.data,
178
143
  null,
179
- this.currentResult.variables!,
180
- this.currentResult.context,
144
+ this.#currentResult.variables!,
145
+ this.#currentResult.context,
181
146
  )
182
- } else if (options.onError) {
183
- this.mutateOptions.onError?.(
184
- this.currentResult.error!,
185
- this.currentResult.variables!,
186
- this.currentResult.context,
147
+ } else if (action?.type === 'error') {
148
+ this.#mutateOptions.onError?.(
149
+ action.error,
150
+ this.#currentResult.variables!,
151
+ this.#currentResult.context,
187
152
  )
188
- this.mutateOptions.onSettled?.(
153
+ this.#mutateOptions.onSettled?.(
189
154
  undefined,
190
- this.currentResult.error,
191
- this.currentResult.variables!,
192
- this.currentResult.context,
155
+ action.error,
156
+ this.#currentResult.variables!,
157
+ this.#currentResult.context,
193
158
  )
194
159
  }
195
160
  }
196
161
 
197
162
  // Then trigger the listeners
198
- if (options.listeners) {
199
- this.listeners.forEach((listener) => {
200
- listener(this.currentResult)
201
- })
202
- }
163
+ this.listeners.forEach((listener) => {
164
+ listener(this.#currentResult)
165
+ })
203
166
  })
204
167
  }
205
168
  }
@@ -6,14 +6,14 @@ type SetupFn = (
6
6
  ) => (() => void) | undefined
7
7
 
8
8
  export class OnlineManager extends Subscribable {
9
- private online?: boolean
10
- private cleanup?: () => void
9
+ #online?: boolean
10
+ #cleanup?: () => void
11
11
 
12
- private setup: SetupFn
12
+ #setup: SetupFn
13
13
 
14
14
  constructor() {
15
15
  super()
16
- this.setup = (onOnline) => {
16
+ this.#setup = (onOnline) => {
17
17
  // addEventListener does not exist in React Native, but window does
18
18
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
19
19
  if (!isServer && window.addEventListener) {
@@ -34,22 +34,22 @@ export class OnlineManager extends Subscribable {
34
34
  }
35
35
 
36
36
  protected onSubscribe(): void {
37
- if (!this.cleanup) {
38
- this.setEventListener(this.setup)
37
+ if (!this.#cleanup) {
38
+ this.setEventListener(this.#setup)
39
39
  }
40
40
  }
41
41
 
42
42
  protected onUnsubscribe() {
43
43
  if (!this.hasListeners()) {
44
- this.cleanup?.()
45
- this.cleanup = undefined
44
+ this.#cleanup?.()
45
+ this.#cleanup = undefined
46
46
  }
47
47
  }
48
48
 
49
49
  setEventListener(setup: SetupFn): void {
50
- this.setup = setup
51
- this.cleanup?.()
52
- this.cleanup = setup((online?: boolean) => {
50
+ this.#setup = setup
51
+ this.#cleanup?.()
52
+ this.#cleanup = setup((online?: boolean) => {
53
53
  if (typeof online === 'boolean') {
54
54
  this.setOnline(online)
55
55
  } else {
@@ -59,7 +59,7 @@ export class OnlineManager extends Subscribable {
59
59
  }
60
60
 
61
61
  setOnline(online?: boolean): void {
62
- this.online = online
62
+ this.#online = online
63
63
 
64
64
  if (online) {
65
65
  this.onOnline()
@@ -73,8 +73,8 @@ export class OnlineManager extends Subscribable {
73
73
  }
74
74
 
75
75
  isOnline(): boolean {
76
- if (typeof this.online === 'boolean') {
77
- return this.online
76
+ if (typeof this.#online === 'boolean') {
77
+ return this.#online
78
78
  }
79
79
 
80
80
  if (
@@ -1,4 +1,3 @@
1
- import { difference, replaceAt } from './utils'
2
1
  import { notifyManager } from './notifyManager'
3
2
  import type {
4
3
  QueryObserverOptions,
@@ -10,23 +9,33 @@ import type { NotifyOptions } from './queryObserver'
10
9
  import { QueryObserver } from './queryObserver'
11
10
  import { Subscribable } from './subscribable'
12
11
 
12
+ function difference<T>(array1: T[], array2: T[]): T[] {
13
+ return array1.filter((x) => array2.indexOf(x) === -1)
14
+ }
15
+
16
+ function replaceAt<T>(array: T[], index: number, value: T): T[] {
17
+ const copy = array.slice(0)
18
+ copy[index] = value
19
+ return copy
20
+ }
21
+
13
22
  type QueriesObserverListener = (result: QueryObserverResult[]) => void
14
23
 
15
24
  export class QueriesObserver extends Subscribable<QueriesObserverListener> {
16
- private client: QueryClient
17
- private result: QueryObserverResult[]
18
- private queries: QueryObserverOptions[]
19
- private observers: QueryObserver[]
20
- private observersMap: Record<string, QueryObserver>
25
+ #client: QueryClient
26
+ #result: QueryObserverResult[]
27
+ #queries: QueryObserverOptions[]
28
+ #observers: QueryObserver[]
29
+ #observersMap: Record<string, QueryObserver>
21
30
 
22
31
  constructor(client: QueryClient, queries?: QueryObserverOptions[]) {
23
32
  super()
24
33
 
25
- this.client = client
26
- this.queries = []
27
- this.result = []
28
- this.observers = []
29
- this.observersMap = {}
34
+ this.#client = client
35
+ this.#queries = []
36
+ this.#result = []
37
+ this.#observers = []
38
+ this.#observersMap = {}
30
39
 
31
40
  if (queries) {
32
41
  this.setQueries(queries)
@@ -35,9 +44,9 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
35
44
 
36
45
  protected onSubscribe(): void {
37
46
  if (this.listeners.length === 1) {
38
- this.observers.forEach((observer) => {
47
+ this.#observers.forEach((observer) => {
39
48
  observer.subscribe((result) => {
40
- this.onUpdate(observer, result)
49
+ this.#onUpdate(observer, result)
41
50
  })
42
51
  })
43
52
  }
@@ -51,7 +60,7 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
51
60
 
52
61
  destroy(): void {
53
62
  this.listeners = []
54
- this.observers.forEach((observer) => {
63
+ this.#observers.forEach((observer) => {
55
64
  observer.destroy()
56
65
  })
57
66
  }
@@ -60,12 +69,12 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
60
69
  queries: QueryObserverOptions[],
61
70
  notifyOptions?: NotifyOptions,
62
71
  ): void {
63
- this.queries = queries
72
+ this.#queries = queries
64
73
 
65
74
  notifyManager.batch(() => {
66
- const prevObservers = this.observers
75
+ const prevObservers = this.#observers
67
76
 
68
- const newObserverMatches = this.findMatchingObservers(this.queries)
77
+ const newObserverMatches = this.#findMatchingObservers(this.#queries)
69
78
 
70
79
  // set options for the new observers to notify of changes
71
80
  newObserverMatches.forEach((match) =>
@@ -87,9 +96,9 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
87
96
  return
88
97
  }
89
98
 
90
- this.observers = newObservers
91
- this.observersMap = newObserversMap
92
- this.result = newResult
99
+ this.#observers = newObservers
100
+ this.#observersMap = newObserversMap
101
+ this.#result = newResult
93
102
 
94
103
  if (!this.hasListeners()) {
95
104
  return
@@ -101,38 +110,38 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
101
110
 
102
111
  difference(newObservers, prevObservers).forEach((observer) => {
103
112
  observer.subscribe((result) => {
104
- this.onUpdate(observer, result)
113
+ this.#onUpdate(observer, result)
105
114
  })
106
115
  })
107
116
 
108
- this.notify()
117
+ this.#notify()
109
118
  })
110
119
  }
111
120
 
112
121
  getCurrentResult(): QueryObserverResult[] {
113
- return this.result
122
+ return this.#result
114
123
  }
115
124
 
116
125
  getQueries() {
117
- return this.observers.map((observer) => observer.getCurrentQuery())
126
+ return this.#observers.map((observer) => observer.getCurrentQuery())
118
127
  }
119
128
 
120
129
  getObservers() {
121
- return this.observers
130
+ return this.#observers
122
131
  }
123
132
 
124
133
  getOptimisticResult(queries: QueryObserverOptions[]): QueryObserverResult[] {
125
- return this.findMatchingObservers(queries).map((match) =>
134
+ return this.#findMatchingObservers(queries).map((match) =>
126
135
  match.observer.getOptimisticResult(match.defaultedQueryOptions),
127
136
  )
128
137
  }
129
138
 
130
- private findMatchingObservers(
139
+ #findMatchingObservers(
131
140
  queries: QueryObserverOptions[],
132
141
  ): QueryObserverMatch[] {
133
- const prevObservers = this.observers
142
+ const prevObservers = this.#observers
134
143
  const defaultedQueryOptions = queries.map((options) =>
135
- this.client.defaultQueryOptions(options),
144
+ this.#client.defaultQueryOptions(options),
136
145
  )
137
146
 
138
147
  const matchingObservers: QueryObserverMatch[] =
@@ -155,29 +164,16 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
155
164
  !matchedQueryHashes.includes(defaultedOptions.queryHash),
156
165
  )
157
166
 
158
- const unmatchedObservers = prevObservers.filter(
159
- (prevObserver) =>
160
- !matchingObservers.some((match) => match.observer === prevObserver),
161
- )
162
-
163
167
  const getObserver = (options: QueryObserverOptions): QueryObserver => {
164
- const defaultedOptions = this.client.defaultQueryOptions(options)
165
- const currentObserver = this.observersMap[defaultedOptions.queryHash!]
166
- return currentObserver ?? new QueryObserver(this.client, defaultedOptions)
168
+ const defaultedOptions = this.#client.defaultQueryOptions(options)
169
+ const currentObserver = this.#observersMap[defaultedOptions.queryHash!]
170
+ return (
171
+ currentObserver ?? new QueryObserver(this.#client, defaultedOptions)
172
+ )
167
173
  }
168
174
 
169
175
  const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map(
170
- (options, index) => {
171
- if (options.keepPreviousData) {
172
- // return previous data from one of the observers that no longer match
173
- const previouslyUsedObserver = unmatchedObservers[index]
174
- if (previouslyUsedObserver !== undefined) {
175
- return {
176
- defaultedQueryOptions: options,
177
- observer: previouslyUsedObserver,
178
- }
179
- }
180
- }
176
+ (options) => {
181
177
  return {
182
178
  defaultedQueryOptions: options,
183
179
  observer: getObserver(options),
@@ -197,18 +193,18 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
197
193
  .sort(sortMatchesByOrderOfQueries)
198
194
  }
199
195
 
200
- private onUpdate(observer: QueryObserver, result: QueryObserverResult): void {
201
- const index = this.observers.indexOf(observer)
196
+ #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {
197
+ const index = this.#observers.indexOf(observer)
202
198
  if (index !== -1) {
203
- this.result = replaceAt(this.result, index, result)
204
- this.notify()
199
+ this.#result = replaceAt(this.#result, index, result)
200
+ this.#notify()
205
201
  }
206
202
  }
207
203
 
208
- private notify(): void {
204
+ #notify(): void {
209
205
  notifyManager.batch(() => {
210
206
  this.listeners.forEach((listener) => {
211
- listener(this.result)
207
+ listener(this.#result)
212
208
  })
213
209
  })
214
210
  }