* Updates the observer's options. This will re-resolve the query being * observed (switching to a different query if the `queryKey` changed), * trigger a fetch if the new options require one and the observer has * subscribers, recompute the current result, and reschedule the stale and *
(
options: QueryObserverOptions<
TQueryFnData,
TError,
TData,
TQueryData,
TQueryKey
>,
)
| 180 | * ``` |
| 181 | */ |
| 182 | setOptions( |
| 183 | options: QueryObserverOptions< |
| 184 | TQueryFnData, |
| 185 | TError, |
| 186 | TData, |
| 187 | TQueryData, |
| 188 | TQueryKey |
| 189 | >, |
| 190 | ): void { |
| 191 | const prevOptions = this.options |
| 192 | const prevQuery = this.#currentQuery |
| 193 | |
| 194 | this.options = this.#client.defaultQueryOptions(options) |
| 195 | |
| 196 | if ( |
| 197 | this.options.enabled !== undefined && |
| 198 | typeof this.options.enabled !== 'boolean' && |
| 199 | typeof this.options.enabled !== 'function' && |
| 200 | typeof resolveQueryValue(this.options.enabled, this.#currentQuery) !== |
| 201 | 'boolean' |
| 202 | ) { |
| 203 | throw new Error( |
| 204 | 'Expected enabled to be a boolean or a callback that returns a boolean', |
| 205 | ) |
| 206 | } |
| 207 | |
| 208 | this.#updateQuery() |
| 209 | this.#currentQuery.setOptions(this.options) |
| 210 | |
| 211 | if ( |
| 212 | prevOptions._defaulted && |
| 213 | !shallowEqualObjects(this.options, prevOptions) |
| 214 | ) { |
| 215 | this.#client.getQueryCache().notify({ |
| 216 | type: 'observerOptionsUpdated', |
| 217 | query: this.#currentQuery, |
| 218 | observer: this, |
| 219 | }) |
| 220 | } |
| 221 | |
| 222 | const mounted = this.hasListeners() |
| 223 | |
| 224 | // Fetch if there are subscribers |
| 225 | if ( |
| 226 | mounted && |
| 227 | shouldFetchOptionally( |
| 228 | this.#currentQuery, |
| 229 | prevQuery, |
| 230 | this.options, |
| 231 | prevOptions, |
| 232 | ) |
| 233 | ) { |
| 234 | this.#executeFetch() |
| 235 | } |
| 236 | |
| 237 | // Update result |
| 238 | this.updateResult() |
| 239 |
no test coverage detected