Skip to content

Commit e9ad5aa

Browse files
authored
Merge pull request #46 from InhiblabCore/dev
fix: use-request throttled & debounce dynamic
2 parents 4b428db + 65622f4 commit e9ad5aa

File tree

6 files changed

+81
-73
lines changed

6 files changed

+81
-73
lines changed

packages/hooks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"docs:build-github": "cross-env BASE_URL=/docs/hooks FLOW=github vhp-vitepress build .docs",
2828
"docs:build-gitee": "cross-env BASE_URL=/docs/hooks FLOW=gitee vhp-vitepress build .docs",
2929
"docs:serve": "vitepress serve .docs --host",
30-
"clean": "rimraf dist lib es",
30+
"clean": "rimraf dist lib es types",
3131
"build": "npm run clean && vue-tsc --noEmit && vite build",
3232
"build:types": "vue-tsc --noEmit && vite build --mode fullTypes",
3333
"type": "tsc -d"

packages/hooks/src/useFetchs/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ref, UnwrapRef, watch, watchEffect } from 'vue'
2-
import { Service, Options } from '@/useRequest/types'
2+
import { Service, Options } from '../useRequest/types'
33
import useRequest from '../useRequest'
44

55
const DEFAULT_KEY = 'VUE_HOOKS_PLUS_USE_REQUEST_DEFAULT_KEY'
Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
<template>
2-
<div style="margin-top: 16px;"><input type="text" v-model="text" /></div>
3-
<div style="margin-top: 16px;">读取值:{{ data }}</div>
2+
<div style="margin-top: 16px;"><input type="text" v-model="text"></div>
3+
<div style="margin-top: 16px;">读取值:{{ data }}</div>
44
</template>
55

66
<script lang="ts" setup>
7-
import { ref, watch } from 'vue'
7+
import { ref, watch } from 'vue'
88
9-
import { useRequest } from 'vue-hooks-plus'
9+
import { useRequest, useTimeout } from 'vue-hooks-plus'
1010
11-
function getUsername(): Promise<string> {
12-
return new Promise((resolve, reject) => {
13-
setTimeout(() => {
14-
resolve(`${String(Date.now())}`)
15-
}, 1000)
16-
})
17-
}
11+
function getUsername(): Promise<string> {
12+
console.log('发送请求')
1813
19-
const text = ref('')
14+
return new Promise((resolve, reject) => {
15+
setTimeout(() => {
16+
resolve(`${String(Date.now())}`)
17+
}, 1000)
18+
})
19+
}
2020
21-
const { data, run } = useRequest(() => getUsername(), {
22-
throttleWait: 300,
23-
manual: true,
24-
})
21+
const text = ref('')
2522
26-
watch(text, () => {
27-
run()
28-
})
23+
const throttleWait = ref(500)
24+
25+
useTimeout(() => {
26+
throttleWait.value = 3000
27+
}, 2000)
28+
29+
const { data, run } = useRequest(() => getUsername(), {
30+
throttleWait,
31+
manual: true,
32+
})
33+
34+
watch(text, () => {
35+
run()
36+
})
2937
</script>
3038

3139
<style scoped lang="less"></style>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ const useDebouncePlugin: Plugin<any, any[]> = (
2626
});
2727

2828
watchEffect((onInvalidate) => {
29-
if (debounceWait) {
29+
if (unref(debounceWait)) {
3030
const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance);
3131
debouncedRef.value = debounce(
3232
(callback: any) => {
3333
callback();
3434
},
35-
debounceWait,
35+
unref(debounceWait),
3636
options.value
3737
);
3838
fetchInstance.runAsync = (...args) => {
@@ -51,7 +51,7 @@ const useDebouncePlugin: Plugin<any, any[]> = (
5151
}
5252
});
5353

54-
if (!debounceWait) {
54+
if (!unref(debounceWait)) {
5555
return {};
5656
}
5757

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
import { ref, watchEffect } from "vue";
2-
import type { DebouncedFunc, ThrottleSettings } from "lodash";
3-
import throttle from "lodash/throttle";
4-
import type { Plugin } from "../types";
1+
import { computed, unref, watchEffect } from 'vue'
2+
import { DebouncedFunc, ThrottleSettings } from 'lodash'
3+
import throttle from 'lodash/throttle'
4+
import { Plugin } from '../types'
55

66
const useThrottlePlugin: Plugin<any, any[]> = (
77
fetchInstance,
8-
{ throttleWait, throttleLeading, throttleTrailing }
8+
{ throttleWait, throttleLeading, throttleTrailing },
99
) => {
10-
const throttledRef = ref<DebouncedFunc<any>>();
11-
12-
13-
14-
const options: ThrottleSettings = {};
15-
if (throttleLeading?.value !== undefined) {
16-
options.leading = throttleLeading.value;
17-
}
18-
if (throttleTrailing?.value !== undefined) {
19-
options.trailing = throttleTrailing.value;
20-
}
21-
22-
watchEffect((onInvalidate) => {
23-
if (throttleWait) {
24-
const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance);
25-
26-
throttledRef.value = throttle(
27-
(callback: any) => {
28-
callback();
29-
},
30-
throttleWait,
31-
options
32-
);
10+
const options = computed(() => {
11+
const ret: ThrottleSettings = {}
12+
if (unref(throttleLeading) !== undefined) {
13+
ret.leading = unref(throttleLeading)
14+
}
15+
if (unref(throttleTrailing) !== undefined) {
16+
ret.trailing = unref(throttleTrailing)
17+
}
18+
return ret
19+
})
20+
21+
const throttledRef = computed<DebouncedFunc<any>>(() =>
22+
throttle(
23+
(callback: any) => {
24+
callback()
25+
},
26+
unref(throttleWait),
27+
options.value,
28+
),
29+
)
30+
31+
watchEffect(onInvalidate => {
32+
if (unref(throttleWait)) {
33+
const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance)
3334
fetchInstance.runAsync = (...args) => {
3435
return new Promise((resolve, reject) => {
3536
throttledRef.value?.(() => {
3637
_originRunAsync(...args)
3738
.then(resolve)
38-
.catch(reject);
39-
});
40-
});
41-
};
42-
39+
.catch(reject)
40+
})
41+
})
42+
}
4343
onInvalidate(() => {
44-
fetchInstance.runAsync = _originRunAsync;
45-
throttledRef.value?.cancel();
46-
});
44+
fetchInstance.runAsync = _originRunAsync
45+
throttledRef.value?.cancel()
46+
})
4747
}
48-
});
48+
})
4949

50-
if (!throttleWait) {
51-
return {};
50+
if (!unref(throttleWait)) {
51+
return {}
5252
}
5353

5454
return {
5555
onCancel: () => {
56-
throttledRef.value?.cancel();
56+
throttledRef.value?.cancel()
5757
},
58-
};
59-
};
58+
}
59+
}
6060

61-
export default useThrottlePlugin;
61+
export default useThrottlePlugin

packages/hooks/src/useRequest/types.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ export type Options<TData, TParams extends any[], TPlugin> = {
6868
focusTimespan?: Ref<number> | number
6969

7070
// 防抖
71-
debounceWait?: number
72-
debounceLeading?: Ref<boolean>
73-
debounceTrailing?: Ref<boolean>
71+
debounceWait?: Ref<number> | number
72+
debounceLeading?: Ref<boolean> | boolean
73+
debounceTrailing?: Ref<boolean> | boolean
7474
debounceMaxWait?: Ref<number>
7575

7676
// 节流
77-
throttleWait?: number
78-
throttleLeading?: Ref<boolean>
79-
throttleTrailing?: Ref<boolean>
77+
throttleWait?: Ref<number> | number
78+
throttleLeading?: Ref<boolean> | boolean
79+
throttleTrailing?: Ref<boolean> | boolean
8080

8181
// 请求缓存
8282
cacheKey?: string

0 commit comments

Comments
 (0)