Skip to content

Commit 20a38a4

Browse files
committed
fix: auto deps
1 parent 9c5f28d commit 20a38a4

File tree

7 files changed

+70
-92
lines changed

7 files changed

+70
-92
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ dist
33
.DS_Store
44
.cache
55
.temp
6+
.vitepress
67
coverage
78

89

packages/hooks/src/useRequest/docs/refreshDeps/demo/demo1.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
storeId,
6767
count,
6868
})
69-
}, 1000)
69+
}, 3000)
7070
})
7171
}
7272
const id = ref(1)
@@ -75,7 +75,7 @@
7575
})
7676
const count = ref(0)
7777
78-
const ready = computed(() => count.value !== 0 && count.value !== 5)
78+
const ready = computed(() => count.value !== 0)
7979
const { data, loading } = useRequest(
8080
() => getUsername({ id: id.value, storeId: store.id, count: count.value }),
8181
{
@@ -86,9 +86,9 @@
8686
storeId: '-',
8787
},
8888
ready,
89-
pollingInterval: 3000,
90-
pollingWhenHidden: false,
89+
debounceWait: 2000,
9190
refreshDeps: true,
91+
debugKey: 'test',
9292
},
9393
)
9494
</script>

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { ref, computed, watchEffect, unref, } from "vue"
2-
import debounce from "lodash/debounce"
1+
import { computed, watchEffect, unref, } from "vue"
32
import type { UseRequestPlugin } from "../types"
4-
import type { DebouncedFunc, DebounceSettings } from 'lodash'
3+
import type { DebounceSettings } from 'lodash'
4+
import useDebounceFn from "../../useDebounceFn"
5+
56

67
const useDebouncePlugin: UseRequestPlugin<unknown, unknown[]> = (
78
fetchInstance,
89
{ debounceWait, debounceLeading, debounceTrailing, debounceMaxWait },
910
) => {
10-
const debouncedRef = ref<DebouncedFunc<any>>()
11-
1211
const options = computed(() => {
1312
const ops: DebounceSettings = {
1413
leading: unref(debounceLeading),
@@ -19,24 +18,23 @@ const useDebouncePlugin: UseRequestPlugin<unknown, unknown[]> = (
1918
return ops
2019
})
2120

21+
const { run, cancel, flush } = useDebounceFn(callback => {
22+
callback()
23+
unref(debounceWait) ?? 0,
24+
options.value
25+
})
26+
2227
watchEffect(onInvalidate => {
2328
if (unref(debounceWait)) {
2429
const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance)
25-
26-
debouncedRef.value = debounce(
27-
(...args: any[]) => {
28-
return new Promise((resolve, reject) => {
29-
_originRunAsync(...args)
30-
.then(resolve)
31-
.catch(reject)
32-
})
33-
},
34-
unref(debounceWait),
35-
options.value,
36-
)
37-
30+
fetchInstance.runAsync = (...args) => {
31+
return new Promise((resolve, reject) => {
32+
run(() => _originRunAsync(...args).then(resolve).catch(reject))
33+
})
34+
}
3835
onInvalidate(() => {
39-
debouncedRef.value?.cancel()
36+
cancel()
37+
fetchInstance.runAsync = _originRunAsync
4038
})
4139
}
4240
})
@@ -51,8 +49,8 @@ const useDebouncePlugin: UseRequestPlugin<unknown, unknown[]> = (
5149
name: 'debouncePlugin',
5250
onCancel: () => {
5351
// 取消时同时执行 cancel 和 flush,确保不会有遗留的防抖函数
54-
debouncedRef.value?.cancel()
55-
debouncedRef.value?.flush()
52+
cancel()
53+
flush()
5654
},
5755
}
5856
}

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
import { computed, unref, watchEffect, ref } from 'vue'
2-
import { type DebouncedFunc, type ThrottleSettings } from 'lodash'
3-
import throttle from 'lodash/throttle'
1+
import { computed, unref, watchEffect } from 'vue'
2+
import { type ThrottleSettings } from 'lodash'
43
import { UseRequestPlugin } from '../types'
4+
import useThrottleFn from '../../useThrottleFn'
55

66
const useThrottlePlugin: UseRequestPlugin<unknown, unknown[]> = (
77
fetchInstance,
88
{ throttleWait, throttleLeading, throttleTrailing },
99
) => {
1010
const options = computed(() => {
11-
const ops: ThrottleSettings = {
12-
leading: unref(throttleLeading),
13-
trailing: unref(throttleTrailing),
11+
const ops: ThrottleSettings = {}
12+
const leading = unref(throttleLeading)
13+
const trailing = unref(throttleTrailing)
14+
if (leading !== undefined) {
15+
ops.leading = leading;
16+
}
17+
if (trailing !== undefined) {
18+
ops.trailing = trailing;
1419
}
15-
1620
return ops
1721
})
1822

19-
const throttledRef = ref<DebouncedFunc<any>>()
23+
const { run, cancel, flush } = useThrottleFn(callback => {
24+
callback()
25+
unref(throttleWait) ?? 0,
26+
options.value
27+
})
2028

2129
watchEffect(onInvalidate => {
2230
if (unref(throttleWait)) {
2331
const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance)
24-
25-
throttledRef.value = throttle(
26-
(...args: any[]) => {
27-
return new Promise((resolve, reject) => {
28-
_originRunAsync(...args)
29-
.then(resolve)
30-
.catch(reject)
31-
})
32-
},
33-
unref(throttleWait),
34-
options.value,
35-
)
36-
32+
fetchInstance.runAsync = (...args) => {
33+
return new Promise((resolve, reject) => {
34+
run(() => _originRunAsync(...args).then(resolve).catch(reject))
35+
})
36+
}
3737
onInvalidate(() => {
38-
throttledRef.value?.cancel()
38+
cancel()
39+
fetchInstance.runAsync = _originRunAsync
3940
})
4041
}
4142
})
@@ -49,7 +50,8 @@ const useThrottlePlugin: UseRequestPlugin<unknown, unknown[]> = (
4950
return {
5051
name: 'throttlePlugin',
5152
onCancel: () => {
52-
throttledRef.value?.cancel()
53+
cancel()
54+
flush()
5355
},
5456
}
5557
}

packages/hooks/src/useRequest/useRequestImplement.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref, reactive, toRefs, onScopeDispose, inject, UnwrapRef, watchEffect, computed, isRef, unref } from 'vue'
1+
import { ref, reactive, toRefs, onScopeDispose, inject, UnwrapRef, computed, isRef, unref, watch } from 'vue'
22
import Fetch from './Fetch'
33
import { USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY } from './config'
44
import {
@@ -82,14 +82,20 @@ function useRequestImplement<TData, TParams extends any[]>(
8282

8383
const readyComputed = computed(() => isRef(ready) ? ready.value : ready)
8484

85-
watchEffect(() => {
86-
if (!manual) {
85+
const run = fetchInstance.run.bind(fetchInstance)
86+
87+
// watch ready and service, auto collect dependencies
88+
// because service is ref value, so we need watch it, he reference will change when service change
89+
watch([readyComputed, serviceRef.value], (cur) => {
90+
const ready = cur[0]
91+
console.log("zhix", ready);
92+
93+
if (ready && !manual && fetchInstance.options.refreshDeps === true) {
8794
const params = fetchInstance.state.params || options.defaultParams || []
88-
// auto collect
89-
if (readyComputed.value && fetchInstance.options.refreshDeps === true && !!serviceRef.value) {
90-
fetchInstance.run(...(params as TParams))
91-
}
95+
run(...(params as TParams))
9296
}
97+
}, {
98+
immediate: true,
9399
})
94100

95101
// manual
@@ -108,7 +114,7 @@ function useRequestImplement<TData, TParams extends any[]>(
108114
cancel: fetchInstance.cancel.bind(fetchInstance),
109115
refresh: fetchInstance.refresh.bind(fetchInstance),
110116
refreshAsync: fetchInstance.refreshAsync.bind(fetchInstance),
111-
run: fetchInstance.run.bind(fetchInstance),
117+
run,
112118
runAsync: fetchInstance.runAsync.bind(fetchInstance),
113119
mutate: fetchInstance.mutate.bind(fetchInstance),
114120
} as unknown) as useRequestResult<TData, TParams>

packages/hooks/src/useThrottle/index.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,11 @@ export interface UseThrottleOptions {
88
}
99

1010
function useThrottle<T>(value: Ref<T>, options?: UseThrottleOptions) {
11-
const throttled = ref()
11+
const throttled = ref<T>(value.value) as Ref<T>;
12+
const { run } = useThrottleFn(() => (throttled.value = value.value), options);
13+
watch(value, () => run(), { deep: true });
14+
return throttled;
1215

13-
throttled.value = value.value
14-
15-
const { run } = useThrottleFn(() => {
16-
throttled.value = value.value
17-
}, options)
18-
19-
watch(
20-
value,
21-
() => {
22-
run.value()
23-
},
24-
{
25-
immediate: true,
26-
},
27-
)
28-
29-
return throttled
3016
}
3117

3218
export default useThrottle

packages/hooks/src/useThrottleFn/index.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
11
import throttle from 'lodash/throttle'
2-
import { onUnmounted, ref, computed } from 'vue'
32
import { UseThrottleOptions } from '../useThrottle'
43

54
type noop = (...args: any) => any
65

76
function useThrottleFn<T extends noop>(fn: T, options?: UseThrottleOptions) {
8-
const fnRef = ref(fn)
97

108
const wait = options?.wait ?? 1000
11-
12-
const throttled = computed(() =>
13-
throttle(
14-
(...args: Parameters<T>): ReturnType<T> => {
15-
return fnRef.value([...args])
16-
},
17-
wait,
18-
options,
19-
),
20-
)
21-
22-
onUnmounted(() => {
23-
throttled.value.cancel()
24-
})
9+
const throttled = throttle(fn, wait, options)
2510

2611
return {
2712
run: throttled,
28-
cancel: throttled.value.cancel,
29-
flush: throttled.value.flush,
13+
cancel: throttled.cancel,
14+
flush: throttled.flush,
3015
}
3116
}
3217

0 commit comments

Comments
 (0)