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

Method getOptimisticResult

packages/query-core/src/queryObserver.ts:218–253  ·  view source on GitHub ↗
(
    options: DefaultedQueryObserverOptions<
      TQueryFnData,
      TError,
      TData,
      TQueryData,
      TQueryKey
    >,
  )

Source from the content-addressed store, hash-verified

216 }
217
218 getOptimisticResult(
219 options: DefaultedQueryObserverOptions<
220 TQueryFnData,
221 TError,
222 TData,
223 TQueryData,
224 TQueryKey
225 >,
226 ): QueryObserverResult<TData, TError> {
227 const query = this.#client.getQueryCache().build(this.#client, options)
228
229 const result = this.createResult(query, options)
230
231 if (shouldAssignObserverCurrentProperties(this, result)) {
232 // this assigns the optimistic result to the current Observer
233 // because if the query function changes, useQuery will be performing
234 // an effect where it would fetch again.
235 // When the fetch finishes, we perform a deep data cloning in order
236 // to reuse objects references. This deep data clone is performed against
237 // the `observer.currentResult.data` property
238 // When QueryKey changes, we refresh the query and get new `optimistic`
239 // result, while we leave the `observer.currentResult`, so when new data
240 // arrives, it finds the old `observer.currentResult` which is related
241 // to the old QueryKey. Which means that currentResult and selectData are
242 // out of sync already.
243 // To solve this, we move the cursor of the currentResult every time
244 // an observer reads an optimistic value.
245
246 // When keeping the previous data, the result doesn't change until new
247 // data arrives.
248 this.#currentResult = result
249 this.#currentResultOptions = this.options
250 this.#currentResultState = this.#currentQuery.state
251 }
252 return result
253 }
254
255 getCurrentResult(): QueryObserverResult<TData, TError> {
256 return this.#currentResult

Callers 15

constructorMethod · 0.95
QueryControllerClass · 0.45
syncClientMethod · 0.45
syncClientMethod · 0.45
runFunction · 0.45
getOptimisticResultFunction · 0.45
useBaseQueryFunction · 0.45
useQueriesFunction · 0.45
createBaseQueryFunction · 0.45
injectQueriesFunction · 0.45
useBaseQueryFunction · 0.45
useQueriesFunction · 0.45

Calls 4

createResultMethod · 0.95
getQueryCacheMethod · 0.80
buildMethod · 0.45

Tested by

no test coverage detected