Skip to content

Commit e3240f0

Browse files
authored
feat(core): make observers on Query a public property (#7326)
1 parent f025a7c commit e3240f0

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

packages/query-core/src/query.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class Query<
160160
#revertState?: QueryState<TData, TError>
161161
#cache: QueryCache
162162
#retryer?: Retryer<TData>
163-
#observers: Array<QueryObserver<any, any, any, any, any>>
163+
observers: Array<QueryObserver<any, any, any, any, any>>
164164
#defaultOptions?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
165165
#abortSignalConsumed: boolean
166166

@@ -170,7 +170,7 @@ export class Query<
170170
this.#abortSignalConsumed = false
171171
this.#defaultOptions = config.defaultOptions
172172
this.setOptions(config.options)
173-
this.#observers = []
173+
this.observers = []
174174
this.#cache = config.cache
175175
this.queryKey = config.queryKey
176176
this.queryHash = config.queryHash
@@ -191,7 +191,7 @@ export class Query<
191191
}
192192

193193
protected optionalRemove() {
194-
if (!this.#observers.length && this.state.fetchStatus === 'idle') {
194+
if (!this.observers.length && this.state.fetchStatus === 'idle') {
195195
this.#cache.remove(this)
196196
}
197197
}
@@ -238,9 +238,7 @@ export class Query<
238238
}
239239

240240
isActive(): boolean {
241-
return this.#observers.some(
242-
(observer) => observer.options.enabled !== false,
243-
)
241+
return this.observers.some((observer) => observer.options.enabled !== false)
244242
}
245243

246244
isDisabled(): boolean {
@@ -253,7 +251,7 @@ export class Query<
253251
}
254252

255253
if (this.getObserversCount() > 0) {
256-
return this.#observers.some(
254+
return this.observers.some(
257255
(observer) => observer.getCurrentResult().isStale,
258256
)
259257
}
@@ -270,7 +268,7 @@ export class Query<
270268
}
271269

272270
onFocus(): void {
273-
const observer = this.#observers.find((x) => x.shouldFetchOnWindowFocus())
271+
const observer = this.observers.find((x) => x.shouldFetchOnWindowFocus())
274272

275273
observer?.refetch({ cancelRefetch: false })
276274

@@ -279,7 +277,7 @@ export class Query<
279277
}
280278

281279
onOnline(): void {
282-
const observer = this.#observers.find((x) => x.shouldFetchOnReconnect())
280+
const observer = this.observers.find((x) => x.shouldFetchOnReconnect())
283281

284282
observer?.refetch({ cancelRefetch: false })
285283

@@ -288,8 +286,8 @@ export class Query<
288286
}
289287

290288
addObserver(observer: QueryObserver<any, any, any, any, any>): void {
291-
if (!this.#observers.includes(observer)) {
292-
this.#observers.push(observer)
289+
if (!this.observers.includes(observer)) {
290+
this.observers.push(observer)
293291

294292
// Stop the query from being garbage collected
295293
this.clearGcTimeout()
@@ -299,10 +297,10 @@ export class Query<
299297
}
300298

301299
removeObserver(observer: QueryObserver<any, any, any, any, any>): void {
302-
if (this.#observers.includes(observer)) {
303-
this.#observers = this.#observers.filter((x) => x !== observer)
300+
if (this.observers.includes(observer)) {
301+
this.observers = this.observers.filter((x) => x !== observer)
304302

305-
if (!this.#observers.length) {
303+
if (!this.observers.length) {
306304
// If the transport layer does not support cancellation
307305
// we'll let the query continue so the result can be cached
308306
if (this.#retryer) {
@@ -321,7 +319,7 @@ export class Query<
321319
}
322320

323321
getObserversCount(): number {
324-
return this.#observers.length
322+
return this.observers.length
325323
}
326324

327325
invalidate(): void {
@@ -354,7 +352,7 @@ export class Query<
354352
// Use the options from the first observer with a query function if no function is found.
355353
// This can happen when the query is hydrated or created with setQueryData.
356354
if (!this.options.queryFn) {
357-
const observer = this.#observers.find((x) => x.options.queryFn)
355+
const observer = this.observers.find((x) => x.options.queryFn)
358356
if (observer) {
359357
this.setOptions(observer.options)
360358
}
@@ -608,7 +606,7 @@ export class Query<
608606
this.state = reducer(this.state)
609607

610608
notifyManager.batch(() => {
611-
this.#observers.forEach((observer) => {
609+
this.observers.forEach((observer) => {
612610
observer.onQueryUpdate()
613611
})
614612

0 commit comments

Comments
 (0)