Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Sep 6, 2024
1 parent 17b4743 commit 55b6c19
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
20 changes: 8 additions & 12 deletions packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { notifyManager } from './notifyManager'
import { Removable } from './removable'
import { canFetch, createRetryer, isCancelledError } from './retryer'
import {
ensureQueryFn,
noop,
replaceData,
resolveEnabled,
timeUntilStale,
} from './utils'
import type { QueryCache } from './queryCache'
import type { QueryObserver } from './queryObserver'
import type { Retryer } from './retryer'
import { notifyManager } from './notifyManager'
import { canFetch, createRetryer, isCancelledError } from './retryer'
import { Removable } from './removable'
import type {
CancelOptions,
DefaultError,
Expand All @@ -25,6 +22,9 @@ import type {
QueryStatus,
SetDataOptions,
} from './types'
import type { QueryCache } from './queryCache'
import type { QueryObserver } from './queryObserver'
import type { Retryer } from './retryer'

// TYPES

Expand Down Expand Up @@ -300,7 +300,7 @@ export class Query<
}

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

// Stop the query from being garbage collected
Expand All @@ -310,12 +310,8 @@ export class Query<
}
}

hasObserver(observer: QueryObserver<any, any, any, any, any>): boolean {
return this.observers.includes(observer)
}

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

if (!this.observers.length) {
Expand Down
20 changes: 4 additions & 16 deletions packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,6 @@ export class QueryObserver<

this.bindMethods()
this.setOptions(options)

const query = this.#client.getQueryCache().find(this.options)

if (query?.promise) {
// If there is an existing promise, we tap into it to resolve the currentThenable
// This is because `use()` actually unmounts `useQuery()` immediately where the observer is never subscribing
query.promise
.catch(() => {
// if this throws, we don't care
})
.finally(() => {
if (!this.#currentQuery.hasObserver(this)) {
this.onQueryUpdate()
}
})
}
}

protected bindMethods(): void {
Expand Down Expand Up @@ -755,6 +739,10 @@ export class QueryObserver<
})
})
}

isObserved(): boolean {
return this.#currentQuery.hasObserver(this)
}
}

function shouldLoadOnMount(
Expand Down
19 changes: 12 additions & 7 deletions packages/react-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ export function useBaseQuery<

useClearResetErrorBoundary(errorResetBoundary)

if (
!client.getQueryState(options.queryKey) &&
defaultedOptions.enabled !== false
) {
client.prefetchQuery(defaultedOptions)
}

const [observer] = React.useState(
() =>
new Observer<TQueryFnData, TError, TData, TQueryData, TQueryKey>(
Expand All @@ -82,6 +75,18 @@ export function useBaseQuery<
),
)

if (
client.getQueryState(options.queryKey)?.data === undefined &&
defaultedOptions.enabled !== false
) {
client.prefetchQuery(defaultedOptions).finally(() => {
if (!observer.hasListeners()) {
// This allows `.use(query.promise)` to work without having to be subscribed to the query
// This is because `use()` actually unmounts `useQuery()` so the observer is never starts subscribing
observer.onQueryUpdate()
}
})
}
const result = observer.getOptimisticResult(defaultedOptions)

React.useSyncExternalStore(
Expand Down

0 comments on commit 55b6c19

Please sign in to comment.