feat: 使用 radash 的 throttle 包装 load 函数,优化加载频率并重构 refresh 方法
This commit is contained in:
@ -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]) => {
|
||||
|
Reference in New Issue
Block a user