feat: 使用 radash 的 throttle 包装 load 函数,优化加载频率并重构 refresh 方法
Some checks failed
/ build-and-deploy-to-vercel (push) Failing after 40s
/ playwright (push) Successful in 2m10s
/ depcheck (push) Successful in 1m39s

This commit is contained in:
严浩
2024-12-11 15:42:49 +08:00
parent 3a9b19d64f
commit 40bf214da7

View File

@ -1,4 +1,6 @@
<script lang="ts">
import { throttle } from 'radash';
function checkIsVisible(el: Element, root: Element | null = null) {
if (!el) return false;
@ -43,19 +45,12 @@ defineSlots<{
// 加载失败
error(props: { retry: () => void }): unknown;
}>();
defineExpose({
refresh: () => {
currentPage = 0;
state.value = '';
return load('refresh');
},
});
const target = ref(null);
let currentPage = 0;
const state = ref<'' | 'loading' | 'loaded' | 'complete' | 'error'>('');
const load = async (why?: string) => {
const loadImpl = async (why?: string) => {
console.group('load');
console.debug(`why :>> `, why);
console.groupEnd();
@ -81,6 +76,19 @@ const load = async (why?: string) => {
}
};
// 使用radash的throttle包装load函数,1秒内最多执行一次
const load = throttle({ interval: 1000 }, loadImpl);
const refresh = () => {
currentPage = 0;
state.value = '';
return loadImpl('refresh'); // refresh 不需要节流
};
defineExpose({
refresh,
});
const { pause, resume /* ,isSupported, isActive */ } = useIntersectionObserver(
target,
([entry]) => {