(
options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
fetchOptions?: FetchOptions<TQueryFnData>,
)
| 395 | } |
| 396 | |
| 397 | async fetch( |
| 398 | options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>, |
| 399 | fetchOptions?: FetchOptions<TQueryFnData>, |
| 400 | ): Promise<TData> { |
| 401 | if ( |
| 402 | this.state.fetchStatus !== 'idle' && |
| 403 | // If the promise in the retryer is already rejected, we have to definitely |
| 404 | // re-start the fetch; there is a chance that the query is still in a |
| 405 | // pending state when that happens |
| 406 | this.#retryer?.status() !== 'rejected' |
| 407 | ) { |
| 408 | if (this.state.data !== undefined && fetchOptions?.cancelRefetch) { |
| 409 | // Silently cancel current fetch if the user wants to cancel refetch |
| 410 | this.cancel({ silent: true }) |
| 411 | } else if (this.#retryer) { |
| 412 | // make sure that retries that were potentially cancelled due to unmounts can continue |
| 413 | this.#retryer.continueRetry() |
| 414 | // Return current promise if we are already fetching |
| 415 | return this.#retryer.promise |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Update config if passed, otherwise the config from the last execution is used |
| 420 | if (options) { |
| 421 | this.setOptions(options) |
| 422 | } |
| 423 | |
| 424 | // Use the options from the first observer with a query function if no function is found. |
| 425 | // This can happen when the query is hydrated or created with setQueryData. |
| 426 | if (!this.options.queryFn) { |
| 427 | const observer = this.observers.find((x) => x.options.queryFn) |
| 428 | if (observer) { |
| 429 | this.setOptions(observer.options) |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (process.env.NODE_ENV !== 'production') { |
| 434 | if (!Array.isArray(this.options.queryKey)) { |
| 435 | console.error( |
| 436 | `As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']`, |
| 437 | ) |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | const abortController = new AbortController() |
| 442 | |
| 443 | // Adds an enumerable signal property to the object that |
| 444 | // which sets abortSignalConsumed to true when the signal |
| 445 | // is read. |
| 446 | const addSignalProperty = (object: unknown) => { |
| 447 | Object.defineProperty(object, 'signal', { |
| 448 | enumerable: true, |
| 449 | get: () => { |
| 450 | this.#abortSignalConsumed = true |
| 451 | return abortController.signal |
| 452 | }, |
| 453 | }) |
| 454 | } |
no test coverage detected