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