UNPKG

9.12 kBSource Map (JSON)View Raw
1{"version":3,"file":"use-deep-compare-effect.umd.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","checkDeps","deps","Error","every","isPrimitive","val","test","useDeepCompareMemoize","value","ref","React","useRef","signalRef","deepEqual","current","useMemo","useDeepCompareEffect","callback","dependencies","useEffect","useDeepCompareEffectNoCheck"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAAA,IAAIA,GAAG,GAAGC,MAAM,CAACC,SAAP,CAAiBC,cAA3B;;CAEA,SAASC,IAAT,CAAcC,IAAd,EAAoBC,GAApB,EAAyBC,GAAzB,EAA8B;CAC7B,uDAAYF,IAAI,CAACG,IAAL,EAAZ,wCAAyB;CAApBD,IAAAA,GAAoB;CACxB,QAAIE,MAAM,CAACF,GAAD,EAAMD,GAAN,CAAV,EAAsB,OAAOC,GAAP;CACtB;CACD;;CAEM,SAASE,MAAT,CAAgBC,GAAhB,EAAqBC,GAArB,EAA0B;CAChC,MAAIC,IAAJ,EAAUC,GAAV,EAAeC,GAAf;CACA,MAAIJ,GAAG,KAAKC,GAAZ,EAAiB,OAAO,IAAP;;CAEjB,MAAID,GAAG,IAAIC,GAAP,IAAc,CAACC,IAAI,GAACF,GAAG,CAACK,WAAV,MAA2BJ,GAAG,CAACI,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