All files / core/blong-browser/src/feedback useFeedbackFreeze.ts

29.41% Statements 10/34
13.33% Branches 2/15
50% Functions 2/4
27.27% Lines 9/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                          8x 8x 8x 8x 8x 8x 8x   8x 8x                                                                                    
/**
 * useFeedbackFreeze.ts — a timer that waits while the mode is on (`@@T-465`).
 *
 * `ActionHint` hides itself after two seconds. Entering feedback mode in that
 * window must not lose the message, so the timer pauses with its remaining time
 * intact and resumes when the mode ends. The alternative — blocking the mode until
 * the hint expires — would make the switch feel broken instead.
 */
 
import {useEffect, useRef} from 'react';
 
import {useFeedbackActive} from './useFeedbackTarget.js';
 
/**
 * Run `onElapsed` after `delay` ms, holding the clock while feedback mode is
 * active.
 *
 * @param delay    the full lifetime, in milliseconds
 * @param onElapsed called once per expiry
 * @param enabled  schedule anything at all; `false` disables the timer entirely
 * @param resetKey a value that identifies the thing being timed — a new hint
 *                 restarts the full lifetime when this changes
 */
export function useFeedbackFreeze(
    delay: number,
    onElapsed: () => void,
    enabled = true,
    resetKey?: unknown,
): void {
    const active = useFeedbackActive();
    const callbackRef = useRef(onElapsed);
    callbackRef.current = onElapsed;
    const remainingRef = useRef(delay);
    const startedRef = useRef<number | null>(null);
    const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
    const keyRef = useRef<unknown>(resetKey);
 
    useEffect(() => {
        Eif (!enabled) return;
        if (keyRef.current !== resetKey) {
            keyRef.current = resetKey;
            remainingRef.current = delay;
            startedRef.current = null;
        }
        if (active) {
            // Pause: keep what is left of the lifetime for the resume below.
            if (timerRef.current) {
                clearTimeout(timerRef.current);
                timerRef.current = null;
            }
            if (startedRef.current != null) {
                remainingRef.current = Math.max(
                    0,
                    remainingRef.current - (Date.now() - startedRef.current),
                );
                startedRef.current = null;
            }
            return;
        }
        startedRef.current = Date.now();
        timerRef.current = setTimeout(() => {
            timerRef.current = null;
            startedRef.current = null;
            callbackRef.current();
        }, remainingRef.current);
        return () => {
            if (timerRef.current) {
                clearTimeout(timerRef.current);
                timerRef.current = null;
            }
            if (startedRef.current != null) {
                remainingRef.current = Math.max(
                    0,
                    remainingRef.current - (Date.now() - startedRef.current),
                );
                startedRef.current = null;
            }
        };
    }, [active, enabled, delay, resetKey]);
}