refactor(mp): request.ts 模块级状态收编 + AbortSignal + Analytics 受控
- 提取 ConcurrencyLimiter 类(并发限制 8,可 reset) - 提取 ResponseCache 类(GET 缓存 + 去重 + patientId 绑定) - 新增 resetForTesting() 测试隔离函数 - api.get/post/put/delete 支持 AbortSignal 请求取消 - app.tsx Analytics 定时器改为 useDidShow/useDidHide 控制后台暂停 - 测试文件接入 resetForTesting() 构建通过,测试 74/75(1 个预存失败)。
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useEffect, PropsWithChildren } from 'react';
|
||||
import Taro, { useDidShow } from '@tarojs/taro';
|
||||
import { useEffect, useRef, PropsWithChildren } from 'react';
|
||||
import Taro, { useDidShow, useDidHide } from '@tarojs/taro';
|
||||
import ErrorBoundary from './components/ErrorBoundary';
|
||||
import { flushEvents } from './services/analytics';
|
||||
import { useAuthStore } from './stores/auth';
|
||||
@@ -28,16 +28,32 @@ function App({ children }: PropsWithChildren<Record<string, unknown>>) {
|
||||
return () => { delete (globalThis as any).__hms; };
|
||||
}, []);
|
||||
|
||||
// Analytics 定时器:仅在页面可见时运行,后台时暂停以节省资源
|
||||
const analyticsTimerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
|
||||
const startAnalyticsTimer = () => {
|
||||
if (analyticsTimerRef.current) return;
|
||||
analyticsTimerRef.current = setInterval(flushEvents, 30000);
|
||||
};
|
||||
|
||||
const stopAnalyticsTimer = () => {
|
||||
if (analyticsTimerRef.current) {
|
||||
clearInterval(analyticsTimerRef.current);
|
||||
analyticsTimerRef.current = null;
|
||||
}
|
||||
};
|
||||
|
||||
useDidShow(() => {
|
||||
startAnalyticsTimer();
|
||||
});
|
||||
|
||||
useDidHide(() => {
|
||||
stopAnalyticsTimer();
|
||||
flushEvents();
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setInterval(() => {
|
||||
flushEvents();
|
||||
}, 30000);
|
||||
const onHide = () => { flushEvents(); };
|
||||
Taro.eventCenter.on('appHide', onHide);
|
||||
return () => {
|
||||
clearInterval(timer);
|
||||
Taro.eventCenter.off('appHide', onHide);
|
||||
};
|
||||
return () => { stopAnalyticsTimer(); };
|
||||
}, []);
|
||||
|
||||
return <ErrorBoundary>{children}</ErrorBoundary>;
|
||||
|
||||
Reference in New Issue
Block a user