|
1 | | -import { ref, computed, watchEffect, unref } from "vue"; |
2 | | -import type { DebouncedFunc, DebounceSettings } from "lodash"; |
3 | | -import debounce from "lodash/debounce"; |
4 | | -import type { UseRequestPlugin } from "../types"; |
| 1 | +import { ref, computed, watchEffect, unref, } from "vue" |
| 2 | +import debounce from "lodash/debounce" |
| 3 | +import type { UseRequestPlugin } from "../types" |
| 4 | +import type { DebouncedFunc, DebounceSettings } from 'lodash' |
5 | 5 |
|
6 | 6 | const useDebouncePlugin: UseRequestPlugin<unknown, unknown[]> = ( |
7 | 7 | fetchInstance, |
8 | | - { debounceWait, debounceLeading, debounceTrailing, debounceMaxWait } |
| 8 | + { debounceWait, debounceLeading, debounceTrailing, debounceMaxWait }, |
9 | 9 | ) => { |
10 | | - const debouncedRef = ref<DebouncedFunc<any>>(); |
| 10 | + const debouncedRef = ref<DebouncedFunc<any>>() |
| 11 | + |
11 | 12 | const options = computed(() => { |
12 | | - const ret: DebounceSettings = {}; |
13 | | - const debounceLeading_ = unref(debounceLeading) |
14 | | - const debounceTrailing_ = unref(debounceTrailing) |
15 | | - const debounceMaxWait_ = unref(debounceMaxWait) |
16 | | - if (debounceLeading_ !== undefined) { |
17 | | - ret.leading = debounceLeading_; |
18 | | - } |
19 | | - if (debounceTrailing_ !== undefined) { |
20 | | - ret.trailing = debounceTrailing_ |
21 | | - } |
22 | | - if (debounceMaxWait_ !== undefined) { |
23 | | - ret.maxWait = debounceMaxWait_; |
| 13 | + const ops: DebounceSettings = { |
| 14 | + leading: unref(debounceLeading), |
| 15 | + trailing: unref(debounceTrailing), |
| 16 | + maxWait: unref(debounceMaxWait), |
24 | 17 | } |
25 | | - return ret; |
26 | | - }); |
27 | 18 |
|
28 | | - watchEffect((onInvalidate) => { |
| 19 | + return ops |
| 20 | + }) |
| 21 | + |
| 22 | + watchEffect(onInvalidate => { |
29 | 23 | if (unref(debounceWait)) { |
30 | | - const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance); |
| 24 | + const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance) |
| 25 | + |
31 | 26 | debouncedRef.value = debounce( |
32 | | - (callback: () => void) => { |
33 | | - callback(); |
34 | | - }, |
35 | | - unref(debounceWait), |
36 | | - options.value |
37 | | - ); |
38 | | - fetchInstance.runAsync = (...args) => { |
39 | | - return new Promise((resolve, reject) => { |
40 | | - debouncedRef.value?.(() => { |
| 27 | + (...args: any[]) => { |
| 28 | + return new Promise((resolve, reject) => { |
41 | 29 | _originRunAsync(...args) |
42 | 30 | .then(resolve) |
43 | | - .catch(reject); |
44 | | - }); |
45 | | - }); |
46 | | - }; |
| 31 | + .catch(reject) |
| 32 | + }) |
| 33 | + }, |
| 34 | + unref(debounceWait), |
| 35 | + options.value, |
| 36 | + ) |
| 37 | + |
47 | 38 | onInvalidate(() => { |
48 | | - debouncedRef.value?.cancel(); |
49 | | - fetchInstance.runAsync = _originRunAsync; |
50 | | - }); |
| 39 | + debouncedRef.value?.cancel() |
| 40 | + }) |
51 | 41 | } |
52 | | - }); |
| 42 | + }) |
53 | 43 |
|
54 | 44 | if (!unref(debounceWait)) { |
55 | | - return {}; |
| 45 | + return { |
| 46 | + name: 'debouncePlugin', |
| 47 | + } |
56 | 48 | } |
57 | 49 |
|
58 | 50 | return { |
59 | | - name: "debouncePlugin", |
| 51 | + name: 'debouncePlugin', |
60 | 52 | onCancel: () => { |
61 | | - debouncedRef.value?.cancel(); |
| 53 | + // 取消时同时执行 cancel 和 flush,确保不会有遗留的防抖函数 |
| 54 | + debouncedRef.value?.cancel() |
| 55 | + debouncedRef.value?.flush() |
62 | 56 | }, |
63 | | - }; |
64 | | -}; |
| 57 | + } |
| 58 | +} |
65 | 59 |
|
66 | | -export default useDebouncePlugin; |
| 60 | +export default useDebouncePlugin |
0 commit comments