MCPcopy Create free account
hub / github.com/TanStack/query / QueryObserver

Class QueryObserver

packages/query-core/src/queryObserver.ts:56–872  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54 * ```
55 */
56export class QueryObserver<
57 TQueryFnData = unknown,
58 TError = DefaultError,
59 TData = TQueryFnData,
60 TQueryData = TQueryFnData,
61 TQueryKey extends QueryKey = QueryKey,
62> extends Subscribable<QueryObserverListener<TData, TError>> {
63 #client: QueryClient
64 #currentQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> = undefined!
65 #currentQueryInitialState: QueryState<TQueryData, TError> = undefined!
66 #currentResult: QueryObserverResult<TData, TError> = undefined!
67 #currentResultState?: QueryState<TQueryData, TError>
68 #currentResultOptions?: QueryObserverOptions<
69 TQueryFnData,
70 TError,
71 TData,
72 TQueryData,
73 TQueryKey
74 >
75 #selectError: TError | null
76 #selectFn?: (data: TQueryData) => TData
77 #selectResult?: TData
78 // This property keeps track of the last query with defined data.
79 // It will be used to pass the previous data and query to the placeholder function between renders.
80 #lastQueryWithDefinedData?: Query<TQueryFnData, TError, TQueryData, TQueryKey>
81 #staleTimeoutId?: ManagedTimerId
82 #refetchIntervalId?: ManagedTimerId
83 #currentRefetchInterval?: number | false
84 #trackedProps = new Set<keyof QueryObserverResult>()
85
86 constructor(
87 client: QueryClient,
88 public options: QueryObserverOptions<
89 TQueryFnData,
90 TError,
91 TData,
92 TQueryData,
93 TQueryKey
94 >,
95 ) {
96 super()
97
98 this.#client = client
99 this.#selectError = null
100
101 this.bindMethods()
102 this.setOptions(options)
103 }
104
105 /**
106 * Binds the methods of the result (`refetch`) to the observer, so they keep working when destructured
107 * from it. Subclasses override it to bind their own result methods as well.
108 */
109 protected bindMethods(): void {
110 this.refetch = this.refetch.bind(this)
111 }
112
113 protected override onSubscribe(): void {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected