-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: useRequest useLoadingDelay plugin bug (#198)
* docs: update COMMIT.md and COMMIT-zh-CN.md * fix: fix useRequest useLoadingDelay plugin bug * docs: opt --------- Co-authored-by: YongGit <[email protected]>
- Loading branch information
1 parent
c0afd68
commit 3c351fb
Showing
6 changed files
with
83 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
import { sleep } from 'test-utils/sleep' | ||
import { mount } from '@vue/test-utils' | ||
import useRequest from '../useRequest' | ||
|
||
function getUsername(): Promise<string> { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(`vue-hooks-plus useRequest A ${new Date().getTime()}`) | ||
}, 1) | ||
}) | ||
} | ||
|
||
import Demo from '../docs/loadingDelay/demo/demo.vue' | ||
|
||
describe('useRequest/LoadingDelay', () => { | ||
it(' should data work ', async () => { | ||
const wrapper = mount(Demo) | ||
const text = wrapper.findAll('div').at(1) | ||
const { loading, run: runData } = useRequest(() => getUsername()) | ||
const { loading: loading1, run: runData1 } = useRequest(() => getUsername(), { | ||
loadingDelay: 300, | ||
}) | ||
runData() | ||
runData1() | ||
await sleep(10) | ||
expect(loading.value).toBe(false) | ||
expect(loading1.value).toBe(true) | ||
|
||
expect(text?.text()).toBe('Username:') | ||
await sleep(301) | ||
expect(text?.text()).toBe('Username:loading...') | ||
|
||
await sleep(1000) | ||
expect(text?.text()).toBe('Username:vue-hooks-plus useRequest A') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 22 additions & 38 deletions
60
packages/hooks/src/useRequest/plugins/useLoadingDelayPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,28 @@ | ||
import { unref, ref } from "vue"; | ||
import type { UseRequestPlugin, Timeout } from "../types"; | ||
|
||
const useLoadingDelayPlugin: UseRequestPlugin<unknown, unknown[]> = ( | ||
fetchInstance, | ||
{ loadingDelay } | ||
) => { | ||
const timerRef = ref<Timeout>(); | ||
if (!unref(loadingDelay)) { | ||
return {}; | ||
} | ||
|
||
const cancelTimeout = () => { | ||
if (timerRef.value) { | ||
clearTimeout(timerRef.value); | ||
} | ||
}; | ||
|
||
import { ref, unref } from 'vue' | ||
import { Timeout, UseRequestPlugin } from '../types' | ||
|
||
const useLoadingDelayPlugin: UseRequestPlugin<unknown, unknown[]> = (inst, { loadingDelay }) => { | ||
const delayRef = ref<Timeout>() | ||
|
||
return { | ||
name: "loadingDelayPlugin", | ||
onBefore: () => { | ||
cancelTimeout(); | ||
timerRef.value = setTimeout(() => { | ||
fetchInstance.setState({ | ||
loading: true, | ||
}); | ||
}, (unref(loadingDelay)) as number); | ||
|
||
return { | ||
loading: false, | ||
}; | ||
}, | ||
name: 'loadingDelayPlugin', | ||
onFinally: () => { | ||
cancelTimeout(); | ||
}, | ||
onCancel: () => { | ||
cancelTimeout(); | ||
if (delayRef.value) { | ||
clearTimeout(unref(delayRef.value)) | ||
|
||
delayRef.value = undefined | ||
} | ||
|
||
inst.setState({ | ||
loading: true, | ||
}) | ||
setTimeout(() => { | ||
inst.setState({ | ||
loading: false, | ||
}) | ||
}, unref(loadingDelay)) | ||
}, | ||
}; | ||
}; | ||
} | ||
} | ||
|
||
export default useLoadingDelayPlugin; | ||
export default useLoadingDelayPlugin |