2020-12-21 20:07:48 +08:00
|
|
|
import { onMounted, ref, watch } from 'vue'
|
2021-08-24 13:36:48 +08:00
|
|
|
|
2020-12-21 20:07:48 +08:00
|
|
|
import type { Ref } from 'vue'
|
|
|
|
|
2021-11-29 15:58:44 +08:00
|
|
|
export const useThrottleRender = (loading: Ref<boolean>, throttle = 0) => {
|
2020-12-21 20:07:48 +08:00
|
|
|
if (throttle === 0) return loading
|
|
|
|
const throttled = ref(false)
|
2021-11-29 15:58:44 +08:00
|
|
|
let timeoutHandle = 0
|
2020-12-21 20:07:48 +08:00
|
|
|
|
|
|
|
const dispatchThrottling = () => {
|
|
|
|
if (timeoutHandle) {
|
|
|
|
clearTimeout(timeoutHandle)
|
|
|
|
}
|
|
|
|
timeoutHandle = window.setTimeout(() => {
|
|
|
|
throttled.value = loading.value
|
|
|
|
}, throttle)
|
|
|
|
}
|
|
|
|
onMounted(dispatchThrottling)
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
watch(
|
|
|
|
() => loading.value,
|
|
|
|
(val) => {
|
|
|
|
if (val) {
|
|
|
|
dispatchThrottling()
|
|
|
|
} else {
|
|
|
|
throttled.value = val
|
|
|
|
}
|
2020-12-21 20:07:48 +08:00
|
|
|
}
|
2021-09-04 19:29:28 +08:00
|
|
|
)
|
2020-12-21 20:07:48 +08:00
|
|
|
return throttled
|
|
|
|
}
|