import {computed, onBeforeUnmount, reactive} from "@peryl/react-compose"; import {createCounter} from '@peryl/utils/createCounter'; import {delay} from "@peryl/utils/delay"; export const AutoLoadingObserver = (() => { const globalState = reactive({ /** * 当前激活的observer,比如点击按钮的话,emit click事件之前激活这个按钮的observer,emit click之后清除这个observer * @author 韦胜健 * @date 2024/3/20 16:11 */ currentObserver: null as null | { observerId: string }, /** * 正在loading的observerId,值是一个数字,是subjectId;激活observer的时候可能会一次性并发执行多个异步任务,所以这里值是一个数字,当值清空的时候意味着关闭observer的加载状态 * @author 韦胜健 * @date 2024/3/20 16:11 */ loadingMap: {} as Record }); const utils = { /*创建一个observerId*/ nextObserverId: createCounter('auto_loading_observer'), /*创建一个subjectId*/ nextSubjectId: createCounter('auto_loading_subject'), }; /** * 创建一个observer,要记得组件销毁的时候执行dispose * @author 韦胜健 * @date 2024/3/20 16:13 */ const useObserver = () => { const observerId = utils.nextObserverId(); const tapStart = () => {globalState.currentObserver = { observerId };}; const tapEnd = () => {globalState.currentObserver = null;}; const isLoading = computed(() => !!globalState.loadingMap[observerId]?.length); onBeforeUnmount(() => isLoading.effect.stop()); return reactive({ tapStart, tapEnd, isLoading }); }; /** * 一个异步任务执行时,调用这个subject * @author 韦胜健 * @date 2024/3/20 16:14 */ const subject = (promise: Promise) => { /*没有激活的observer,证明当前异步任务不需要开启loading*/ if (!globalState.currentObserver) {return;} /*为本次执行异步任务创建一个subjectId*/ const subjectId = utils.nextSubjectId(); /*当前激活的observerId*/ const { observerId } = globalState.currentObserver; /*关联subjectId与observerId,开启observer的加载状态*/ if (!globalState.loadingMap[observerId]) { globalState.loadingMap = { ...globalState.loadingMap, [observerId]: [subjectId] }; } else { globalState.loadingMap[observerId]!.push(subjectId); } const startDatetime = Date.now(); // 异步任务执行开始时间 const minDatetime = 1000; // loading最小时间 /*不论成功或者失败,都解除关联subjectId与observerId*/ promise.finally(async () => { /*异步任务执行结束之后,立即将observe放回去,因为可能是串行执行的异步任务,下一个异步任务也要延续这个observe的loading状态*/ globalState.currentObserver = { observerId }; delay().then(() => {globalState.currentObserver = null;}); /*如果耗费时间小于最小时间,则等待剩余的时间再关闭loading*/ const waitDatetime = startDatetime + minDatetime - Date.now(); if (waitDatetime > 0) {await delay(waitDatetime);} const index = globalState.loadingMap[observerId]?.indexOf(subjectId); if (index != null && index > -1) { const arr = [...globalState.loadingMap[observerId]!]; arr.splice(index, 1); if (arr.length) { globalState.loadingMap[observerId] = arr; } else { const map = { ...globalState.loadingMap }; delete map[observerId]; globalState.loadingMap = map; } } }); }; const request = (id: string, duraiton?: number) => { const duration = duraiton || (Math.random() * 2000 + 1000); const promise = delay(duration).then(() => { console.log('request:', id); return { id, duration }; }); AutoLoadingObserver.subject(promise); return promise; }; return { useObserver, subject, globalState, request, }; })(); /*export const NextTick = (() => { const generateNextTickId = createCounter('next_tick'); const nextTickMap = {} as Record; function clearNextTick(id: string) { delete nextTickMap[id]; } function setNextTick(fn: SimpleFunction) { const id = generateNextTickId(); nextTickMap[id] = true; console.log('nextTickMap', nextTickMap); delay().then(() => { if (id in nextTickMap) { fn(); delete nextTickMap[id]; } }); return id; } return { clearNextTick, setNextTick, }; })(); export const NextTick_TEST = (() => { const generateNextTickId = createCounter('next_tick'); const nextTickMap = {} as Record; function clearNextTick(id: string) { delete nextTickMap[id]; } function setNextTick(fn: SimpleFunction) { const id = generateNextTickId(); nextTickMap[id] = true; console.log('nextTickMap', nextTickMap); delay(1000).then(() => { if (id in nextTickMap) { fn(); delete nextTickMap[id]; } }); return id; } return { clearNextTick, setNextTick, }; })();*/