MCPcopy
hub / github.com/TanStack/query / setQueries

Method setQueries

packages/query-core/src/queriesObserver.ts:87–157  ·  view source on GitHub ↗
(
    queries: Array<QueryObserverOptions>,
    options?: QueriesObserverOptions<TCombinedResult>,
  )

Source from the content-addressed store, hash-verified

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) {

Callers 7

useQueriesFunction · 0.95
createQueriesFunction · 0.95
useQueriesFunction · 0.95
constructorMethod · 0.95
useQueriesFunction · 0.80
injectQueriesFunction · 0.80

Calls 11

#onUpdateMethod · 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
subscribeMethod · 0.45

Tested by

no test coverage detected