Skip to content

Commit

Permalink
fix: useRequest useLoadingDelayPlugin plugin not clear timeout when u…
Browse files Browse the repository at this point in the history
…nmount (#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>
  • Loading branch information
XiaoDaiGua-Ray and NelsonYong committed May 13, 2024
1 parent b49ea6f commit ac9ea96
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions packages/hooks/src/useRequest/plugins/useLoadingDelayPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,42 @@ import { Timeout, UseRequestPlugin } from '../types'
const useLoadingDelayPlugin: UseRequestPlugin<unknown, unknown[]> = (inst, { loadingDelay }) => {
const delayRef = ref<Timeout>()

const clear = () => {
if (delayRef.value) {
clearTimeout(unref(delayRef.value))

delayRef.value = undefined
}
}

return {
name: 'loadingDelayPlugin',
onFinally: () => {
if (delayRef.value) {
clearTimeout(unref(delayRef.value))
clear()

delayRef.value = undefined
}
const delay = unref(loadingDelay)

inst.setState({
loading: true,
})
setTimeout(() => {
/**
*
* if loadingDelay is set, the loading state will be delayed,
* until the delay time is reached.
*
* if delay is set to 0, the loading state will not be delayed. because 0 is mean nothing.
*/
if (delay) {
inst.setState({
loading: false,
loading: true,
})
}, unref(loadingDelay))

delayRef.value = setTimeout(() => {
inst.setState({
loading: false,
})
}, delay)
}
},
onError: () => {
clear()
},
}
}
Expand Down

0 comments on commit ac9ea96

Please sign in to comment.