UNPKG

6.42 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
3 typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.UseDeepCompareEffect = {}, global.React));
5})(this, (function (exports, React) { 'use strict';
6
7 function _interopNamespace(e) {
8 if (e && e.__esModule) return e;
9 var n = Object.create(null);
10 if (e) {
11 Object.keys(e).forEach(function (k) {
12 if (k !== 'default') {
13 var d = Object.getOwnPropertyDescriptor(e, k);
14 Object.defineProperty(n, k, d.get ? d : {
15 enumerable: true,
16 get: function () { return e[k]; }
17 });
18 }
19 });
20 }
21 n["default"] = e;
22 return Object.freeze(n);
23 }
24
25 var React__namespace = /*#__PURE__*/_interopNamespace(React);
26
27 function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
28
29 function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
30
31 function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
32
33 var has = Object.prototype.hasOwnProperty;
34
35 function find(iter, tar, key) {
36 for (var _iterator = _createForOfIteratorHelperLoose(iter.keys()), _step; !(_step = _iterator()).done;) {
37 key = _step.value;
38 if (dequal(key, tar)) return key;
39 }
40 }
41
42 function dequal(foo, bar) {
43 var ctor, len, tmp;
44 if (foo === bar) return true;
45
46 if (foo && bar && (ctor = foo.constructor) === bar.constructor) {
47 if (ctor === Date) return foo.getTime() === bar.getTime();
48 if (ctor === RegExp) return foo.toString() === bar.toString();
49
50 if (ctor === Array) {
51 if ((len = foo.length) === bar.length) {
52 while (len-- && dequal(foo[len], bar[len])) {
53 }
54 }
55
56 return len === -1;
57 }
58
59 if (ctor === Set) {
60 if (foo.size !== bar.size) {
61 return false;
62 }
63
64 for (var _iterator2 = _createForOfIteratorHelperLoose(foo), _step2; !(_step2 = _iterator2()).done;) {
65 len = _step2.value;
66 tmp = len;
67
68 if (tmp && typeof tmp === 'object') {
69 tmp = find(bar, tmp);
70 if (!tmp) return false;
71 }
72
73 if (!bar.has(tmp)) return false;
74 }
75
76 return true;
77 }
78
79 if (ctor === Map) {
80 if (foo.size !== bar.size) {
81 return false;
82 }
83
84 for (var _iterator3 = _createForOfIteratorHelperLoose(foo), _step3; !(_step3 = _iterator3()).done;) {
85 len = _step3.value;
86 tmp = len[0];
87
88 if (tmp && typeof tmp === 'object') {
89 tmp = find(bar, tmp);
90 if (!tmp) return false;
91 }
92
93 if (!dequal(len[1], bar.get(tmp))) {
94 return false;
95 }
96 }
97
98 return true;
99 }
100
101 if (ctor === ArrayBuffer) {
102 foo = new Uint8Array(foo);
103 bar = new Uint8Array(bar);
104 } else if (ctor === DataView) {
105 if ((len = foo.byteLength) === bar.byteLength) {
106 while (len-- && foo.getInt8(len) === bar.getInt8(len)) {
107 }
108 }
109
110 return len === -1;
111 }
112
113 if (ArrayBuffer.isView(foo)) {
114 if ((len = foo.byteLength) === bar.byteLength) {
115 while (len-- && foo[len] === bar[len]) {
116 }
117 }
118
119 return len === -1;
120 }
121
122 if (!ctor || typeof foo === 'object') {
123 len = 0;
124
125 for (ctor in foo) {
126 if (has.call(foo, ctor) && ++len && !has.call(bar, ctor)) return false;
127 if (!(ctor in bar) || !dequal(foo[ctor], bar[ctor])) return false;
128 }
129
130 return Object.keys(bar).length === len;
131 }
132 }
133
134 return foo !== foo && bar !== bar;
135 }
136
137 function checkDeps(deps) {
138 if (!deps || !deps.length) {
139 throw new Error('useDeepCompareEffect should not be used with no dependencies. Use React.useEffect instead.');
140 }
141
142 if (deps.every(isPrimitive)) {
143 throw new Error('useDeepCompareEffect should not be used with dependencies that are all primitive values. Use React.useEffect instead.');
144 }
145 }
146
147 function isPrimitive(val) {
148 return val == null || /^[sbn]/.test(typeof val);
149 }
150 /**
151 * @param value the value to be memoized (usually a dependency list)
152 * @returns a momoized version of the value as long as it remains deeply equal
153 */
154
155
156 function useDeepCompareMemoize(value) {
157 var ref = React__namespace.useRef(value);
158 var signalRef = React__namespace.useRef(0);
159
160 if (!dequal(value, ref.current)) {
161 ref.current = value;
162 signalRef.current += 1;
163 } // eslint-disable-next-line react-hooks/exhaustive-deps
164
165
166 return React__namespace.useMemo(function () {
167 return ref.current;
168 }, [signalRef.current]);
169 }
170
171 function useDeepCompareEffect(callback, dependencies) {
172 {
173 checkDeps(dependencies);
174 } // eslint-disable-next-line react-hooks/exhaustive-deps
175
176
177 return React__namespace.useEffect(callback, useDeepCompareMemoize(dependencies));
178 }
179
180 function useDeepCompareEffectNoCheck(callback, dependencies) {
181 // eslint-disable-next-line react-hooks/exhaustive-deps
182 return React__namespace.useEffect(callback, useDeepCompareMemoize(dependencies));
183 }
184
185 exports["default"] = useDeepCompareEffect;
186 exports.useDeepCompareEffectNoCheck = useDeepCompareEffectNoCheck;
187 exports.useDeepCompareMemoize = useDeepCompareMemoize;
188
189 Object.defineProperty(exports, '__esModule', { value: true });
190
191}));
192//# sourceMappingURL=use-deep-compare-effect.umd.js.map