fix(miniprogram): 首页体征数据加载时序 + 并发控制 + 权限修复
- ConcurrencyLimiter 12→8 预留长轮询通道,避免超微信 10 并发限制 - usePageData 添加 AbortController,页面隐藏/卸载自动取消请求 - useHomeData 添加 useEffect 监听 currentPatient 变化自动触发数据加载 - 医护人员首页跳转前不渲染 HomeDashboard,避免触发无用 API 请求 - auth.ts getPatients 正确提取分页响应 .data 数组 - health.ts getTodaySummary 从 Storage 回退读取 patient_id - health store refreshToday 从 auth store 回退获取 currentPatient.id - auth store restore() 状态变化时清理请求缓存,避免返回过期数据
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useRef, useState, useCallback } from 'react';
|
||||
import Taro, { useDidShow, usePullDownRefresh } from '@tarojs/taro';
|
||||
import { useRef, useState, useCallback, useEffect } from 'react';
|
||||
import Taro, { useDidShow, useDidHide, usePullDownRefresh } from '@tarojs/taro';
|
||||
|
||||
interface UsePageDataOptions {
|
||||
throttleMs?: number;
|
||||
@@ -14,7 +14,7 @@ interface UsePageDataResult {
|
||||
}
|
||||
|
||||
export function usePageData(
|
||||
fetcher: () => Promise<void>,
|
||||
fetcher: (signal?: AbortSignal) => Promise<void>,
|
||||
options?: UsePageDataOptions,
|
||||
): UsePageDataResult {
|
||||
const throttleMs = options?.throttleMs ?? 5000;
|
||||
@@ -26,6 +26,14 @@ export function usePageData(
|
||||
const lastRunRef = useRef(0);
|
||||
const fetcherRef = useRef(fetcher);
|
||||
fetcherRef.current = fetcher;
|
||||
const abortRef = useRef<AbortController | null>(null);
|
||||
|
||||
const abort = useCallback(() => {
|
||||
if (abortRef.current) {
|
||||
abortRef.current.abort();
|
||||
abortRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const run = useCallback(async (force = false) => {
|
||||
if (!enabled || loadingRef.current) return;
|
||||
@@ -33,11 +41,15 @@ export function usePageData(
|
||||
loadingRef.current = true;
|
||||
setLoading(true);
|
||||
lastRunRef.current = Date.now();
|
||||
abort();
|
||||
const ac = new AbortController();
|
||||
abortRef.current = ac;
|
||||
try {
|
||||
await fetcherRef.current();
|
||||
await fetcherRef.current(ac.signal);
|
||||
} finally {
|
||||
loadingRef.current = false;
|
||||
setLoading(false);
|
||||
if (abortRef.current === ac) abortRef.current = null;
|
||||
}
|
||||
}, [enabled, throttleMs]);
|
||||
|
||||
@@ -45,6 +57,16 @@ export function usePageData(
|
||||
run();
|
||||
});
|
||||
|
||||
useDidHide(() => {
|
||||
abort();
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
abort();
|
||||
};
|
||||
}, []);
|
||||
|
||||
const trigger = useCallback(() => {
|
||||
run(true);
|
||||
}, [run]);
|
||||
@@ -54,11 +76,15 @@ export function usePageData(
|
||||
loadingRef.current = true;
|
||||
setLoading(true);
|
||||
lastRunRef.current = Date.now();
|
||||
abort();
|
||||
const ac = new AbortController();
|
||||
abortRef.current = ac;
|
||||
try {
|
||||
await fetcherRef.current();
|
||||
await fetcherRef.current(ac.signal);
|
||||
} finally {
|
||||
loadingRef.current = false;
|
||||
setLoading(false);
|
||||
if (abortRef.current === ac) abortRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user