Skip to content

Commit 42ae3d7

Browse files
fix: fix bug #246; fix useDebouncePlugin and useThrottlePlugin some problems
1 parent 1029d1a commit 42ae3d7

File tree

2 files changed

+59
-70
lines changed

2 files changed

+59
-70
lines changed
Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,60 @@
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'
55

66
const useDebouncePlugin: UseRequestPlugin<unknown, unknown[]> = (
77
fetchInstance,
8-
{ debounceWait, debounceLeading, debounceTrailing, debounceMaxWait }
8+
{ debounceWait, debounceLeading, debounceTrailing, debounceMaxWait },
99
) => {
10-
const debouncedRef = ref<DebouncedFunc<any>>();
10+
const debouncedRef = ref<DebouncedFunc<any>>()
11+
1112
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),
2417
}
25-
return ret;
26-
});
2718

28-
watchEffect((onInvalidate) => {
19+
return ops
20+
})
21+
22+
watchEffect(onInvalidate => {
2923
if (unref(debounceWait)) {
30-
const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance);
24+
const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance)
25+
3126
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) => {
4129
_originRunAsync(...args)
4230
.then(resolve)
43-
.catch(reject);
44-
});
45-
});
46-
};
31+
.catch(reject)
32+
})
33+
},
34+
unref(debounceWait),
35+
options.value,
36+
)
37+
4738
onInvalidate(() => {
48-
debouncedRef.value?.cancel();
49-
fetchInstance.runAsync = _originRunAsync;
50-
});
39+
debouncedRef.value?.cancel()
40+
})
5141
}
52-
});
42+
})
5343

5444
if (!unref(debounceWait)) {
55-
return {};
45+
return {
46+
name: 'debouncePlugin',
47+
}
5648
}
5749

5850
return {
59-
name: "debouncePlugin",
51+
name: 'debouncePlugin',
6052
onCancel: () => {
61-
debouncedRef.value?.cancel();
53+
// 取消时同时执行 cancel 和 flush,确保不会有遗留的防抖函数
54+
debouncedRef.value?.cancel()
55+
debouncedRef.value?.flush()
6256
},
63-
};
64-
};
57+
}
58+
}
6559

66-
export default useDebouncePlugin;
60+
export default useDebouncePlugin

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

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, unref, watchEffect } from 'vue'
1+
import { computed, unref, watchEffect, ref } from 'vue'
22
import { type DebouncedFunc, type ThrottleSettings } from 'lodash'
33
import throttle from 'lodash/throttle'
44
import { UseRequestPlugin } from '../types'
@@ -8,51 +8,46 @@ const useThrottlePlugin: UseRequestPlugin<unknown, unknown[]> = (
88
{ throttleWait, throttleLeading, throttleTrailing },
99
) => {
1010
const options = computed(() => {
11-
const ret: ThrottleSettings = {}
12-
if (unref(throttleLeading) !== undefined) {
13-
ret.leading = unref(throttleLeading)
11+
const ops: ThrottleSettings = {
12+
leading: unref(throttleLeading),
13+
trailing: unref(throttleTrailing),
1414
}
15-
if (unref(throttleTrailing) !== undefined) {
16-
ret.trailing = unref(throttleTrailing)
17-
}
18-
return ret
15+
16+
return ops
1917
})
2018

21-
const throttledRef = computed<DebouncedFunc<any>>(() =>
22-
throttle(
23-
(callback: () => void) => {
24-
callback()
25-
},
26-
unref(throttleWait),
27-
options.value,
28-
),
29-
)
19+
const throttledRef = ref<DebouncedFunc<any>>()
3020

3121
watchEffect(onInvalidate => {
3222
if (unref(throttleWait)) {
3323
const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance)
34-
fetchInstance.runAsync = (...args) => {
35-
return new Promise((resolve, reject) => {
36-
throttledRef.value?.(() => {
24+
25+
throttledRef.value = throttle(
26+
(...args: any[]) => {
27+
return new Promise((resolve, reject) => {
3728
_originRunAsync(...args)
3829
.then(resolve)
3930
.catch(reject)
4031
})
41-
})
42-
}
32+
},
33+
unref(throttleWait),
34+
options.value,
35+
)
36+
4337
onInvalidate(() => {
44-
fetchInstance.runAsync = _originRunAsync
4538
throttledRef.value?.cancel()
4639
})
4740
}
4841
})
4942

5043
if (!unref(throttleWait)) {
51-
return {}
44+
return {
45+
name: 'throttlePlugin',
46+
}
5247
}
5348

5449
return {
55-
name: "throttlePlugin",
50+
name: 'throttlePlugin',
5651
onCancel: () => {
5752
throttledRef.value?.cancel()
5853
},

0 commit comments

Comments
 (0)