1 | {"version":3,"file":"use-deep-compare-effect.umd.min.js","sources":["../node_modules/dequal/dist/index.mjs","../src/index.ts"],"sourcesContent":["var has = Object.prototype.hasOwnProperty;\n\nfunction find(iter, tar, key) {\n\tfor (key of iter.keys()) {\n\t\tif (dequal(key, tar)) return key;\n\t}\n}\n\nexport function dequal(foo, bar) {\n\tvar ctor, len, tmp;\n\tif (foo === bar) return true;\n\n\tif (foo && bar && (ctor=foo.constructor) === bar.constructor) {\n\t\tif (ctor === Date) return foo.getTime() === bar.getTime();\n\t\tif (ctor === RegExp) return foo.toString() === bar.toString();\n\n\t\tif (ctor === Array) {\n\t\t\tif ((len=foo.length) === bar.length) {\n\t\t\t\twhile (len-- && dequal(foo[len], bar[len]));\n\t\t\t}\n\t\t\treturn len === -1;\n\t\t}\n\n\t\tif (ctor === Set) {\n\t\t\tif (foo.size !== bar.size) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tfor (len of foo) {\n\t\t\t\ttmp = len;\n\t\t\t\tif (tmp && typeof tmp === 'object') {\n\t\t\t\t\ttmp = find(bar, tmp);\n\t\t\t\t\tif (!tmp) return false;\n\t\t\t\t}\n\t\t\t\tif (!bar.has(tmp)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\n\t\tif (ctor === Map) {\n\t\t\tif (foo.size !== bar.size) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tfor (len of foo) {\n\t\t\t\ttmp = len[0];\n\t\t\t\tif (tmp && typeof tmp === 'object') {\n\t\t\t\t\ttmp = find(bar, tmp);\n\t\t\t\t\tif (!tmp) return false;\n\t\t\t\t}\n\t\t\t\tif (!dequal(len[1], bar.get(tmp))) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\n\t\tif (ctor === ArrayBuffer) {\n\t\t\tfoo = new Uint8Array(foo);\n\t\t\tbar = new Uint8Array(bar);\n\t\t} else if (ctor === DataView) {\n\t\t\tif ((len=foo.byteLength) === bar.byteLength) {\n\t\t\t\twhile (len-- && foo.getInt8(len) === bar.getInt8(len));\n\t\t\t}\n\t\t\treturn len === -1;\n\t\t}\n\n\t\tif (ArrayBuffer.isView(foo)) {\n\t\t\tif ((len=foo.byteLength) === bar.byteLength) {\n\t\t\t\twhile (len-- && foo[len] === bar[len]);\n\t\t\t}\n\t\t\treturn len === -1;\n\t\t}\n\n\t\tif (!ctor || typeof foo === 'object') {\n\t\t\tlen = 0;\n\t\t\tfor (ctor in foo) {\n\t\t\t\tif (has.call(foo, ctor) && ++len && !has.call(bar, ctor)) return false;\n\t\t\t\tif (!(ctor in bar) || !dequal(foo[ctor], bar[ctor])) return false;\n\t\t\t}\n\t\t\treturn Object.keys(bar).length === len;\n\t\t}\n\t}\n\n\treturn foo !== foo && bar !== bar;\n}\n","import * as React from 'react'\nimport {dequal as deepEqual} from 'dequal'\n\ntype UseEffectParams = Parameters<typeof React.useEffect>\ntype EffectCallback = UseEffectParams[0]\ntype DependencyList = UseEffectParams[1]\n// yes, I know it's void, but I like what this communicates about\n// the intent of these functions: It's just like useEffect\ntype UseEffectReturn = ReturnType<typeof React.useEffect>\n\nfunction checkDeps(deps: DependencyList) {\n if (!deps || !deps.length) {\n throw new Error(\n 'useDeepCompareEffect should not be used with no dependencies. Use React.useEffect instead.',\n )\n }\n if (deps.every(isPrimitive)) {\n throw new Error(\n 'useDeepCompareEffect should not be used with dependencies that are all primitive values. Use React.useEffect instead.',\n )\n }\n}\n\nfunction isPrimitive(val: unknown) {\n return val == null || /^[sbn]/.test(typeof val)\n}\n\n/**\n * @param value the value to be memoized (usually a dependency list)\n * @returns a momoized version of the value as long as it remains deeply equal\n */\nexport function useDeepCompareMemoize<T>(value: T) {\n const ref = React.useRef<T>(value)\n const signalRef = React.useRef<number>(0)\n\n if (!deepEqual(value, ref.current)) {\n ref.current = value\n signalRef.current += 1\n }\n\n // eslint-disable-next-line react-hooks/exhaustive-deps\n return React.useMemo(() => ref.current, [signalRef.current])\n}\n\nfunction useDeepCompareEffect(\n callback: EffectCallback,\n dependencies: DependencyList,\n): UseEffectReturn {\n if (process.env.NODE_ENV !== 'production') {\n checkDeps(dependencies)\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n return React.useEffect(callback, useDeepCompareMemoize(dependencies))\n}\n\nexport function useDeepCompareEffectNoCheck(\n callback: EffectCallback,\n dependencies: DependencyList,\n): UseEffectReturn {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n return React.useEffect(callback, useDeepCompareMemoize(dependencies))\n}\n\nexport default useDeepCompareEffect\n"],"names":["has","Object","prototype","hasOwnProperty","find","iter","tar","key","keys","dequal","foo","bar","ctor","len","tmp","constructor","Date","getTime","RegExp","toString","Array","length","Set","size","Map","get","ArrayBuffer","Uint8Array","DataView","byteLength","getInt8","isView","call","useDeepCompareMemoize","value","ref","React","useRef","signalRef","deepEqual","current","useMemo","callback","dependencies","useEffect"],"mappings":"44CAAA,IAAIA,EAAMC,OAAOC,UAAUC,eAE3B,SAASC,EAAKC,EAAMC,EAAKC,iBACZF,EAAKG,0BACZC,EADAF,UACYD,GAAM,OAAOC,EAIxB,SAASE,EAAOC,EAAKC,OACvBC,EAAMC,EAAKC,KACXJ,IAAQC,EAAK,OAAO,KAEpBD,GAAOC,IAAQC,EAAKF,EAAIK,eAAiBJ,EAAII,YAAa,IACzDH,IAASI,KAAM,OAAON,EAAIO,YAAcN,EAAIM,aAC5CL,IAASM,OAAQ,OAAOR,EAAIS,aAAeR,EAAIQ,cAE/CP,IAASQ,MAAO,KACdP,EAAIH,EAAIW,UAAYV,EAAIU,YACrBR,KAASJ,EAAOC,EAAIG,GAAMF,EAAIE,aAEtB,IAATA,KAGJD,IAASU,IAAK,IACbZ,EAAIa,OAASZ,EAAIY,YACb,gBAEIb,kBAAK,KAChBI,EADID,YAEsB,iBAARC,KACjBA,EAAMV,EAAKO,EAAKG,IACN,OAAO,MAEbH,EAAIX,IAAIc,GAAM,OAAO,SAEpB,KAGJF,IAASY,IAAK,IACb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