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

queriesObserver.ts

158 lines4.3 KB

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 }