Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

use-visible-second-clock.ts

24 lines896 Bytes

1import { useIsFocused } from "@react-navigation/native";
2import { secondTick } from "@t3tools/client-runtime/clock";
3import { useSyncExternalStore } from "react";
4​
5import { useAppActive } from "../../lib/appState";
6​
7const subscribeNever = () => () => undefined;
8​
9/**
10 * Retained routes stay mounted when hidden, so an ungated tick would keep
11 * re-rendering them. Every visible label shares one module-level tick.
12 */
13export function useVisibleSecondClock(enabled: boolean): number {
14 const focused = useIsFocused();
15 const appActive = useAppActive();
16 const ticking = enabled && focused && appActive;
17 // While paused the snapshot still reads the shared tick, so resuming shows
18 // the current second rather than the one the clock stopped on.
19 return useSyncExternalStore(
20 ticking ? secondTick.subscribe : subscribeNever,
21 secondTick.getSnapshot,
22 secondTick.getSnapshot,
23 );