Skip to content

Commit 0a6f6e2

Browse files
committed
fix: use-request optimized type
1 parent 57751fa commit 0a6f6e2

11 files changed

+181
-163
lines changed

packages/hooks/src/useRequest/Fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Ref } from 'vue'
22
import { FetchState, Options, PluginReturn, Service } from './types'
33

4-
export default class Fetch<TData, TParams extends any[] = any> {
4+
export default class Fetch<TData, TParams extends unknown[] = any> {
55
pluginImpls: PluginReturn<TData, TParams>[] | undefined
66

77
count = 0

packages/hooks/src/useRequest/plugins/useAutoRunPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isRef, unref, ref, watch } from 'vue'
22
import { FetchState, Plugin } from '../types'
33

44
// support refreshDeps & ready
5-
const useAutoRunPlugin: Plugin<any, any[]> = (
5+
const useAutoRunPlugin: Plugin<unknown, unknown[]> = (
66
fetchInstance,
77
{ manual, ready = true, defaultParams = [], refreshDeps = [], refreshDepsAction },
88
) => {

packages/hooks/src/useRequest/plugins/useCachePlugin.ts

Lines changed: 127 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -5,140 +5,134 @@ import { CachedData } from '../utils/cache'
55
import * as cachePromise from '../utils/cachePromise'
66
import * as cacheSubscribe from '../utils/cacheSubscribe'
77

8-
const useCachePlugin: Plugin<any, any[]> = (
9-
fetchInstance,
10-
{
11-
cacheKey,
12-
cacheTime = 5 * 60 * 1000,
13-
staleTime = 0,
14-
setCache: customSetCache,
15-
getCache: customGetCache,
16-
}
8+
const useCachePlugin: Plugin<unknown, unknown[]> = (
9+
fetchInstance,
10+
{
11+
cacheKey,
12+
cacheTime = 5 * 60 * 1000,
13+
staleTime = 0,
14+
setCache: customSetCache,
15+
getCache: customGetCache,
16+
},
1717
) => {
18-
const unSubscribeRef = ref<() => void>()
19-
20-
const currentPromiseRef = ref<Promise<any>>()
21-
22-
const _setCache = (key: string, cachedData: CachedData) => {
23-
if (customSetCache) {
24-
customSetCache(cachedData)
25-
} else {
26-
cache.setCache(key, cacheTime, cachedData)
27-
}
28-
cacheSubscribe.trigger(key, cachedData.data)
29-
}
30-
31-
const _getCache = (key: string, params: any[] = []) => {
32-
if (customGetCache) {
33-
return customGetCache(params)
34-
}
35-
return cache.getCache(key)
36-
}
37-
38-
watchEffect(() => {
39-
if (!cacheKey) {
40-
return
41-
}
42-
43-
// 获取初始化的data
44-
const cacheData = _getCache(cacheKey)
45-
if (cacheData && Object.hasOwnProperty.call(cacheData, 'data')) {
46-
fetchInstance.state.data = cacheData.data
47-
fetchInstance.state.params = cacheData.params
48-
49-
console.log('staleTime', staleTime)
50-
51-
if (
52-
staleTime === -1 ||
53-
new Date().getTime() - cacheData.time <= staleTime
54-
) {
55-
fetchInstance.state.loading = false
56-
}
57-
}
58-
59-
// 如果存在相同的cacheKey,触发更新
60-
unSubscribeRef.value = cacheSubscribe.subscribe(cacheKey, (data) => {
61-
fetchInstance.setState({ data })
62-
})
63-
})
64-
65-
onUnmounted(() => {
66-
unSubscribeRef.value?.()
67-
})
68-
69-
if (!cacheKey) {
70-
return {}
71-
}
72-
73-
return {
74-
onBefore: (params) => {
75-
const cacheData = _getCache(cacheKey, params)
76-
77-
if (!cacheData || !Object.hasOwnProperty.call(cacheData, 'data')) {
78-
return {}
79-
}
80-
// 数据是新鲜就停止请求
81-
if (
82-
staleTime === -1 ||
83-
new Date().getTime() - cacheData.time <= staleTime
84-
) {
85-
console.log('停止请求')
86-
87-
return {
88-
loading: false,
89-
data: cacheData?.data,
90-
returnNow: true,
91-
}
92-
} else {
93-
// 数据不新鲜,则返回data,并且继续发送请求
94-
return {
95-
data: cacheData?.data,
96-
}
97-
}
98-
},
99-
onRequest: (service, args) => {
100-
let servicePromise = cachePromise.getCachePromise(cacheKey)
101-
// 如果存在servicePromise,并且它没有被触发,则使用它
102-
if (servicePromise && servicePromise !== currentPromiseRef.value) {
103-
return { servicePromise }
104-
}
105-
106-
servicePromise = service(...args)
107-
currentPromiseRef.value = servicePromise
108-
cachePromise.setCachePromise(cacheKey, servicePromise)
109-
return { servicePromise }
110-
},
111-
onSuccess: (data, params) => {
112-
if (cacheKey) {
113-
// 取消更新,避免反复触发自己
114-
unSubscribeRef.value?.()
115-
_setCache(cacheKey, {
116-
data,
117-
params,
118-
time: new Date().getTime(),
119-
})
120-
// 触发器更新
121-
unSubscribeRef.value = cacheSubscribe.subscribe(cacheKey, (d) => {
122-
fetchInstance.setState({ data: d })
123-
})
124-
}
125-
},
126-
onMutate: (data) => {
127-
if (cacheKey) {
128-
// 取消更新,避免反复触发自己
129-
unSubscribeRef.value?.()
130-
_setCache(cacheKey, {
131-
data,
132-
params: fetchInstance.state.params,
133-
time: new Date().getTime(),
134-
})
135-
// 触发器更新
136-
unSubscribeRef.value = cacheSubscribe.subscribe(cacheKey, (d) => {
137-
fetchInstance.setState({ data: d })
138-
})
139-
}
140-
},
141-
}
18+
const unSubscribeRef = ref<() => void>()
19+
20+
const currentPromiseRef = ref<Promise<any>>()
21+
22+
const _setCache = (key: string, cachedData: CachedData) => {
23+
if (customSetCache) {
24+
customSetCache(cachedData)
25+
} else {
26+
cache.setCache(key, cacheTime, cachedData)
27+
}
28+
cacheSubscribe.trigger(key, cachedData.data)
29+
}
30+
31+
const _getCache = (key: string, params: any[] = []) => {
32+
if (customGetCache) {
33+
return customGetCache(params)
34+
}
35+
return cache.getCache(key)
36+
}
37+
38+
watchEffect(() => {
39+
if (!cacheKey) {
40+
return
41+
}
42+
43+
// 获取初始化的data
44+
const cacheData = _getCache(cacheKey)
45+
if (cacheData && Object.hasOwnProperty.call(cacheData, 'data')) {
46+
fetchInstance.state.data = cacheData.data
47+
fetchInstance.state.params = cacheData.params
48+
49+
console.log('staleTime', staleTime)
50+
51+
if (staleTime === -1 || new Date().getTime() - cacheData.time <= staleTime) {
52+
fetchInstance.state.loading = false
53+
}
54+
}
55+
56+
// 如果存在相同的cacheKey,触发更新
57+
unSubscribeRef.value = cacheSubscribe.subscribe(cacheKey, data => {
58+
fetchInstance.setState({ data })
59+
})
60+
})
61+
62+
onUnmounted(() => {
63+
unSubscribeRef.value?.()
64+
})
65+
66+
if (!cacheKey) {
67+
return {}
68+
}
69+
70+
return {
71+
onBefore: params => {
72+
const cacheData = _getCache(cacheKey, params)
73+
74+
if (!cacheData || !Object.hasOwnProperty.call(cacheData, 'data')) {
75+
return {}
76+
}
77+
// 数据是新鲜就停止请求
78+
if (staleTime === -1 || new Date().getTime() - cacheData.time <= staleTime) {
79+
console.log('停止请求')
80+
81+
return {
82+
loading: false,
83+
data: cacheData?.data,
84+
returnNow: true,
85+
}
86+
} else {
87+
// 数据不新鲜,则返回data,并且继续发送请求
88+
return {
89+
data: cacheData?.data,
90+
}
91+
}
92+
},
93+
onRequest: (service, args) => {
94+
let servicePromise = cachePromise.getCachePromise(cacheKey)
95+
// 如果存在servicePromise,并且它没有被触发,则使用它
96+
if (servicePromise && servicePromise !== currentPromiseRef.value) {
97+
return { servicePromise }
98+
}
99+
100+
servicePromise = service(...args)
101+
currentPromiseRef.value = servicePromise
102+
cachePromise.setCachePromise(cacheKey, servicePromise)
103+
return { servicePromise }
104+
},
105+
onSuccess: (data, params) => {
106+
if (cacheKey) {
107+
// 取消更新,避免反复触发自己
108+
unSubscribeRef.value?.()
109+
_setCache(cacheKey, {
110+
data,
111+
params,
112+
time: new Date().getTime(),
113+
})
114+
// 触发器更新
115+
unSubscribeRef.value = cacheSubscribe.subscribe(cacheKey, d => {
116+
fetchInstance.setState({ data: d })
117+
})
118+
}
119+
},
120+
onMutate: data => {
121+
if (cacheKey) {
122+
// 取消更新,避免反复触发自己
123+
unSubscribeRef.value?.()
124+
_setCache(cacheKey, {
125+
data,
126+
params: fetchInstance.state.params,
127+
time: new Date().getTime(),
128+
})
129+
// 触发器更新
130+
unSubscribeRef.value = cacheSubscribe.subscribe(cacheKey, d => {
131+
fetchInstance.setState({ data: d })
132+
})
133+
}
134+
},
135+
}
142136
}
143137

144138
export default useCachePlugin

packages/hooks/src/useRequest/plugins/useDebouncePlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { DebouncedFunc, DebounceSettings } from "lodash";
33
import debounce from "lodash/debounce";
44
import type { Plugin } from "../types";
55

6-
const useDebouncePlugin: Plugin<any, any[]> = (
6+
const useDebouncePlugin: Plugin<unknown, unknown[]> = (
77
fetchInstance,
88
{ debounceWait, debounceLeading, debounceTrailing, debounceMaxWait }
99
) => {

packages/hooks/src/useRequest/plugins/useLoadingDelayPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { unref, ref } from "vue";
22
import type { Plugin, Timeout } from "../types";
33

4-
const useLoadingDelayPlugin: Plugin<any, any[]> = (
4+
const useLoadingDelayPlugin: Plugin<unknown, unknown[]> = (
55
fetchInstance,
66
{ loadingDelay }
77
) => {

packages/hooks/src/useRequest/plugins/usePollingPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Plugin, Interval } from "../types";
33
import isDocumentVisible from "../utils/isDocumentVisible";
44
import subscribeReVisible from "../utils/subscribeReVisible";
55

6-
const usePollingPlugin: Plugin<any, any[]> = (
6+
const usePollingPlugin: Plugin<unknown, unknown[]> = (
77
fetchInstance,
88
{ pollingInterval, pollingWhenHidden = true, pollingErrorRetryCount = -1 }
99
) => {

packages/hooks/src/useRequest/plugins/useRefreshOnWindowFocusPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Plugin } from "../types";
33
import limit from "../utils/limit";
44
import subscribeFocus from "../utils/subscribeFocus";
55

6-
const useRefreshOnWindowFocusPlugin: Plugin<any, any[]> = (
6+
const useRefreshOnWindowFocusPlugin: Plugin<unknown, unknown[]> = (
77
fetchInstance,
88
{ refreshOnWindowFocus, focusTimespan = 5000 }
99
) => {

packages/hooks/src/useRequest/plugins/useRetryPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ref } from "vue";
22
import type { Plugin, Timeout } from "../types";
33

4-
const useRetryPlugin: Plugin<any, any[]> = (
4+
const useRetryPlugin: Plugin<unknown, unknown[]> = (
55
fetchInstance,
66
{ retryInterval, retryCount }
77
) => {

packages/hooks/src/useRequest/plugins/useThrottlePlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DebouncedFunc, ThrottleSettings } from 'lodash'
33
import throttle from 'lodash/throttle'
44
import { Plugin } from '../types'
55

6-
const useThrottlePlugin: Plugin<any, any[]> = (
6+
const useThrottlePlugin: Plugin<unknown, unknown[]> = (
77
fetchInstance,
88
{ throttleWait, throttleLeading, throttleTrailing },
99
) => {

0 commit comments

Comments
 (0)