-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
172 lines (131 loc) · 3.88 KB
/
index.tsx
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
import React, { useEffect } from 'react';
import { createContext, useCallback, useContext, useMemo } from 'react';
import { atom, RecoilRoot, useRecoilState } from 'recoil';
const urlMap = new Map()
const AtomMap = new Map()
const createAtom = (key: string, defaultValue: any) => {
if (AtomMap.has(key)) {
return AtomMap.get(key)
}
const atomItem = atom({
key,
default: defaultValue
})
AtomMap.set(key, atomItem)
return atomItem
}
const createKey = (key: string) => {
const datakey = key + "data"
const loadingKey = key + "loading"
const errorKey = key + 'error'
const messageKey = key + 'message'
return [datakey, loadingKey, errorKey, messageKey]
}
const SRContext = createContext({ urlMap, AtomMap, createAtom, createKey })
export const SRProvider: React.FC<{ children: any }> = ({ children }) => {
return (
<SRContext.Provider value={{ urlMap, AtomMap, createAtom, createKey }}>
{children}
</SRContext.Provider>
)
}
export const SingleRoot: React.FC<{ children: any }> = ({ children }) => {
return (
<RecoilRoot>
<SRProvider>
{children}
</SRProvider>
</RecoilRoot>
)
}
interface singleRequestConfig {
awaitData: any
}
export const useSingleRequest = (
urlkey: string,
request: (params?: any) => Promise<any>,
formater?: (res: any) => any,
config?: {
log?: boolean,
refreshInterval?: number
}
) => {
const { urlMap, createAtom, createKey } = useContext(SRContext)
const [datakey, loadingKey, errorKey, messageKey] = useMemo(() => {
return createKey(urlkey)
}, [createKey, urlkey])
const dataAtom = createAtom(datakey, undefined)
const loadingAtom = createAtom(loadingKey, true)
const errorAtom = createAtom(errorKey, false)
const messageAtom = createAtom(messageKey, "")
const [data, setData] = useRecoilState<any>(dataAtom)
const [isLoading, setLoading] = useRecoilState<boolean>(loadingAtom);
const [isError, setError] = useRecoilState<boolean>(errorAtom);
const [errorMessage, setErrorMessage] = useRecoilState<string>(messageAtom);
useEffect(() => {
if (!urlMap.has(urlkey)) {
urlMap.set(urlkey, undefined)
if (config?.log) {
console.log(urlkey, "run")
}
request()
.then((res) => {
const data = formater ? formater(res) : res;
setData(data);
setLoading(false);
urlMap.set(urlkey, data);
// console.log({ urlMap })
})
.catch((reason) => {
setErrorMessage(reason);
setError(true);
});
}
}, [config?.log, formater, request, setData, setError, setErrorMessage, setLoading, urlMap, urlkey])
// useEffect(() => { //被recoil维护数据 不再需要
// if (!data && urlMap.has(urlkey)) {
// if (urlMap.get(urlkey)) {
// if (config?.log) {
// console.log(urlkey, "getCacheData")
// }
// setData(urlMap.get(urlkey))
// setLoading(false)
// }
// }
// }, [config?.log, data, setData, setLoading, urlMap, urlkey])
const runRequest = useCallback((params?: any) => {
request(params)
.then((res) => {
const data = formater ? formater(res) : res;
setData(data);
setLoading(false);
urlMap.set(urlkey, data);
})
.catch((reason) => {
setErrorMessage(reason);
setError(true);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [urlkey])
useEffect(() => {
if (config?.refreshInterval) {
const timer = setInterval(() => {
if (config.log) {
console.log(urlkey, "refreshInterval run")
}
runRequest()
}, config.refreshInterval)
return () => {
clearInterval(timer)
}
}
}, [config?.log, config?.refreshInterval, runRequest, urlkey])
return {
data,
setData,
isLoading,
isError,
errorMessage,
runRequest,
};
};