Open History to see the last commit
1import { notifyManager } from './notifyManager'
2import type {
3 QueryObserverOptions,
4 QueryObserverResult,
5 DefaultedQueryObserverOptions,
6} from './types'
7import type { QueryClient } from './queryClient'
8import type { NotifyOptions } from './queryObserver'
9import { QueryObserver } from './queryObserver'
10import { Subscribable } from './subscribable'
11β
12function difference<T>(array1: T[], array2: T[]): T[] {
13 return array1.filter((x) => array2.indexOf(x) === -1)
14}
15β
16function replaceAt<T>(array: T[], index: number, value: T): T[] {
17 const copy = array.slice(0)
18 copy[index] = value
19 return copy
20}
21β
22type QueriesObserverListener = (result: QueryObserverResult[]) => void
23β
24export class QueriesObserver extends Subscribable<QueriesObserverListener> {
25 #client: QueryClient
26 #result: QueryObserverResult[]
27 #queries: QueryObserverOptions[]
28 #observers: QueryObserver[]
29β
30 constructor(client: QueryClient, queries?: QueryObserverOptions[]) {
31 super()
32β
33 this.#client = client
34 this.#queries = []
35 this.#result = []
36 this.#observers = []
37β
38 if (queries) {
39 this.setQueries(queries)
40 }
41 }
42β
43 protected onSubscribe(): void {
44 if (this.listeners.length === 1) {
45 this.#observers.forEach((observer) => {
46 observer.subscribe((result) => {
47 this.#onUpdate(observer, result)
48 })
49 })
50 }
51 }
52β
53 protected onUnsubscribe(): void {
54 if (!this.listeners.length) {
55 this.destroy()
56 }
57 }
58β
59 destroy(): void {
60 this.listeners = []
61 this.#observers.forEach((observer) => {
62 observer.destroy()
63 })
64 }
65β
66 setQueries(
67 queries: QueryObserverOptions[],
68 notifyOptions?: NotifyOptions,
69 ): void {
70 this.#queries = queries
71β
72 notifyManager.batch(() => {
73 const prevObservers = this.#observers
74β
75 const newObserverMatches = this.#findMatchingObservers(this.#queries)
76β
77 // set options for the new observers to notify of changes
78 newObserverMatches.forEach((match) =>
79 match.observer.setOptions(match.defaultedQueryOptions, notifyOptions),
80 )
81β
82 const newObservers = newObserverMatches.map((match) => match.observer)
83 const newResult = newObservers.map((observer) =>
84 observer.getCurrentResult(),
85 )
86β
87 const hasIndexChange = newObservers.some(
88 (observer, index) => observer !== prevObservers[index],
89 )
90 if (prevObservers.length === newObservers.length && !hasIndexChange) {
91 return
92 }
93β
94 this.#observers = newObservers
95 this.#result = newResult
96β
97 if (!this.hasListeners()) {
98 return
99 }
100β
101 difference(prevObservers, newObservers).forEach((observer) => {
102 observer.destroy()
103 })
104β
105 difference(newObservers, prevObservers).forEach((observer) => {
106 observer.subscribe((result) => {
107 this.#onUpdate(observer, result)
108 })
109 })
110β
111 this.#notify()
112 })
113 }
114β
115 getCurrentResult(): QueryObserverResult[] {
116 return this.#result
117 }
118β
119 getQueries() {
120 return this.#observers.map((observer) => observer.getCurrentQuery())
121 }
122β
123 getObservers() {
124 return this.#observers
125 }
126β
127 getOptimisticResult(queries: QueryObserverOptions[]): QueryObserverResult[] {
128 return this.#findMatchingObservers(queries).map((match) =>
129 match.observer.getOptimisticResult(match.defaultedQueryOptions),
130 )
131 }
132β
133 #findMatchingObservers(
134 queries: QueryObserverOptions[],
135 ): QueryObserverMatch[] {
136 const prevObservers = this.#observers
137 const prevObserversMap = new Map(
138 prevObservers.map((observer) => [observer.options.queryHash, observer]),
139 )
140β
141 const defaultedQueryOptions = queries.map((options) =>
142 this.#client.defaultQueryOptions(options),
143 )
144β
145 const matchingObservers: QueryObserverMatch[] =
146 defaultedQueryOptions.flatMap((defaultedOptions) => {
147 const match = prevObserversMap.get(defaultedOptions.queryHash)
148 if (match != null) {
149 return [{ defaultedQueryOptions: defaultedOptions, observer: match }]
150 }
151 return []
152 })
153β
154 const matchedQueryHashes = new Set(
155 matchingObservers.map((match) => match.defaultedQueryOptions.queryHash),
156 )
157 const unmatchedQueries = defaultedQueryOptions.filter(
158 (defaultedOptions) => !matchedQueryHashes.has(defaultedOptions.queryHash),
159 )
160β
161 const getObserver = (options: QueryObserverOptions): QueryObserver => {
162 const defaultedOptions = this.#client.defaultQueryOptions(options)
163 const currentObserver = this.#observers.find(
164 (o) => o.options.queryHash === defaultedOptions.queryHash,
165 )
166 return (
167 currentObserver ?? new QueryObserver(this.#client, defaultedOptions)
168 )
169 }
170β
171 const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map(
172 (options) => {
173 return {
174 defaultedQueryOptions: options,
175 observer: getObserver(options),
176 }
177 },
178 )
179β
180 const sortMatchesByOrderOfQueries = (
181 a: QueryObserverMatch,
182 b: QueryObserverMatch,
183 ): number =>
184 defaultedQueryOptions.indexOf(a.defaultedQueryOptions) -
185 defaultedQueryOptions.indexOf(b.defaultedQueryOptions)
186β
187 return matchingObservers
188 .concat(newOrReusedObservers)
189 .sort(sortMatchesByOrderOfQueries)
190 }
191β
192 #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {
193 const index = this.#observers.indexOf(observer)
194 if (index !== -1) {
195 this.#result = replaceAt(this.#result, index, result)
196 this.#notify()
197 }
198 }
199β
200 #notify(): void {
201 notifyManager.batch(() => {
202 this.listeners.forEach((listener) => {
203 listener(this.#result)
204 })
205 })
206 }
207}
208β
209type QueryObserverMatch = {
210 defaultedQueryOptions: DefaultedQueryObserverOptions
211 observer: QueryObserver
212}