Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queriesObserver.ts

159 lines4.3 KB

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 }