(
queries: Array<QueryObserverOptions>,
options?: QueriesObserverOptions<TCombinedResult>,
)
| 85 | } |
| 86 | |
| 87 | setQueries( |
| 88 | queries: Array<QueryObserverOptions>, |
| 89 | options?: QueriesObserverOptions<TCombinedResult>, |
| 90 | ): void { |
| 91 | this.#queries = queries |
| 92 | this.#options = options |
| 93 | |
| 94 | if (process.env.NODE_ENV !== 'production') { |
| 95 | const queryHashes = queries.map( |
| 96 | (query) => this.#client.defaultQueryOptions(query).queryHash, |
| 97 | ) |
| 98 | if (new Set(queryHashes).size !== queryHashes.length) { |
| 99 | console.warn( |
| 100 | '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.', |
| 101 | ) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | notifyManager.batch(() => { |
| 106 | const prevObservers = this.#observers |
| 107 | |
| 108 | const newObserverMatches = this.#findMatchingObservers(this.#queries) |
| 109 | this.#observerMatches = newObserverMatches |
| 110 | |
| 111 | // set options for the new observers to notify of changes |
| 112 | newObserverMatches.forEach((match) => |
| 113 | match.observer.setOptions(match.defaultedQueryOptions), |
| 114 | ) |
| 115 | |
| 116 | const newObservers = newObserverMatches.map((match) => match.observer) |
| 117 | const newResult = newObservers.map((observer) => |
| 118 | observer.getCurrentResult(), |
| 119 | ) |
| 120 | |
| 121 | const hasLengthChange = prevObservers.length !== newObservers.length |
| 122 | const hasIndexChange = newObservers.some( |
| 123 | (observer, index) => observer !== prevObservers[index], |
| 124 | ) |
| 125 | const hasStructuralChange = hasLengthChange || hasIndexChange |
| 126 | |
| 127 | const hasResultChange = hasStructuralChange |
| 128 | ? true |
| 129 | : newResult.some((result, index) => { |
| 130 | const prev = this.#result[index] |
| 131 | return !prev || !shallowEqualObjects(result, prev) |
| 132 | }) |
| 133 | |
| 134 | if (!hasStructuralChange && !hasResultChange) return |
| 135 | |
| 136 | if (hasStructuralChange) { |
| 137 | this.#observers = newObservers |
| 138 | } |
| 139 | |
| 140 | this.#result = newResult |
| 141 | |
| 142 | if (!this.hasListeners()) return |
| 143 | |
| 144 | if (hasStructuralChange) { |
no test coverage detected