-
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.
- Loading branch information
1 parent
ac9ea96
commit 3676e77
Showing
8 changed files
with
978 additions
and
898 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
packages/use-request-plugins/src/useBroadcastChannelPlugin/useBroadcastChannel.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
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
71 changes: 37 additions & 34 deletions
71
packages/use-request/src/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,47 @@ | ||
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 {}; | ||
} | ||
import { ref, unref } from 'vue' | ||
import { Timeout, UseRequestPlugin } from '../types' | ||
|
||
const cancelTimeout = () => { | ||
if (timerRef.value) { | ||
clearTimeout(timerRef.value); | ||
} | ||
}; | ||
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", | ||
onBefore: () => { | ||
cancelTimeout(); | ||
timerRef.value = setTimeout(() => { | ||
fetchInstance.setState({ | ||
name: 'loadingDelayPlugin', | ||
onFinally: () => { | ||
clear() | ||
|
||
const delay = unref(loadingDelay) | ||
|
||
/** | ||
* | ||
* 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: true, | ||
}); | ||
}, (unref(loadingDelay)) as number); | ||
}) | ||
|
||
return { | ||
loading: false, | ||
}; | ||
delayRef.value = setTimeout(() => { | ||
inst.setState({ | ||
loading: false, | ||
}) | ||
}, delay) | ||
} | ||
}, | ||
onFinally: () => { | ||
cancelTimeout(); | ||
onError: () => { | ||
clear() | ||
}, | ||
onCancel: () => { | ||
cancelTimeout(); | ||
}, | ||
}; | ||
}; | ||
} | ||
} | ||
|
||
export default useLoadingDelayPlugin; | ||
export default useLoadingDelayPlugin |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.