1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = useStableMemo;
|
5 | var _react = require("react");
|
6 | function isEqual(a, b) {
|
7 | if (a.length !== b.length) return false;
|
8 | for (let i = 0; i < a.length; i++) {
|
9 | if (a[i] !== b[i]) {
|
10 | return false;
|
11 | }
|
12 | }
|
13 | return true;
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function useStableMemo(factory, deps) {
|
24 | let isValid = true;
|
25 | const valueRef = (0, _react.useRef)();
|
26 |
|
27 | if (!valueRef.current) {
|
28 | valueRef.current = {
|
29 | deps,
|
30 | result: factory()
|
31 | };
|
32 |
|
33 | } else {
|
34 | isValid = !!(deps && valueRef.current.deps && isEqual(deps, valueRef.current.deps));
|
35 | }
|
36 | const cache = isValid ? valueRef.current : {
|
37 | deps,
|
38 | result: factory()
|
39 | };
|
40 |
|
41 | valueRef.current = cache;
|
42 | return cache.result;
|
43 | } |
\ | No newline at end of file |