MCPcopy Create free account
hub / github.com/TanStack/query / setQueries

Method setQueries

packages/query-core/src/queriesObserver.ts:132–200  ·  view source on GitHub ↗

* Replaces the set of queries being observed. Existing `QueryObserver`s * are reused for queries that match an already-observed query hash; * observers for queries that are no longer present are destroyed, and new * observers are created and subscribed to for newly added queries. * @para

(
    queries: Array<QueryObserverOptions>,
    options?: QueriesObserverOptions<TCombinedResult>,
  )

Source from the content-addressed store, hash-verified

130 * ```
131 */
132 setQueries(
133 queries: Array<QueryObserverOptions>,
134 options?: QueriesObserverOptions<TCombinedResult>,
135 ): void {
136 this.#queries = queries
137 this.#options = options
138
139 if (process.env.NODE_ENV !== 'production') {
140 const queryHashes = queries.map(
141 (query) => this.#client.defaultQueryOptions(query).queryHash,
142 )
143 if (new Set(queryHashes).size !== queryHashes.length) {
144 console.warn(
145 '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.',
146 )
147 }
148 }
149
150 notifyManager.batch(() => {
151 const prevObservers = this.#observers
152
153 const newObserverMatches = this.#findMatchingObservers(this.#queries)
154
155 // set options for the new observers to notify of changes
156 newObserverMatches.forEach((match) =>
157 match.observer.setOptions(match.defaultedQueryOptions),
158 )
159
160 const newObservers = newObserverMatches.map((match) => match.observer)
161 const newResult = newObservers.map((observer) =>
162 observer.getCurrentResult(),
163 )
164
165 const hasLengthChange = prevObservers.length !== newObservers.length
166 const hasIndexChange = newObservers.some(
167 (observer, index) => observer !== prevObservers[index],
168 )
169 const hasStructuralChange = hasLengthChange || hasIndexChange
170
171 const hasResultChange = hasStructuralChange
172 ? true
173 : newResult.some((result, index) => {
174 const prev = this.#result[index]
175 return !prev || !shallowEqualObjects(result, prev)
176 })
177
178 if (!hasStructuralChange && !hasResultChange) return
179
180 if (hasStructuralChange) {
181 this.#observerMatches = newObserverMatches
182 this.#observers = newObservers
183 }
184
185 this.#result = newResult
186
187 if (!this.hasListeners()) return
188
189 if (hasStructuralChange) {

Callers 10

useQueriesFunction · 0.95
useQueriesFunction · 0.95
constructorMethod · 0.95
refreshOptionsMethod · 0.80
useQueriesFunction · 0.80
useQueriesFunction · 0.80
injectQueriesFunction · 0.80
getRefetchFunction · 0.80
createQueriesFunction · 0.80

Calls 10

#subscribeToMethod · 0.95
#notifyMethod · 0.95
shallowEqualObjectsFunction · 0.90
differenceFunction · 0.85
defaultQueryOptionsMethod · 0.80
hasListenersMethod · 0.80
setOptionsMethod · 0.45
getCurrentResultMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected