Skip to content

Commit ac9ea96

Browse files
fix: useRequest useLoadingDelayPlugin plugin not clear timeout when unmount (#199)
* docs: update COMMIT.md and COMMIT-zh-CN.md * fix: fix useRequest useLoadingDelay plugin bug * docs: opt * fix: fix useRequest useLoadingDelayPlugin plugin not clear timeout when unmount --------- Co-authored-by: YongGit <1013588891@qq.com>
1 parent b49ea6f commit ac9ea96

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,42 @@ import { Timeout, UseRequestPlugin } from '../types'
44
const useLoadingDelayPlugin: UseRequestPlugin<unknown, unknown[]> = (inst, { loadingDelay }) => {
55
const delayRef = ref<Timeout>()
66

7+
const clear = () => {
8+
if (delayRef.value) {
9+
clearTimeout(unref(delayRef.value))
10+
11+
delayRef.value = undefined
12+
}
13+
}
14+
715
return {
816
name: 'loadingDelayPlugin',
917
onFinally: () => {
10-
if (delayRef.value) {
11-
clearTimeout(unref(delayRef.value))
18+
clear()
1219

13-
delayRef.value = undefined
14-
}
20+
const delay = unref(loadingDelay)
1521

16-
inst.setState({
17-
loading: true,
18-
})
19-
setTimeout(() => {
22+
/**
23+
*
24+
* if loadingDelay is set, the loading state will be delayed,
25+
* until the delay time is reached.
26+
*
27+
* if delay is set to 0, the loading state will not be delayed. because 0 is mean nothing.
28+
*/
29+
if (delay) {
2030
inst.setState({
21-
loading: false,
31+
loading: true,
2232
})
23-
}, unref(loadingDelay))
33+
34+
delayRef.value = setTimeout(() => {
35+
inst.setState({
36+
loading: false,
37+
})
38+
}, delay)
39+
}
40+
},
41+
onError: () => {
42+
clear()
2443
},
2544
}
2645
}

0 commit comments

Comments
 (0)