Open History to see the last commit
1import { difference, getQueryKeyHashFn, replaceAt } from './utils'
2import { notifyManager } from './notifyManager'
3import type { QueryObserverOptions, QueryObserverResult } from './types'
4import type { QueryClient } from './queryClient'
5import { NotifyOptions, QueryObserver } from './queryObserver'
6import { Subscribable } from './subscribable'
7β
8type QueriesObserverListener = (result: QueryObserverResult[]) => void
9β
10export class QueriesObserver extends Subscribable<QueriesObserverListener> {
11 private client: QueryClient
12 private result: QueryObserverResult[]
13 private queries: QueryObserverOptions[]
14 private observers: QueryObserver[]
15β
16 constructor(client: QueryClient, queries?: QueryObserverOptions[]) {
17 super()
18β
19 this.client = client
20 this.queries = queries || []
21 this.result = []
22 this.observers = []
23 this.setQueries(this.queries)
24 }
25β
26 protected onSubscribe(): void {
27 if (this.listeners.length === 1) {
28 this.observers.forEach(observer => {
29 observer.subscribe(result => {
30 this.onUpdate(observer, result)
31 })
32 })
33 }
34 }
35β
36 protected onUnsubscribe(): void {
37 if (!this.listeners.length) {
38 this.destroy()
39 }
40 }
41β
42 destroy(): void {
43 this.listeners = []
44 this.observers.forEach(observer => {
45 observer.destroy()
46 })
47 }
48β
49 setQueries(
50 queries: QueryObserverOptions[],
51 notifyOptions?: NotifyOptions
52 ): void {
53 this.queries = queries
54 this.updateObservers(notifyOptions)
55 }
56β
57 getCurrentResult(): QueryObserverResult[] {
58 return this.result
59 }
60β
61 getOptimisticResult(queries: QueryObserverOptions[]): QueryObserverResult[] {
62 return queries.map((options, i) => {
63 let observer: QueryObserver | undefined = this.observers[i]
64β
65 const defaultedOptions = this.client.defaultQueryObserverOptions(options)
66 const hashFn = getQueryKeyHashFn(defaultedOptions)
67 defaultedOptions.queryHash = hashFn(defaultedOptions.queryKey!)
68β
69 if (
70 !observer ||
71 observer.getCurrentQuery().queryHash !== defaultedOptions.queryHash
72 ) {
73 observer = this.observers.find(
74 x => x.getCurrentQuery().queryHash === defaultedOptions.queryHash
75 )
76 }
77β
78 if (!observer) {
79 observer = new QueryObserver(this.client, defaultedOptions)
80 }
81β
82 return observer.getOptimisticResult(defaultedOptions)
83 })
84 }
85β
86 private updateObservers(notifyOptions?: NotifyOptions): void {
87 notifyManager.batch(() => {
88 let hasIndexChange = false
89β
90 const prevObservers = this.observers
91 const newObservers = this.queries.map((options, i) => {
92 let observer: QueryObserver | undefined = prevObservers[i]
93β
94 const defaultedOptions = this.client.defaultQueryObserverOptions(
95 options
96 )
97 const hashFn = getQueryKeyHashFn(defaultedOptions)
98 defaultedOptions.queryHash = hashFn(defaultedOptions.queryKey!)
99β
100 if (
101 !observer ||
102 observer.getCurrentQuery().queryHash !== defaultedOptions.queryHash
103 ) {
104 hasIndexChange = true
105 observer = prevObservers.find(
106 x => x.getCurrentQuery().queryHash === defaultedOptions.queryHash
107 )
108 }
109β
110 if (observer) {
111 observer.setOptions(defaultedOptions, notifyOptions)
112 return observer
113 }
114β
115 return new QueryObserver(this.client, defaultedOptions)
116 })
117β
118 if (prevObservers.length === newObservers.length && !hasIndexChange) {
119 return
120 }
121β
122 this.observers = newObservers
123 this.result = newObservers.map(observer => observer.getCurrentResult())
124β
125 if (!this.listeners.length) {
126 return
127 }
128β
129 difference(prevObservers, newObservers).forEach(observer => {
130 observer.destroy()
131 })
132β
133 difference(newObservers, prevObservers).forEach(observer => {
134 observer.subscribe(result => {
135 this.onUpdate(observer, result)
136 })
137 })
138β
139 this.notify()
140 })
141 }
142β
143 private onUpdate(observer: QueryObserver, result: QueryObserverResult): void {
144 const index = this.observers.indexOf(observer)
145 if (index !== -1) {
146 this.result = replaceAt(this.result, index, result)
147 this.notify()
148 }
149 }
150β
151 private notify(): void {
152 notifyManager.batch(() => {
153 this.listeners.forEach(listener => {
154 listener(this.result)
155 })
156 })
157 }
158}