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-08-24 13:36:48 +08:00
|
|
|
import type { TimeoutHandle } from '@element-plus/utils/types'
|
2020-12-21 20:07:48 +08:00
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export default function (loading: Ref<boolean>, throttle = 0) {
|
2020-12-21 20:07:48 +08:00
|
|
|
if (throttle === 0) return loading
|
|
|
|
const throttled = ref(false)
|
|
|
|
let timeoutHandle: TimeoutHandle = 0
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|