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