Skip to content

Commit e1faa42

Browse files
fix(web): snoozed thread status wakes at its wake time
useNowAfter read Date.now() in a render-phase adjustment, and the React Compiler cached that read once per instance. Every later adjustment reused the first timestamp, so the status line kept saying "Snoozed" after the wake time while the sidebar already showed "Woke". The clock is now read inside the state updater, which the compiler cannot memoize. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6801e62 commit e1faa42

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
3+
import { act, fakeTimers, render } from "../../test/render";
4+
import { useNowAfter } from "./useDeadline";
5+
6+
function Probe(props: { deadlineMs: number }) {
7+
const nowMs = useNowAfter(props.deadlineMs);
8+
return <output>{nowMs >= props.deadlineMs ? "passed" : "pending"}</output>;
9+
}
10+
11+
describe("useNowAfter", () => {
12+
it("reads a fresh clock when a later deadline passes", async () => {
13+
// The first render reads the clock.
14+
using _timers = fakeTimers({ now: 0 });
15+
await using screen = await render(<Probe deadlineMs={1_000} />, {
16+
// A bare hook probe; nothing here navigates.
17+
router: false,
18+
});
19+
await screen.rerender(<Probe deadlineMs={2_000} />);
20+
await expect.element(screen.getByText("pending")).toBeInTheDocument();
21+
22+
await act(() => vi.advanceTimersByTime(2_000));
23+
await expect.element(screen.getByText("passed")).toBeInTheDocument();
24+
});
25+
});

‎apps/web/src/hooks/useDeadline.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ 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
}

0 commit comments

Comments
 (0)