{"version":3,"file":"use-latest-ref.cjs","names":[],"sources":["../../src/hooks/use-latest-ref.ts"],"sourcesContent":["/* eslint-disable react-hooks/refs -- writing at render time is deliberate; an effect would add a staleness window (see the docstring) */\nimport { useRef, type RefObject } from \"react\";\n\n/**\n * Keeps a ref pointing at the most recent value it was given.\n *\n * The escape hatch for reading fresh state inside something that must not be\n * re-created when that state changes: an interval, a subscription, an event\n * listener registered once on mount. Listing the value in the effect's\n * dependencies would tear the effect down and set it back up on every change;\n * omitting it captures the value from the render that created the closure and\n * never sees another. The ref is neither — a stable object whose `current` is\n * always current.\n *\n * The assignment happens **during render**, for the same reason\n * {@link useStableCallback} does it: moving it into an effect opens a\n * one-commit staleness window, where an effect declared earlier in the same\n * commit reads the previous render's value.\n *\n * Reach for {@link useStableCallback} instead when the value is a function you\n * want to *call* — it hands back a callable with a stable identity, rather than\n * making every call site reach through `.current`.\n *\n * **List the returned ref in your effect's dependencies.** It is a stable object,\n * so the effect never re-runs because of it, but `react-hooks/exhaustive-deps`\n * cannot prove that for a custom hook the way it does for a bare `useRef`.\n * Listing it lets the rule verify the array instead of taking an omission on\n * trust.\n *\n * **Not for holding the previous value.** The write happens during render, so\n * `.current` is already the current value by the time any effect reads it. A hook\n * that wants the value from the *previous* commit — `usePrevious` — needs the\n * effect-based write, and swapping it for this would make it return the present.\n *\n * @typeParam T - The tracked value.\n * @param value - The value to track. Written on every render.\n * @returns A stable ref whose `current` holds the latest `value`.\n *\n * @example\n * const optionsRef = useLatestRef(options);\n *\n * useEffect(() => {\n *   const id = setInterval(() => poll(optionsRef.current), 5_000);\n *   return () => clearInterval(id);\n * }, []); // the interval survives every options change, and still reads the latest\n */\nexport function useLatestRef<T>(value: T): RefObject<T> {\n    const ref = useRef(value);\n    ref.current = value;\n    return ref;\n}\n"],"mappings":"uBA8CA,SAAgB,EAAgB,EAAwB,CACpD,IAAM,GAAA,EAAM,EAAA,OAAA,CAAO,CAAK,EAExB,MADA,GAAI,QAAU,EACP,CACX"}