From 92b42d559df1e9a35740d2e963cce2a754b7649a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E4=B8=AB=E8=AE=B2=E6=A2=B5?= Date: Sat, 11 Jul 2026 20:53:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(redis):=20=E4=BC=98=E5=8C=96=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=B7=E6=96=B0=E9=80=BB=E8=BE=91=E5=92=8C=E6=98=BE?= =?UTF-8?q?=E7=A4=BATTL=E7=9A=84=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改computeAutoRefreshTick函数,简化逻辑判断 - 更新computeDisplayTtl函数,确保倒计时为零时不显示过期的服务器TTL --- .../__tests__/redis/redisAutoRefresh.spec.ts | 20 +++++++++---------- .../desktop/src/lib/redis/redisAutoRefresh.ts | 14 +++++-------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/apps/desktop/src/lib/__tests__/redis/redisAutoRefresh.spec.ts b/apps/desktop/src/lib/__tests__/redis/redisAutoRefresh.spec.ts index 628dce439c..c787da6649 100644 --- a/apps/desktop/src/lib/__tests__/redis/redisAutoRefresh.spec.ts +++ b/apps/desktop/src/lib/__tests__/redis/redisAutoRefresh.spec.ts @@ -10,19 +10,20 @@ describe("computeAutoRefreshTick", () => { expect(computeAutoRefreshTick(false, 5, true)).toEqual({ type: "idle" }); }); - it("returns decrement when countdown is above zero", () => { + it("returns decrement while more than one second remains", () => { expect(computeAutoRefreshTick(true, 10, false)).toEqual({ type: "decrement" }); - expect(computeAutoRefreshTick(true, 1, false)).toEqual({ type: "decrement" }); // decrement even when loading — countdown should keep ticking expect(computeAutoRefreshTick(true, 3, true)).toEqual({ type: "decrement" }); }); - it("returns refresh when countdown reaches zero and not loading", () => { + it("refreshes on the expiry tick instead of exposing a zero countdown", () => { + expect(computeAutoRefreshTick(true, 1, false)).toEqual({ type: "refresh" }); expect(computeAutoRefreshTick(true, 0, false)).toEqual({ type: "refresh" }); expect(computeAutoRefreshTick(true, -1, false)).toEqual({ type: "refresh" }); }); - it("returns idle when countdown is zero but a load is already in flight", () => { + it("does not start another refresh when a load is already in flight", () => { + expect(computeAutoRefreshTick(true, 1, true)).toEqual({ type: "decrement" }); // This prevents concurrent load() calls expect(computeAutoRefreshTick(true, 0, true)).toEqual({ type: "idle" }); expect(computeAutoRefreshTick(true, -1, true)).toEqual({ type: "idle" }); @@ -50,9 +51,8 @@ describe("computeDisplayTtl", () => { expect(computeDisplayTtl(false, 3, 10)).toBe(10); }); - it("returns server TTL when countdown has not started (zero)", () => { - // countdownTtl starts at 0 before first tick; show server TTL - expect(computeDisplayTtl(true, 0, 10)).toBe(10); + it("does not flash back to the stale server TTL at zero", () => { + expect(computeDisplayTtl(true, 0, 5)).toBe(0); }); it("returns live countdown when auto-refresh is active and counting", () => { @@ -60,9 +60,7 @@ describe("computeDisplayTtl", () => { expect(computeDisplayTtl(true, 1, 10)).toBe(1); }); - it("returns server TTL when countdown would go below zero", () => { - // Edge case: countdownTtl shouldn't be negative in practice, - // but if it is, fall back to server TTL - expect(computeDisplayTtl(true, -1, 10)).toBe(10); + it("clamps an active countdown below zero instead of showing stale data", () => { + expect(computeDisplayTtl(true, -1, 10)).toBe(0); }); }); diff --git a/apps/desktop/src/lib/redis/redisAutoRefresh.ts b/apps/desktop/src/lib/redis/redisAutoRefresh.ts index 683a19f201..ae8eff91fa 100644 --- a/apps/desktop/src/lib/redis/redisAutoRefresh.ts +++ b/apps/desktop/src/lib/redis/redisAutoRefresh.ts @@ -18,9 +18,8 @@ export type AutoRefreshTickAction = { type: "idle" } | { type: "decrement" } | { */ export function computeAutoRefreshTick(enabled: boolean, countdownTtl: number, isLoading: boolean): AutoRefreshTickAction { if (!enabled) return { type: "idle" }; - if (countdownTtl > 0) return { type: "decrement" }; - if (countdownTtl <= 0 && !isLoading) return { type: "refresh" }; - return { type: "idle" }; + if (isLoading) return countdownTtl > 0 ? { type: "decrement" } : { type: "idle" }; + return countdownTtl <= 1 ? { type: "refresh" } : { type: "decrement" }; } /** @@ -36,12 +35,9 @@ export function shouldStopAutoRefresh(ttl: number): boolean { /** * Compute the TTL value that should be displayed in the badge. * - * When auto-refresh is active and countdown is running, the live - * countdown value is shown. Otherwise the last known server TTL is used. + * When auto-refresh is active, the live countdown value is shown. The + * last known server TTL must not reappear after the countdown reaches zero. */ export function computeDisplayTtl(autoRefreshEnabled: boolean, countdownTtl: number, serverTtl: number): number { - if (autoRefreshEnabled && countdownTtl > 0) { - return countdownTtl; - } - return serverTtl; + return autoRefreshEnabled ? Math.max(countdownTtl, 0) : serverTtl; }