Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 阻断屏加定时兜底核对,不再只依赖 AppState 跳变

guyong79808b2bb23c
diff --git a/apps/mobile/src/__tests__/forcedUpdateGate.test.ts b/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
index 47cb213488..ed02f6b920 100644
--- a/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
+++ b/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
@@ -75,6 +75,10 @@ describe('强更阻断闸门', () => {
const fetchLatest = readCode('src/update/fetchLatestRelease.ts');
expect(fetchLatest).toContain("'cache-control': 'no-cache'");
expect(fetchLatest).toContain('&t=${Date.now()}');
+ // 光靠 AppState 跳变不够:用户停在阻断屏上不动时没有任何跳变,必须有定时兜底。
+ const recheckHook = readCode('src/update/useForcedUpdateRecheck.ts');
+ expect(recheckHook).toContain('rechecker.handleTick()');
+ expect(recheckHook).toContain('clearInterval(timer)');
// 在途期间若有更新的观察写入 store,本次旧结论必须作废(compare-and-set)。
expect(recheck).toContain('const startRevision = deps.getRevision?.();');
expect(recheck).toContain('deps.onCleared(startRevision)');
diff --git a/apps/mobile/src/update/forcedUpdateRecheck.test.ts b/apps/mobile/src/update/forcedUpdateRecheck.test.ts
index c8c1b20a8b..9f992858fe 100644
--- a/apps/mobile/src/update/forcedUpdateRecheck.test.ts
+++ b/apps/mobile/src/update/forcedUpdateRecheck.test.ts
@@ -101,6 +101,43 @@ describe('createForcedUpdateRechecker 触发条件', () => {
});
});
+describe('createForcedUpdateRechecker 定时兜底', () => {
+ it('挂载后立刻 tick:间隔不足 → 不检查', () => {
+ const deps = makeDeps();
+ const rechecker = createForcedUpdateRechecker(deps);
+ expect(rechecker.handleTick()).toBeNull();
+ expect(deps.fetchLatest).not.toHaveBeenCalled();
+ });
+
+ it('用户停在阻断屏不动(无任何 AppState 跳变)→ 间隔满足后 tick 会核对', async () => {
+ const deps = makeDeps();
+ const rechecker = createForcedUpdateRechecker(deps, { minIntervalMs: 1000 });
+ deps.advance(1001);
+ const result = rechecker.handleTick();
+ expect(result).not.toBeNull();
+ await result;
+ expect(deps.fetchLatest).toHaveBeenCalledOnce();
+ });
+
+ it('在后台被置位、用户在节流窗口内回来 → 那次 active 被丢掉,但 tick 随后补上', async () => {
+ const deps = makeDeps({ getAppState: () => 'background' });
+ const rechecker = createForcedUpdateRechecker(deps, { minIntervalMs: 1000 });
+ deps.advance(500); // 窗口内回前台
+ expect(rechecker.handleAppStateChange('active')).toBeNull();
+ deps.advance(600); // 窗口过去
+ expect(rechecker.handleTick()).not.toBeNull();
+ });
+
+ it('tick 与 AppState 共用节流:刚查过就 tick → 不重复发起', async () => {
+ const deps = makeDeps();
+ const rechecker = createForcedUpdateRechecker(deps, { minIntervalMs: 1000 });
+ deps.advance(1001);
+ await resume(rechecker);
+ expect(rechecker.handleTick()).toBeNull();
+ expect(deps.fetchLatest).toHaveBeenCalledOnce();
+ });
+});
+
describe('createForcedUpdateRechecker 解除判定', () => {
const runOnce = (deps: ReturnType<typeof makeDeps>) => {
const rechecker = createForcedUpdateRechecker(deps, { minIntervalMs: 0 });
diff --git a/apps/mobile/src/update/forcedUpdateRecheck.ts b/apps/mobile/src/update/forcedUpdateRecheck.ts
index 60e2cda652..101df5624f 100644
--- a/apps/mobile/src/update/forcedUpdateRecheck.ts
+++ b/apps/mobile/src/update/forcedUpdateRecheck.ts
@@ -86,6 +86,15 @@ export interface ForcedUpdateRechecker {
* 未触发时返回 null(便于测试断言),触发时返回本次核对的 Promise(永不 reject)。
*/
handleAppStateChange: (next: string) => Promise<ForcedUpdateRecheckOutcome> | null;
+ /**
+ * 定时兜底入口(阻断屏挂载期间周期性调用)。只受节流与在途约束,不看 AppState。
+ *
+ * 为什么必须有:光靠 AppState 跳变不够 —— (1) 用户就坐在阻断屏上不动时没有任何跳变;
+ * (2) 阻断态在后台被置位、用户在节流窗口内就回来时,那次 'active' 会被节流丢掉,
+ * 而后再没有事件来重试。两种情况下运维撤回门槛 / 修正安装地址都不会被观察到,
+ * 阻断屏会一直停在旧状态。
+ */
+ handleTick: () => Promise<ForcedUpdateRecheckOutcome> | null;
}
/** 创建阻断屏核对器(持有节流/在途状态;阻断屏挂载期间一个实例)。 */
@@ -160,6 +169,13 @@ export function createForcedUpdateRechecker(
}
}
+ /** 节流 + 在途门:两个入口共用,通过则占用本轮并发起。 */
+ function tryRun(): Promise<ForcedUpdateRecheckOutcome> | null {
+ if (inFlight || deps.now() - lastRunAt < minIntervalMs) return null;
+ lastRunAt = deps.now();
+ return run();
+ }
+
return {
handleAppStateChange(next: string): Promise<ForcedUpdateRecheckOutcome> | null {
if (next === 'background') {
@@ -168,9 +184,10 @@ export function createForcedUpdateRechecker(
}
if (next !== 'active' || !wasBackground) return null;
wasBackground = false;
- if (inFlight || deps.now() - lastRunAt < minIntervalMs) return null;
- lastRunAt = deps.now();
- return run();
+ return tryRun();
+ },
+ handleTick(): Promise<ForcedUpdateRecheckOutcome> | null {
+ return tryRun();
},
};
}
diff --git a/apps/mobile/src/update/useForcedUpdateRecheck.ts b/apps/mobile/src/update/useForcedUpdateRecheck.ts
index 183ffc87f3..43e98df83d 100644
--- a/apps/mobile/src/update/useForcedUpdateRecheck.ts
+++ b/apps/mobile/src/update/useForcedUpdateRecheck.ts
@@ -18,6 +18,9 @@ import {
} from './forcedUpdateStore';
import { isCanaryChannel } from './canaryChannelStore';
+/** 定时敲门间隔;真实请求频率仍由核对器内部节流决定(默认 30s)。 */
+const RECHECK_TICK_MS = 30_000;
+
export function useForcedUpdateRecheck(isCanary = isCanaryChannel()): void {
useEffect(() => {
let current = true;
@@ -47,9 +50,16 @@ export function useForcedUpdateRecheck(isCanary = isCanaryChannel()): void {
const subscription = AppState.addEventListener('change', (next) => {
void rechecker.handleAppStateChange(next);
});
+ // 定时兜底:光靠 AppState 跳变不够 —— 用户就停在阻断屏上不动时没有任何跳变,
+ // 而在后台被置位又在节流窗口内回前台的那次 'active' 也会被节流丢掉。
+ // 实际请求频率由核对器内部节流(默认 30s)决定,这里只负责按时敲门。
+ const timer = setInterval(() => {
+ void rechecker.handleTick();
+ }, RECHECK_TICK_MS);
return () => {
current = false;
subscription.remove();
+ clearInterval(timer);
};
}, [isCanary]);
}