-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-query.ts
253 lines (232 loc) · 8.49 KB
/
create-query.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { useState } from 'react'
import {
InitQueryOptions,
Maybe,
Query,
QueryState,
QueryStoreApi,
getValue,
hasValue,
identity,
initQuery,
isClient,
} from 'dev-disk'
import { CreateStoresOptions, UseStores, createStores } from './create-stores.ts'
// ----------------------------------------
// Type definitions
export type UseQuery<T extends Query> = UseStores<
QueryState<T>,
T['key'] extends Record<string, any> ? T['key'] : Record<string, never>,
QueryStoreApi<T>
> & {
fetch: ((key?: Maybe<T['key']>) => Promise<QueryState<T>>) & {
cacheFirst: (key?: Maybe<T['key']>) => Promise<QueryState<T>>
}
fetchNextPage: (key?: Maybe<T['key']>) => Promise<QueryState<T>>
reset: (key?: Maybe<T['key']>) => void
invalidate: (key?: Maybe<T['key']>) => void
optimisticUpdate: (param: {
key: Maybe<T['key']>
response: T['response'] | ((prevState: QueryState<T>) => T['response'])
}) => { revert: () => void; invalidate: () => void }
useInitialResponse: (param: {
key: T['key'] extends Record<string, any> ? T['key'] : Record<string, never>
response: T['response']
}) => void
useSuspend: (
key?: (T['key'] extends Record<string, any> ? T['key'] : Record<string, never>) | undefined,
) => Extract<QueryState<T>, { status: 'success' }>
}
export type CreateQueryOptions<T extends Query> = InitQueryOptions<T> &
CreateStoresOptions<
QueryState<T>,
T['key'] extends Record<string, any> ? T['key'] : Record<string, never>
> & {
fetchOnMount?: boolean | 'always' | ((key: T['key']) => boolean | 'always')
fetchOnWindowFocus?: boolean | 'always' | ((key: T['key']) => boolean | 'always')
fetchOnReconnect?: boolean | 'always' | ((key: T['key']) => boolean | 'always')
keepPreviousData?: boolean
gcTime?: number
}
// ----------------------------------------
// Source code
export const createQuery = <T extends Query>(options: CreateQueryOptions<T>): UseQuery<T> => {
const initializer = initQuery(options)
const defaultFetchOnWindowFocus = options.fetchOnMount ?? true
const defaultFetchOnReconnect = options.fetchOnMount ?? true
const {
fetchOnMount = true,
fetchOnWindowFocus = defaultFetchOnWindowFocus,
fetchOnReconnect = defaultFetchOnReconnect,
refetchInterval = false,
keepPreviousData,
gcTime = 5 * 60 * 1000, // 5 minutes
select = identity as NonNullable<InitQueryOptions<T>['select']>,
getNextPageParam = () => undefined,
} = options
const useQuery = createStores(
initializer,
(() => {
const windowFocusHandler = () => {
useQuery.stores.forEach((store) => {
if (store.getSubscribers().size === 0) return
const result = getValue(fetchOnWindowFocus, store.key)
if (result === 'always') store.fetch()
else if (result) store.fetch.cacheFirst()
})
}
const reconnectHandler = () => {
useQuery.stores.forEach((store) => {
if (store.getSubscribers().size === 0) return
const result = getValue(fetchOnReconnect, store.key)
if (result === 'always') store.fetch()
else if (result) store.fetch.cacheFirst()
})
}
return {
...options,
// onFirstSubscribe:
// - Run refetch interval
// - Handle fetchOnWindowFocus & fetchOnReconnect
// - Cancel garbage collection timeout
onFirstSubscribe: (state) => {
const store = useQuery.getStore(state.key)
if (state.isSuccess) {
const refetchIntervalValue = isClient && getValue(refetchInterval, state)
if (refetchIntervalValue) {
store.internal.timeout.refetchInterval = window.setTimeout(
store.fetch,
refetchIntervalValue,
)
}
}
if (isClient) {
if (fetchOnWindowFocus) window.addEventListener('focus', windowFocusHandler)
if (fetchOnReconnect) window.addEventListener('online', reconnectHandler)
}
clearTimeout(store.internal.timeout.garbageCollection)
options.onFirstSubscribe?.(state)
},
// onSubscribe:
// - Handle fetchOnMount
onSubscribe: (state) => {
const store = useQuery.getStore(state.key)
const result = getValue(fetchOnMount, store.key)
if (result === 'always') store.fetch()
else if (result) store.fetch.cacheFirst()
options.onSubscribe?.(state)
},
// onLastUnsubscribe:
// - Start garbage collection timeout
// - Cancel retry, retryNextPage, refetchInterval
// - Reset retry counter
// - Detach window event handler
// (disable fetchOnWindowFocus & fetchOnReconnect since it has no subscribers anymore)
onLastUnsubscribe: (state) => {
let totalSubs = 0
useQuery.stores.forEach((store) => {
if (store.getSubscribers()) totalSubs++
})
if (isClient && totalSubs === 0) {
if (fetchOnWindowFocus) window.removeEventListener('focus', windowFocusHandler)
if (fetchOnReconnect) window.removeEventListener('online', reconnectHandler)
}
const store = useQuery.getStore(state.key)
store.set({ retryCount: 0, retryNextPageCount: 0 })
clearTimeout(store.internal.timeout.retry)
clearTimeout(store.internal.timeout.retryNextPage)
clearTimeout(store.internal.timeout.refetchInterval)
if (isClient && gcTime !== Infinity) {
store.internal.timeout.garbageCollection = window.setTimeout(store.reset, gcTime)
}
options.onLastUnsubscribe?.(state)
},
// onBeforeChangeKey:
// - Handle keepPreviousData
onBeforeChangeKey: (nextKey, prevKey) => {
if (keepPreviousData) {
const pStore = useQuery.getStore(prevKey)
const nStore = useQuery.getStore(nextKey)
const nextData = nStore.get()
const prevData = pStore.get()
if (prevData.data && !nextData.data) {
nStore.set({
status: 'success',
isEmpty: false,
isSuccess: true,
isError: false,
data: prevData.data,
response: prevData.response,
isPreviousData: true,
})
}
}
options.onBeforeChangeKey?.(nextKey, prevKey)
},
}
})(),
)
const fetch = Object.assign((key?: Maybe<T['key']>) => useQuery.getStore(key as any).fetch(), {
cacheFirst: (key?: Maybe<T['key']>) => useQuery.getStore(key as any).fetch.cacheFirst(),
})
const fetchNextPage = (key?: Maybe<T['key']>) => useQuery.getStore(key as any).fetchNextPage()
const reset = (key?: Maybe<T['key']>) => {
if (key) useQuery.getStore(key as any).reset()
else useQuery.stores.forEach((store) => store.reset())
}
const invalidate = (key?: Maybe<T['key']>) => {
if (key) useQuery.getStore(key as any).invalidate()
else useQuery.stores.forEach((store) => store.invalidate())
}
const optimisticUpdate = ({
key,
response,
}: {
key: Maybe<T['key']>
response: T['response'] | ((prevState: QueryState<T>) => T['response'])
}) => useQuery.getStore(key as any).optimisticUpdate(response)
const useInitialResponse = ({
key,
response,
}: {
key: T['key'] extends Record<string, any> ? T['key'] : Record<string, never>
response: T['response']
}) => {
useState(() => {
const store = useQuery.getStore(key)
const state = store.get()
if (response === undefined || state.isSuccess) return
const newPageParam = getNextPageParam(response, state)
store.set({
status: 'success',
isEmpty: false,
isSuccess: true,
isError: false,
response,
responseUpdatedAt: Date.now(),
data: select(response, state),
pageParam: newPageParam,
pageParams: [undefined, newPageParam],
hasNextPage: hasValue(newPageParam),
})
})
}
const useSuspend = (
key?: T['key'] extends Record<string, any> ? T['key'] : Record<string, never>,
): Extract<QueryState<T>, { status: 'success' }> => {
const state = useQuery(key)
const store = useQuery.getStore(key)
if (state.isEmpty) throw store.fetch()
if (state.isError) throw store.get().error
return state
}
return Object.assign(useQuery, {
fetch,
fetchNextPage,
reset,
invalidate,
optimisticUpdate,
useInitialResponse,
useSuspend,
})
}