From 59fe98d2bafb0860b5ed5b819d0a32a4b6ce9d43 Mon Sep 17 00:00:00 2001 From: Julien BIANCHI Date: Sun, 18 Oct 2026 23:22:55 +0200 Subject: [PATCH] fix: refresh the shared clock when the first component subscribes again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `useNow` store keeps a module-level `now` and runs its 30 s interval only while at least one component is subscribed. When the last subscriber unmounts, the interval stops, and the next subscription restarted it without reading the clock: `now` kept the value of the moment the interval stopped until the first tick. Inertia keeps module state across navigations, so after a few minutes on a page without timestamps (Appearance, Terminal, Safety, About, Terms, Privacy) and back to the log, every relative age was behind by the time spent away for up to 30 s: commits posted meanwhile showed "now", older ones were rejuvenated, poll countdowns were stretched, and then everything jumped at once on the first tick. Read the clock again when the interval is restarted, so the first render after coming back is right away. The test drives the hook through a mocked `useSyncExternalStore` with fake timers: subscribe, unsubscribe, move the system clock ten minutes, subscribe again and expect the new time, then tick once to cover the interval. 🤖 Generated with Claude Code --- resources/js/hooks/use-now.ts | 1 + tests/Unit/UseNow.test.tsx | 42 +++++++++++++++++++++++++++++++++++ tests/draft-images.config.ts | 1 + tests/tsconfig.json | 1 + 4 files changed, 45 insertions(+) create mode 100644 tests/Unit/UseNow.test.tsx diff --git a/resources/js/hooks/use-now.ts b/resources/js/hooks/use-now.ts index 8db9e7cca5..e0aa337aae 100644 --- a/resources/js/hooks/use-now.ts +++ b/resources/js/hooks/use-now.ts @@ -8,6 +8,7 @@ const subscribe = (listener: () => void) => { listeners.add(listener); if (interval === undefined) { + now = Date.now(); interval = setInterval(() => { now = Date.now(); listeners.forEach((notify) => notify()); diff --git a/tests/Unit/UseNow.test.tsx b/tests/Unit/UseNow.test.tsx new file mode 100644 index 0000000000..b1ea13fe09 --- /dev/null +++ b/tests/Unit/UseNow.test.tsx @@ -0,0 +1,42 @@ +/// +import assert from 'node:assert/strict'; +import * as React from 'react'; +import { afterAll, test, vi } from 'vite-plus/test'; + +type Subscribe = (listener: () => void) => () => void; + +const unsubscribes: (() => void)[] = []; +const listener = vi.fn(); + +vi.doMock('react', () => ({ + ...React, + useSyncExternalStore: (subscribe: Subscribe, getSnapshot: () => number) => { + unsubscribes.push(subscribe(listener)); + + return getSnapshot(); + }, +})); + +afterAll(() => { + vi.useRealTimers(); + vi.doUnmock('react'); +}); + +test('reads the clock again when a component subscribes after a pause', async () => { + vi.useFakeTimers(); + const start = new Date('2026-10-18T12:00:00Z').getTime(); + vi.setSystemTime(start); + const { useNow } = await import('../../resources/js/hooks/use-now'); + + assert.equal(useNow().getTime(), start); + + unsubscribes.shift()?.(); + vi.setSystemTime(start + 10 * 60_000); + + assert.equal(useNow().getTime(), start + 10 * 60_000); + + vi.advanceTimersByTime(30_000); + + assert.equal(listener.mock.calls.length, 1); + assert.equal(useNow().getTime(), start + 10 * 60_000 + 30_000); +}); diff --git a/tests/draft-images.config.ts b/tests/draft-images.config.ts index 69c28b9577..7bf447fa47 100644 --- a/tests/draft-images.config.ts +++ b/tests/draft-images.config.ts @@ -31,6 +31,7 @@ export default defineConfig({ 'tests/Unit/LinkifiedText.test.tsx', 'tests/Unit/StagedRail.test.tsx', 'tests/Unit/ComposerPreview.test.tsx', + 'tests/Unit/UseNow.test.tsx', ], }, }); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 5fa6cf8640..aecdaaa645 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -13,6 +13,7 @@ "Unit/DraftImageReads.test.tsx", "Unit/ImageAttachments.test.tsx", "Unit/StagedRail.test.tsx", + "Unit/UseNow.test.tsx", "../resources/js/types/*.d.ts", "draft-images.config.ts" ]