MCPcopy
hub / github.com/TanStack/query / getOptimisticResult

Method getOptimisticResult

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

Source from the content-addressed store, hash-verified

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

Callers 11

runFunction · 0.45
getOptimisticResultFunction · 0.45
useBaseQueryFunction · 0.45
useQueriesFunction · 0.45
createBaseQueryFunction · 0.45
injectQueriesFunction · 0.45
createBaseQueryFunction · 0.45
useBaseQueryFunction · 0.45
onHydratedFunction · 0.45

Calls 4

createResultMethod · 0.95
getQueryCacheMethod · 0.80
buildMethod · 0.45

Tested by

no test coverage detected