import { useCallback, useEffect, useRef } from 'react'; import { viewerEpoch } from '../utils/writeThroughHold'; // Call it as an action starts; the check it returns says whether the page that started it is still shown to the same // viewer, so a completion that arrives later does nothing on the page or app that replaced it. export const useLifetime = (): (() => () => boolean) => { const mountedRef = useRef(true); useEffect(() => { mountedRef.current = true; return () => { mountedRef.current = false; }; }, []); return useCallback(() => { const viewer = viewerEpoch(); return () => mountedRef.current && viewer === viewerEpoch(); }, []); };