element-plus/packages/hooks/use-throttle-render/index.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-12-21 20:07:48 +08:00
import { onMounted, ref, watch } from 'vue'
feat(components): [skeleton] `throttle` supports values ​​as object (#17041) * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * feat: 重构`useThrottleRender`钩子以提高代码可读性和效率 - 简化了对`throttle`参数的判断逻辑,通过`isNumber`函数判断是否为数字 - 将`leadingDispatch`和`trailingDispatch`函数合并为`dispatcher`函数,根据传入的类型判断执行逻辑 - 优化了`watch`回调函数,使用`dispatcher`函数替代重复的判断逻辑 * feat: 写法优化 * feat: 引入`isObject`函数替代原有的`typeof throttle === 'object'`判断方式 * feat: 优化骨架屏文档结构和示例 * feat: 完善文字描述和修改对应的文件名 * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * feat: Optimize code writing * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md * feat: modify doc * feat: md * feat: 补充 useThrottleRender 钩子的测试用例 * feat: 将 SkeletonThrottle 类型移动到hook中, 重命名为 ThrottleType 以提高通用性 --------- Co-authored-by: btea <2356281422@qq.com>
2024-11-07 21:31:16 +08:00
import { isNumber, isObject, isUndefined } from '@element-plus/utils'
2020-12-21 20:07:48 +08:00
import type { Ref } from 'vue'
feat(components): [skeleton] `throttle` supports values ​​as object (#17041) * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * feat: 重构`useThrottleRender`钩子以提高代码可读性和效率 - 简化了对`throttle`参数的判断逻辑,通过`isNumber`函数判断是否为数字 - 将`leadingDispatch`和`trailingDispatch`函数合并为`dispatcher`函数,根据传入的类型判断执行逻辑 - 优化了`watch`回调函数,使用`dispatcher`函数替代重复的判断逻辑 * feat: 写法优化 * feat: 引入`isObject`函数替代原有的`typeof throttle === 'object'`判断方式 * feat: 优化骨架屏文档结构和示例 * feat: 完善文字描述和修改对应的文件名 * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * feat: Optimize code writing * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md * feat: modify doc * feat: md * feat: 补充 useThrottleRender 钩子的测试用例 * feat: 将 SkeletonThrottle 类型移动到hook中, 重命名为 ThrottleType 以提高通用性 --------- Co-authored-by: btea <2356281422@qq.com>
2024-11-07 21:31:16 +08:00
export type ThrottleType =
| { leading?: number; trailing?: number; initVal?: boolean }
| number
export const useThrottleRender = (
loading: Ref<boolean>,
throttle: ThrottleType = 0
) => {
2020-12-21 20:07:48 +08:00
if (throttle === 0) return loading
feat(components): [skeleton] `throttle` supports values ​​as object (#17041) * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * feat: 重构`useThrottleRender`钩子以提高代码可读性和效率 - 简化了对`throttle`参数的判断逻辑,通过`isNumber`函数判断是否为数字 - 将`leadingDispatch`和`trailingDispatch`函数合并为`dispatcher`函数,根据传入的类型判断执行逻辑 - 优化了`watch`回调函数,使用`dispatcher`函数替代重复的判断逻辑 * feat: 写法优化 * feat: 引入`isObject`函数替代原有的`typeof throttle === 'object'`判断方式 * feat: 优化骨架屏文档结构和示例 * feat: 完善文字描述和修改对应的文件名 * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * feat: Optimize code writing * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md * feat: modify doc * feat: md * feat: 补充 useThrottleRender 钩子的测试用例 * feat: 将 SkeletonThrottle 类型移动到hook中, 重命名为 ThrottleType 以提高通用性 --------- Co-authored-by: btea <2356281422@qq.com>
2024-11-07 21:31:16 +08:00
const initVal = isObject(throttle) && Boolean(throttle.initVal)
const throttled = ref(initVal)
let timeoutHandle: ReturnType<typeof setTimeout> | null = null
2020-12-21 20:07:48 +08:00
feat(components): [skeleton] `throttle` supports values ​​as object (#17041) * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * feat: 重构`useThrottleRender`钩子以提高代码可读性和效率 - 简化了对`throttle`参数的判断逻辑,通过`isNumber`函数判断是否为数字 - 将`leadingDispatch`和`trailingDispatch`函数合并为`dispatcher`函数,根据传入的类型判断执行逻辑 - 优化了`watch`回调函数,使用`dispatcher`函数替代重复的判断逻辑 * feat: 写法优化 * feat: 引入`isObject`函数替代原有的`typeof throttle === 'object'`判断方式 * feat: 优化骨架屏文档结构和示例 * feat: 完善文字描述和修改对应的文件名 * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * feat: Optimize code writing * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md * feat: modify doc * feat: md * feat: 补充 useThrottleRender 钩子的测试用例 * feat: 将 SkeletonThrottle 类型移动到hook中, 重命名为 ThrottleType 以提高通用性 --------- Co-authored-by: btea <2356281422@qq.com>
2024-11-07 21:31:16 +08:00
const dispatchThrottling = (timer: number | undefined) => {
if (isUndefined(timer)) {
throttled.value = loading.value
return
}
2020-12-21 20:07:48 +08:00
if (timeoutHandle) {
clearTimeout(timeoutHandle)
}
timeoutHandle = setTimeout(() => {
2020-12-21 20:07:48 +08:00
throttled.value = loading.value
feat(components): [skeleton] `throttle` supports values ​​as object (#17041) * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * feat: 重构`useThrottleRender`钩子以提高代码可读性和效率 - 简化了对`throttle`参数的判断逻辑,通过`isNumber`函数判断是否为数字 - 将`leadingDispatch`和`trailingDispatch`函数合并为`dispatcher`函数,根据传入的类型判断执行逻辑 - 优化了`watch`回调函数,使用`dispatcher`函数替代重复的判断逻辑 * feat: 写法优化 * feat: 引入`isObject`函数替代原有的`typeof throttle === 'object'`判断方式 * feat: 优化骨架屏文档结构和示例 * feat: 完善文字描述和修改对应的文件名 * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * feat: Optimize code writing * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md * feat: modify doc * feat: md * feat: 补充 useThrottleRender 钩子的测试用例 * feat: 将 SkeletonThrottle 类型移动到hook中, 重命名为 ThrottleType 以提高通用性 --------- Co-authored-by: btea <2356281422@qq.com>
2024-11-07 21:31:16 +08:00
}, timer)
}
const dispatcher = (type: 'leading' | 'trailing') => {
if (type === 'leading') {
if (isNumber(throttle)) {
dispatchThrottling(throttle)
} else {
dispatchThrottling(throttle.leading)
}
} else {
if (isObject(throttle)) {
dispatchThrottling(throttle.trailing)
} else {
throttled.value = false
}
}
2020-12-21 20:07:48 +08:00
}
feat(components): [skeleton] `throttle` supports values ​​as object (#17041) * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * feat: 重构`useThrottleRender`钩子以提高代码可读性和效率 - 简化了对`throttle`参数的判断逻辑,通过`isNumber`函数判断是否为数字 - 将`leadingDispatch`和`trailingDispatch`函数合并为`dispatcher`函数,根据传入的类型判断执行逻辑 - 优化了`watch`回调函数,使用`dispatcher`函数替代重复的判断逻辑 * feat: 写法优化 * feat: 引入`isObject`函数替代原有的`typeof throttle === 'object'`判断方式 * feat: 优化骨架屏文档结构和示例 * feat: 完善文字描述和修改对应的文件名 * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * feat: Optimize code writing * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md * feat: modify doc * feat: md * feat: 补充 useThrottleRender 钩子的测试用例 * feat: 将 SkeletonThrottle 类型移动到hook中, 重命名为 ThrottleType 以提高通用性 --------- Co-authored-by: btea <2356281422@qq.com>
2024-11-07 21:31:16 +08:00
onMounted(() => dispatcher('leading'))
2020-12-21 20:07:48 +08:00
watch(
() => loading.value,
(val) => {
feat(components): [skeleton] `throttle` supports values ​​as object (#17041) * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * feat: 重构`useThrottleRender`钩子以提高代码可读性和效率 - 简化了对`throttle`参数的判断逻辑,通过`isNumber`函数判断是否为数字 - 将`leadingDispatch`和`trailingDispatch`函数合并为`dispatcher`函数,根据传入的类型判断执行逻辑 - 优化了`watch`回调函数,使用`dispatcher`函数替代重复的判断逻辑 * feat: 写法优化 * feat: 引入`isObject`函数替代原有的`typeof throttle === 'object'`判断方式 * feat: 优化骨架屏文档结构和示例 * feat: 完善文字描述和修改对应的文件名 * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * feat: Optimize code writing * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md * feat: modify doc * feat: md * feat: 补充 useThrottleRender 钩子的测试用例 * feat: 将 SkeletonThrottle 类型移动到hook中, 重命名为 ThrottleType 以提高通用性 --------- Co-authored-by: btea <2356281422@qq.com>
2024-11-07 21:31:16 +08:00
dispatcher(val ? 'leading' : 'trailing')
2020-12-21 20:07:48 +08:00
}
)
feat(components): [skeleton] `throttle` supports values ​​as object (#17041) * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * fix(components): [skeleton] `throttle` property not working * fix: lint fix * feat: add func & doc & test * feat: remove test modify * feat: increase document examples, improve document descriptions * feat: 重构`useThrottleRender`钩子以提高代码可读性和效率 - 简化了对`throttle`参数的判断逻辑,通过`isNumber`函数判断是否为数字 - 将`leadingDispatch`和`trailingDispatch`函数合并为`dispatcher`函数,根据传入的类型判断执行逻辑 - 优化了`watch`回调函数,使用`dispatcher`函数替代重复的判断逻辑 * feat: 写法优化 * feat: 引入`isObject`函数替代原有的`typeof throttle === 'object'`判断方式 * feat: 优化骨架屏文档结构和示例 * feat: 完善文字描述和修改对应的文件名 * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * feat: Optimize code writing * Update docs/en-US/component/skeleton.md Co-authored-by: btea <2356281422@qq.com> * Update docs/en-US/component/skeleton.md * feat: modify doc * feat: md * feat: 补充 useThrottleRender 钩子的测试用例 * feat: 将 SkeletonThrottle 类型移动到hook中, 重命名为 ThrottleType 以提高通用性 --------- Co-authored-by: btea <2356281422@qq.com>
2024-11-07 21:31:16 +08:00
2020-12-21 20:07:48 +08:00
return throttled
}