{"version":3,"file":"useStable-CwvpwWKi.mjs","names":[],"sources":["../src/react-util/useStable.ts"],"sourcesContent":["import { useRef } from \"react\";\n\nconst depsChanged = (previous: React.DependencyList, next: React.DependencyList): boolean =>\n  previous.length !== next.length || previous.some((dep, i) => !Object.is(dep, next[i]));\n\n/**\n * Build a value once and keep it until `deps` change — the guarantee `useMemo` does not make.\n *\n * `useMemo` is a performance hint: React may discard a cached value and recompute it whenever it\n * likes. That is harmless for a derived number and quietly wrong for anything holding state, which\n * would be silently rebuilt mid-life — a lazy observable dropping what it had loaded, a store losing\n * its subscriptions. Reach for this whenever the value *is* the state rather than a view of it.\n *\n * ```ts\n * const controller = useStable(() => new AbortController(), [requestId]);\n * ```\n *\n * `deps` are compared with `Object.is`, exactly as React compares its own.\n */\nexport function useStable<T>(create: () => T, deps: React.DependencyList): T {\n  const ref = useRef<{ deps: React.DependencyList; value: T } | undefined>(undefined);\n\n  if (!ref.current || depsChanged(ref.current.deps, deps)) {\n    ref.current = { deps, value: create() };\n  }\n\n  return ref.current.value;\n}\n"],"mappings":";;;AAEA,MAAM,eAAe,UAAgC,SACnD,SAAS,WAAW,KAAK,UAAU,SAAS,MAAM,KAAK,MAAM,CAAC,OAAO,GAAG,KAAK,KAAK,EAAE,CAAC;;;;;;;;;;;;;;;AAgBvF,SAAgB,UAAa,QAAiB,MAA+B;CAC3E,MAAM,MAAM,OAA6D,MAAS;CAElF,IAAI,CAAC,IAAI,WAAW,YAAY,IAAI,QAAQ,MAAM,IAAI,GACpD,IAAI,UAAU;EAAE;EAAM,OAAO,OAAO;CAAE;CAGxC,OAAO,IAAI,QAAQ;AACrB"}