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

Method setQueries

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

Source from the content-addressed store, hash-verified

86 }
87
88 setQueries(
89 queries: Array<QueryObserverOptions>,
90 options?: QueriesObserverOptions<TCombinedResult>,
91 ): void {
92 this.#queries = queries
93 this.#options = options
94
95 if (process.env.NODE_ENV !== 'production') {
96 const queryHashes = queries.map(
97 (query) => this.#client.defaultQueryOptions(query).queryHash,
98 )
99 if (new Set(queryHashes).size !== queryHashes.length) {
100 console.warn(
101 '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.',
102 )
103 }
104 }
105
106 notifyManager.batch(() => {
107 const prevObservers = this.#observers
108
109 const newObserverMatches = this.#findMatchingObservers(this.#queries)
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.#observerMatches = newObserverMatches
138 this.#observers = newObservers
139 }
140
141 this.#result = newResult
142
143 if (!this.hasListeners()) return
144
145 if (hasStructuralChange) {

Callers 9

useQueriesFunction · 0.95
useQueriesFunction · 0.95
constructorMethod · 0.95
refreshOptionsMethod · 0.80
useQueriesFunction · 0.80
injectQueriesFunction · 0.80
useQueriesFunction · 0.80
createQueriesFunction · 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