|
1 | | -import { useEffect, useState } from "react"; |
| 1 | +import { useSyncExternalStore } from "react"; |
2 | 2 |
|
3 | 3 | /** Minute-quantized clock ("YYYY-MM-DDTHH:MM") for settled-state resolution. |
4 | | - Ticks are aligned to UTC minute boundaries — not offset from mount time — |
5 | | - so every consumer (sidebar partition, composer banner) crosses each minute |
6 | | - together and effectiveSettled can never disagree between surfaces. */ |
7 | | -export function useNowMinute(): string { |
8 | | - const [nowMinute, setNowMinute] = useState(() => new Date().toISOString().slice(0, 16)); |
9 | | - useEffect(() => { |
10 | | - let intervalId: number | null = null; |
11 | | - const timeoutId = window.setTimeout( |
12 | | - () => { |
13 | | - setNowMinute(new Date().toISOString().slice(0, 16)); |
14 | | - intervalId = window.setInterval( |
15 | | - () => setNowMinute(new Date().toISOString().slice(0, 16)), |
16 | | - 60_000, |
17 | | - ); |
18 | | - }, |
19 | | - 60_000 - (Date.now() % 60_000), |
20 | | - ); |
21 | | - return () => { |
22 | | - window.clearTimeout(timeoutId); |
23 | | - if (intervalId !== null) window.clearInterval(intervalId); |
24 | | - }; |
25 | | - }, []); |
| 4 | + One module-level timer feeds every consumer through useSyncExternalStore, |
| 5 | + so all surfaces resolving effectiveSettled against it (sidebar partition, |
| 6 | + composer banner) share a single value by construction and tick on UTC |
| 7 | + minute boundaries together. */ |
| 8 | + |
| 9 | +function currentMinute(): string { |
| 10 | + return new Date().toISOString().slice(0, 16); |
| 11 | +} |
| 12 | + |
| 13 | +let nowMinute = currentMinute(); |
| 14 | +let timerId: number | null = null; |
| 15 | +let timerIsInterval = false; |
| 16 | +const listeners = new Set<() => void>(); |
| 17 | + |
| 18 | +function tick(): void { |
| 19 | + const next = currentMinute(); |
| 20 | + if (next !== nowMinute) { |
| 21 | + nowMinute = next; |
| 22 | + for (const listener of listeners) listener(); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function startTimer(): void { |
| 27 | + // Align to the next UTC minute boundary, then tick every 60s. Ticks re-read |
| 28 | + // the clock, so a throttled or late timer self-corrects when it fires. |
| 29 | + timerIsInterval = false; |
| 30 | + timerId = window.setTimeout( |
| 31 | + () => { |
| 32 | + tick(); |
| 33 | + timerIsInterval = true; |
| 34 | + timerId = window.setInterval(tick, 60_000); |
| 35 | + }, |
| 36 | + 60_000 - (Date.now() % 60_000), |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +function subscribe(listener: () => void): () => void { |
| 41 | + if (listeners.size === 0) { |
| 42 | + // The stored minute may have gone stale while no one was subscribed. |
| 43 | + nowMinute = currentMinute(); |
| 44 | + startTimer(); |
| 45 | + } |
| 46 | + listeners.add(listener); |
| 47 | + return () => { |
| 48 | + listeners.delete(listener); |
| 49 | + if (listeners.size === 0 && timerId !== null) { |
| 50 | + if (timerIsInterval) window.clearInterval(timerId); |
| 51 | + else window.clearTimeout(timerId); |
| 52 | + timerId = null; |
| 53 | + } |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +function getSnapshot(): string { |
26 | 58 | return nowMinute; |
27 | 59 | } |
| 60 | + |
| 61 | +export function useNowMinute(): string { |
| 62 | + return useSyncExternalStore(subscribe, getSnapshot, getSnapshot); |
| 63 | +} |
0 commit comments