MentraOS / mobile /modules /engine /src /react /useEngineSnapshot.ts
Anirudh1950's picture
Add files using upload-large-folder tool
8a29801 verified
Raw History Blame Contribute Delete
574 Bytes
import {useEffect, useRef, useState} from "react"
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
}