element-plus/packages/hooks/use-throttle-render/index.ts
三咲智子 55348b30b6
style: use prettier (#3228)
* style: use prettier

* style: just prettier format, no code changes

* style: eslint fix
object-shorthand, prefer-const

* style: fix no-void

* style: no-console
2021-09-04 19:29:28 +08:00

33 lines
731 B
TypeScript

import { onMounted, ref, watch } from 'vue'
import type { Ref } from 'vue'
import type { TimeoutHandle } from '@element-plus/utils/types'
export default function (loading: Ref<boolean>, throttle = 0) {
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)
watch(
() => loading.value,
(val) => {
if (val) {
dispatchThrottling()
} else {
throttled.value = val
}
}
)
return throttled
}