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

Method getOptimisticResult

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

Source from the content-addressed store, hash-verified

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