Skip to content

Commit 6f09e06

Browse files
fix(mobile): thread lists wake for every snooze expiring in the same minute
The next snooze wake was searched after the floored minute, so after one snooze woke the list, a second one later in the same minute was never armed. Search after the latest time the hook returned instead. Mobile's useNowAfter also froze after its first adjustment under React Compiler, which caches a bare Date.now() in render once per instance. Read the clock inside the state updater, as web's useNowAfter already does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8b3421d commit 6f09e06

4 files changed

Lines changed: 49 additions & 9 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { act, useLayoutEffect } from "react";
2+
import { createRoot } from "react-dom/client";
3+
import { expect, it, vi } from "vite-plus/test";
4+
5+
import { useThreadListNow } from "./use-thread-list-now";
6+
7+
const MINUTE_MS = Date.UTC(2026, 9, 27, 12, 0);
8+
const iso = (secondsIntoMinute: number) =>
9+
new Date(MINUTE_MS + secondsIntoMinute * 1000).toISOString();
10+
11+
it("wakes for each snooze that expires within the same minute", async () => {
12+
using _timers = { [Symbol.dispose]: () => vi.useRealTimers() };
13+
vi.useFakeTimers({ now: MINUTE_MS + 10_000 });
14+
const threads = [{ snoozedUntil: iso(30) }, { snoozedUntil: iso(40) }];
15+
const seen: string[] = [];
16+
function Probe() {
17+
const now = useThreadListNow(threads);
18+
useLayoutEffect(() => {
19+
seen.push(now);
20+
});
21+
return null;
22+
}
23+
24+
const root = createRoot(document.createElement("div"));
25+
await act(async () => root.render(<Probe />));
26+
await act(async () => vi.advanceTimersByTime(20_000));
27+
expect(seen.at(-1)).toBe(iso(30));
28+
29+
await act(async () => vi.advanceTimersByTime(10_000));
30+
expect(seen.at(-1)).toBe(iso(40));
31+
act(() => root.unmount());
32+
});

‎apps/mobile/src/features/threads/use-thread-list-now.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useState } from "react";
2+
13
import { useNowAfter, useNowMinuteMs } from "../../lib/clock";
24
import { nextSnoozeWakeAfter } from "./threadListV2";
35

@@ -11,6 +13,12 @@ export function useThreadListNow(
1113
threads: ReadonlyArray<{ readonly snoozedUntil?: string | null }>,
1214
): string {
1315
const minuteMs = useNowMinuteMs();
14-
const afterWakeMs = useNowAfter(nextSnoozeWakeAfter(threads, minuteMs));
15-
return new Date(Math.max(minuteMs, afterWakeMs)).toISOString();
16+
// The latest time returned. The next wake is searched after it, not after
17+
// the floored minute, so a second snooze expiring later in the same minute
18+
// arms its own wake instead of waiting for the next minute tick.
19+
const [latestMs, setLatestMs] = useState(minuteMs);
20+
const afterWakeMs = useNowAfter(nextSnoozeWakeAfter(threads, Math.max(minuteMs, latestMs)));
21+
const nowMs = Math.max(minuteMs, afterWakeMs, latestMs);
22+
if (nowMs !== latestMs) setLatestMs(nowMs);
23+
return new Date(nowMs).toISOString();
1624
}

‎apps/mobile/src/lib/clock.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ export function useDeadlinePassed(deadlineMs: number | null): boolean {
1313
*/
1414
export function useNowAfter(deadlineMs: number | null): number {
1515
const passed = useDeadlinePassed(deadlineMs);
16-
const [snapshot, setSnapshot] = useState(() => ({ deadlineMs, passed, nowMs: Date.now() }));
17-
if (snapshot.deadlineMs !== deadlineMs || snapshot.passed !== passed) {
18-
// oxlint-disable-next-line react/purity -- Re-reads the clock only in the render-phase adjustment that runs when the deadline changes or passes.
19-
const next = { deadlineMs, passed, nowMs: Date.now() };
20-
setSnapshot(next);
21-
return next.nowMs;
22-
}
16+
const readNow = () => ({ deadlineMs, passed, nowMs: Date.now() });
17+
const [snapshot, setSnapshot] = useState(readNow);
18+
// The clock is read inside the updater: the compiler caches a bare
19+
// `Date.now()` in render once per instance, so a later adjustment would reuse
20+
// the first reading. React discards this render and re-runs with the update.
21+
if (snapshot.deadlineMs !== deadlineMs || snapshot.passed !== passed) setSnapshot(readNow);
2322
return snapshot.nowMs;
2423
}
2524

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const reactTests = [
2525
"src/state/queries.debounce.test.tsx",
2626
"src/state/use-selected-thread-git-actions.test.tsx",
2727
"src/features/terminal/useTerminalExitNavigation.test.tsx",
28+
"src/features/threads/use-thread-list-now.test.tsx",
2829
];
2930

3031
const sourceRoot = `${normalizePath(NodeURL.fileURLToPath(new URL("./src", import.meta.url)))}/`;

0 commit comments

Comments
 (0)