(
options: DefaultedQueryObserverOptions<
TQueryFnData,
TError,
TData,
TQueryData,
TQueryKey
>,
)
| 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 |
no test coverage detected