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