Skip to content

Commit

Permalink
feat(core): make observers on Query a public property (#7326)
Browse files Browse the repository at this point in the history
  • Loading branch information
TkDodo committed Apr 23, 2024
1 parent f025a7c commit e3240f0
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions packages/query-core/src/query.ts
Expand Up @@ -160,7 +160,7 @@ export class Query<
#revertState?: QueryState<TData, TError>
#cache: QueryCache
#retryer?: Retryer<TData>
#observers: Array<QueryObserver<any, any, any, any, any>>
observers: Array<QueryObserver<any, any, any, any, any>>
#defaultOptions?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
#abortSignalConsumed: boolean

Expand All @@ -170,7 +170,7 @@ export class Query<
this.#abortSignalConsumed = false
this.#defaultOptions = config.defaultOptions
this.setOptions(config.options)
this.#observers = []
this.observers = []
this.#cache = config.cache
this.queryKey = config.queryKey
this.queryHash = config.queryHash
Expand All @@ -191,7 +191,7 @@ export class Query<
}

protected optionalRemove() {
if (!this.#observers.length && this.state.fetchStatus === 'idle') {
if (!this.observers.length && this.state.fetchStatus === 'idle') {
this.#cache.remove(this)
}
}
Expand Down Expand Up @@ -238,9 +238,7 @@ export class Query<
}

isActive(): boolean {
return this.#observers.some(
(observer) => observer.options.enabled !== false,
)
return this.observers.some((observer) => observer.options.enabled !== false)
}

isDisabled(): boolean {
Expand All @@ -253,7 +251,7 @@ export class Query<
}

if (this.getObserversCount() > 0) {
return this.#observers.some(
return this.observers.some(
(observer) => observer.getCurrentResult().isStale,
)
}
Expand All @@ -270,7 +268,7 @@ export class Query<
}

onFocus(): void {
const observer = this.#observers.find((x) => x.shouldFetchOnWindowFocus())
const observer = this.observers.find((x) => x.shouldFetchOnWindowFocus())

observer?.refetch({ cancelRefetch: false })

Expand All @@ -279,7 +277,7 @@ export class Query<
}

onOnline(): void {
const observer = this.#observers.find((x) => x.shouldFetchOnReconnect())
const observer = this.observers.find((x) => x.shouldFetchOnReconnect())

observer?.refetch({ cancelRefetch: false })

Expand All @@ -288,8 +286,8 @@ export class Query<
}

addObserver(observer: QueryObserver<any, any, any, any, any>): void {
if (!this.#observers.includes(observer)) {
this.#observers.push(observer)
if (!this.observers.includes(observer)) {
this.observers.push(observer)

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

removeObserver(observer: QueryObserver<any, any, any, any, any>): void {
if (this.#observers.includes(observer)) {
this.#observers = this.#observers.filter((x) => x !== observer)
if (this.observers.includes(observer)) {
this.observers = this.observers.filter((x) => x !== observer)

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

getObserversCount(): number {
return this.#observers.length
return this.observers.length
}

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

notifyManager.batch(() => {
this.#observers.forEach((observer) => {
this.observers.forEach((observer) => {
observer.onQueryUpdate()
})

Expand Down

0 comments on commit e3240f0

Please sign in to comment.