Download mobile/src/hooks/useEngineSnapshot.ts from Anirudh1950/MentraOS: direct link, hf CLI and curl.
- Browser
- Download file 1.2 kB
-
https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/src/hooks/useEngineSnapshot.ts
- Command line
-
hf download hf://Anirudh1950/MentraOS/mobile/src/hooks/useEngineSnapshot.ts
-
curl -L -o useEngineSnapshot.ts https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/src/hooks/useEngineSnapshot.ts
1.2 kB
| import {useEffect, useRef, useState} from "react" | |
| /** | |
| * Subscribe a component to an engine read-model: `getSnapshot` reads the current | |
| * projection, `subscribe` wires the matching `onX` engine subscription. | |
| * | |
| * The subscription is established ONCE on mount — `subscribe` must target the | |
| * same engine surface for the component's lifetime (every call site passes a | |
| * fixed `engine.*.onX`). The refs keep the *callbacks* fresh across renders, | |
| * but an identity change of `subscribe` is deliberately ignored; a | |
| * prop-dependent subscription would need `subscribe` in the effect deps and a | |
| * `useCallback` at the call site instead. | |
| */ | |
| export function useEngineSnapshot<T>(getSnapshot: () => T, subscribe: (onChange: () => void) => () => void): T { | |
| const getSnapshotRef = useRef(getSnapshot) | |
| const subscribeRef = useRef(subscribe) | |
| getSnapshotRef.current = getSnapshot | |
| subscribeRef.current = subscribe | |
| const [snapshot, setSnapshot] = useState(() => getSnapshot()) | |
| useEffect(() => { | |
| setSnapshot(getSnapshotRef.current()) | |
| return subscribeRef.current(() => { | |
| setSnapshot(getSnapshotRef.current()) | |
| }) | |
| }, []) | |
| return snapshot | |
| } | |