{"version":3,"file":"useSameValuesReference.mjs","names":[],"sources":["../../../src/utils/useSameValuesReference.ts"],"sourcesContent":["import { useLayoutEffect, useRef } from 'react';\n\nconst isPlainObject = (value: unknown): value is Record<string, unknown> => {\n  if (typeof value !== 'object' || value === null) return false;\n  const prototype = Object.getPrototypeOf(value);\n  return prototype === Object.prototype || prototype === null;\n};\n\n/**\n * Whether two values hold the same data: plain objects and arrays are compared key by key, at\n * every depth, and anything else (functions, class instances, dates) by identity. A pair already\n * being compared further up counts as equal, so cyclic values end the comparison.\n */\nconst hasSameValues = (\n  a: unknown,\n  b: unknown,\n  pairsInProgress: WeakMap<object, WeakSet<object>> = new WeakMap(),\n): boolean => {\n  if (Object.is(a, b)) return true;\n  const bothArrays = Array.isArray(a) && Array.isArray(b);\n  if (bothArrays && a.length !== b.length) return false;\n  if (!bothArrays && (!isPlainObject(a) || !isPlainObject(b))) return false;\n\n  const left = a as Record<string, unknown>;\n  const right = b as Record<string, unknown>;\n  if (pairsInProgress.get(left)?.has(right)) return true;\n  const comparedWithLeft = pairsInProgress.get(left) ?? new WeakSet<object>();\n  comparedWithLeft.add(right);\n  pairsInProgress.set(left, comparedWithLeft);\n\n  const keys = Object.keys(left);\n  return (\n    keys.length === Object.keys(right).length &&\n    keys.every(\n      (key) =>\n        Object.prototype.hasOwnProperty.call(right, key) &&\n        hasSameValues(left[key], right[key], pairsInProgress),\n    )\n  );\n};\n\n/**\n * Returns the last committed value for as long as new values hold the same data, so an object or\n * array literal created on every render keeps one reference. Use it to feed such a value to a\n * hook dependency list, a memoized component or anything else that compares by identity.\n *\n * Plain objects and arrays are compared key by key, at every depth; functions, class instances\n * and other values by identity, so pass stable functions inside the value. The reference advances\n * only when React commits a render, so a render that React discards cannot replace it.\n *\n * @param value - Value whose reference should only change when its data does.\n * @returns `value`, or the previously committed value when it holds the same data.\n */\nexport const useSameValuesReference = <T>(value: T): T => {\n  const committedRef = useRef(value);\n  const next = hasSameValues(committedRef.current, value) ? committedRef.current : value;\n  useLayoutEffect(() => {\n    committedRef.current = next;\n  });\n  return next;\n};\n"],"mappings":";;AAEA,IAAM,iBAAiB,UAAqD;CAC1E,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM,OAAO;CACxD,MAAM,YAAY,OAAO,eAAe,KAAK;CAC7C,OAAO,cAAc,OAAO,aAAa,cAAc;AACzD;;;;;;AAOA,IAAM,iBACJ,GACA,GACA,kCAAoD,IAAI,QAAQ,MACpD;CACZ,IAAI,OAAO,GAAG,GAAG,CAAC,GAAG,OAAO;CAC5B,MAAM,aAAa,MAAM,QAAQ,CAAC,KAAK,MAAM,QAAQ,CAAC;CACtD,IAAI,cAAc,EAAE,WAAW,EAAE,QAAQ,OAAO;CAChD,IAAI,CAAC,eAAe,CAAC,cAAc,CAAC,KAAK,CAAC,cAAc,CAAC,IAAI,OAAO;CAEpE,MAAM,OAAO;CACb,MAAM,QAAQ;CACd,IAAI,gBAAgB,IAAI,IAAI,CAAC,EAAE,IAAI,KAAK,GAAG,OAAO;CAClD,MAAM,mBAAmB,gBAAgB,IAAI,IAAI,qBAAK,IAAI,QAAgB;CAC1E,iBAAiB,IAAI,KAAK;CAC1B,gBAAgB,IAAI,MAAM,gBAAgB;CAE1C,MAAM,OAAO,OAAO,KAAK,IAAI;CAC7B,OACE,KAAK,WAAW,OAAO,KAAK,KAAK,CAAC,CAAC,UACnC,KAAK,OACF,QACC,OAAO,UAAU,eAAe,KAAK,OAAO,GAAG,KAC/C,cAAc,KAAK,MAAM,MAAM,MAAM,eAAe,CACxD;AAEJ;;;;;;;;;;;;;AAcA,IAAa,0BAA6B,UAAgB;CACxD,MAAM,eAAe,OAAO,KAAK;CACjC,MAAM,OAAO,cAAc,aAAa,SAAS,KAAK,IAAI,aAAa,UAAU;CACjF,sBAAsB;EACpB,aAAa,UAAU;CACzB,CAAC;CACD,OAAO;AACT"}