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

Method setQueries

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

Source from the content-addressed store, hash-verified

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

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