katanakit-js / QueryObserver
Class: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>
Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:28
A QueryObserver watches a single query in the QueryCache and computes a QueryObserverResult from its state, recomputing and notifying subscribers whenever the underlying query (or the observer's options) changes. It is the primitive that framework adapters (e.g. useQuery) build their hooks on top of, but it can also be used directly to observe and switch between queries outside of any framework.
Example
const observer = new QueryObserver(queryClient, {
queryKey: ['posts'],
queryFn: fetchPosts,
})
const unsubscribe = observer.subscribe((result) => {
console.log(result.data)
})Extends
Subscribable<QueryObserverListener<TData,TError>>
Extended by
Type Parameters
| Type Parameter | Default type |
|---|---|
TQueryFnData | unknown |
TError | DefaultError |
TData | TQueryFnData |
TQueryData | TQueryFnData |
TQueryKey extends QueryKey | QueryKey |
Constructors
Constructor
new QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>(client, options): QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:31
Parameters
| Parameter | Type |
|---|---|
client | QueryClient |
options | QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey> |
Returns
QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>
Overrides
Subscribable<QueryObserverListener<TData, TError>>.constructorProperties
| Property | Modifier | Type | Inherited from | Defined in |
|---|---|---|---|---|
listeners | protected | Set<QueryObserverListener<TData, TError>> | Subscribable.listeners | node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:8 |
options | public | QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey> | - | node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:30 |
Methods
bindMethods()
protected bindMethods(): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:32
Returns
void
createResult()
protected createResult(query, options): QueryObserverResult<TData, TError>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:135
Parameters
| Parameter | Type |
|---|---|
query | Query<TQueryFnData, TError, TQueryData, TQueryKey> |
options | QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey> |
Returns
QueryObserverResult<TData, TError>
destroy()
destroy(): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:52
Stops observing the current query: clears all listeners, cancels the stale and refetch-interval timers, and removes this observer from the query it was observing.
Returns
void
fetch()
protected fetch(fetchOptions): Promise<QueryObserverResult<TData, TError>>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:134
Parameters
| Parameter | Type |
|---|---|
fetchOptions | ObserverFetchOptions |
Returns
Promise<QueryObserverResult<TData, TError>>
fetchOptimistic()
fetchOptimistic(options): Promise<QueryObserverResult<TData, TError>>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:133
Fetches a query defined by the given options without affecting this observer's own tracked query or result, and returns a promise that resolves with the QueryObserverResult for that fetch. This is useful for prefetching data that another observer (e.g. a query about to be navigated to) will need, ahead of time.
Parameters
| Parameter | Type |
|---|---|
options | QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey> |
Returns
Promise<QueryObserverResult<TData, TError>>
Example
const result = await observer.fetchOptimistic({
queryKey: ['posts', 2],
queryFn: () => fetchPost(2),
})
console.log(result.data)getCurrentQuery()
getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:105
Returns the Query instance this observer is currently observing.
Returns
Query<TQueryFnData, TError, TQueryData, TQueryKey>
getCurrentResult()
getCurrentResult(): QueryObserverResult<TData, TError>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:87
Returns the most recently computed QueryObserverResult for the observed query. This is a point-in-time read; to be notified of updates as they happen, subscribe to the observer instead (its inherited subscribe method).
Returns
QueryObserverResult<TData, TError>
Example
const result = observer.getCurrentResult()
console.log(result.status, result.data)getOptimisticResult()
getOptimisticResult(options): QueryObserverResult<TData, TError>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:74
Computes the result the observer would produce for the given (already-defaulted) options right now, building the underlying Query if it doesn't exist yet, without waiting for a subscription callback. Called by framework adapters on every render (e.g. useQuery) so the returned value is available synchronously, ahead of setOptions triggering an actual fetch.
Parameters
| Parameter | Type |
|---|---|
options | DefaultedQueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey> |
Returns
QueryObserverResult<TData, TError>
hasListeners()
hasListeners(): boolean;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:28
Returns true while at least one listener is registered, false once they have all unsubscribed.
Returns
boolean
Inherited from
Subscribable.hasListenersonSubscribe()
protected onSubscribe(): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:33
Returns
void
Overrides
Subscribable.onSubscribeonUnsubscribe()
protected onUnsubscribe(): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:34
Returns
void
Overrides
Subscribable.onUnsubscriberefetch()
refetch(__namedParameters?): Promise<QueryObserverResult<TData, TError>>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:116
Refetches the observed query and returns a promise that resolves with the resulting QueryObserverResult.
Parameters
| Parameter | Type |
|---|---|
__namedParameters? | RefetchOptions |
Returns
Promise<QueryObserverResult<TData, TError>>
Example
const result = await observer.refetch({ cancelRefetch: false })
console.log(result.data)setOptions()
setOptions(options): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:67
Updates the observer's options. This will re-resolve the query being observed (switching to a different query if the queryKey changed), trigger a fetch if the new options require one and the observer has subscribers, recompute the current result, and reschedule the stale and refetch-interval timers as needed.
Parameters
| Parameter | Type |
|---|---|
options | QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey> |
Returns
void
Example
observer.setOptions({ queryKey: ['posts', 1], queryFn: () => fetchPost(1) })
// later: switch to a different query, reusing the same observer
observer.setOptions({ queryKey: ['posts', 2], queryFn: () => fetchPost(2) })shouldFetchOnReconnect()
shouldFetchOnReconnect(): boolean;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:40
Returns whether the observed query is currently stale and configured (via the refetchOnReconnect option) to refetch when the network reconnects.
Returns
boolean
shouldFetchOnWindowFocus()
shouldFetchOnWindowFocus(): boolean;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:46
Returns whether the observed query is currently stale and configured (via the refetchOnWindowFocus option) to refetch when the window regains focus.
Returns
boolean
subscribe()
subscribe(listener): () => void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:24
Registers a listener to be called on every update this object notifies about. Returns a function that removes the listener again — call it to stop listening. The base class never drops a listener on its own, though some subclasses clear all of theirs in destroy().
Parameters
| Parameter | Type | Description |
|---|---|---|
listener | QueryObserverListener | Called on each update, with whatever the subclass passes to its subscribers. |
Returns
() => void
Example
const unsubscribe = subscribable.subscribe(() => {
// react to the update
})
unsubscribe()Inherited from
Subscribable.subscribetrackProp()
trackProp(key): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:101
Records that the given QueryObserverResult property was read, so a subsequent update only notifies this observer if a tracked property actually changed. Normally called indirectly via QueryObserver#trackResult's proxy; exposed directly for adapters that track property access themselves (e.g. through their own reactivity system) instead of via the proxy.
Parameters
| Parameter | Type |
|---|---|
key | | "data" | "error" | "status" | "isPending" | "isRefetching" | "refetch" | "isPaused" | "isError" | "isLoading" | "isLoadingError" | "isRefetchError" | "isSuccess" | "isPlaceholderData" | "dataUpdatedAt" | "errorUpdatedAt" | "failureCount" | "failureReason" | "errorUpdateCount" | "isFetched" | "isFetchedAfterMount" | "isFetching" | "isInitialLoading" | "isStale" | "isEnabled" | "fetchStatus" |
Returns
void
trackResult()
trackResult(result, onPropTracked?): QueryObserverResult<TData, TError>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:94
Wraps a QueryObserverResult in a Proxy that records which properties are read, via QueryObserver#trackProp (and an optional onPropTracked callback). Used by framework adapters when notifyOnChangeProps is not set, to implement its default "only re-render on properties you actually read" behavior.
Parameters
| Parameter | Type |
|---|---|
result | QueryObserverResult<TData, TError> |
onPropTracked? | (key) => void |
Returns
QueryObserverResult<TData, TError>
updateResult()
updateResult(): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:141
Recomputes and stores the current result from the current query/options, notifying listeners if it changed. Framework adapters call this right after subscribing to make sure no query update was missed in the gap between creating the observer and subscribing to it.
Returns
void