@homebound/beam 2.117.1 → 2.117.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -6,7 +6,7 @@ const react_1 = require("react");
|
|
|
6
6
|
/** Evaluates a computed function `fn` to a regular value and triggers a re-render whenever it changes. */
|
|
7
7
|
function useComputed(fn, deps) {
|
|
8
8
|
// We always return the useRef value, and use this just to trigger re-renders
|
|
9
|
-
const [,
|
|
9
|
+
const [, setTick] = (0, react_1.useState)(0);
|
|
10
10
|
const ref = (0, react_1.useRef)({
|
|
11
11
|
runner: undefined,
|
|
12
12
|
value: undefined,
|
|
@@ -17,25 +17,25 @@ function useComputed(fn, deps) {
|
|
|
17
17
|
// with `useEffect`, which would only get calc'd after the 1st render has
|
|
18
18
|
// already been done.
|
|
19
19
|
(0, react_1.useMemo)(() => {
|
|
20
|
-
let tick = 0;
|
|
21
20
|
const { current } = ref;
|
|
22
21
|
// If deps has changed, unhook the previous observer
|
|
23
22
|
if (current.runner) {
|
|
24
23
|
current.runner();
|
|
25
24
|
}
|
|
25
|
+
// If deps has changed, we're already re-running, so don't trigger a 2nd one
|
|
26
|
+
current.hasRan = false;
|
|
26
27
|
current.runner = (0, mobx_1.autorun)(() => {
|
|
27
28
|
// Always eval fn() (even on 1st render) to register our observable.
|
|
28
29
|
const newValue = fn();
|
|
29
|
-
const oldValue = current
|
|
30
|
+
const { value: oldValue, hasRan: oldHasRun } = current;
|
|
30
31
|
current.value = newValue;
|
|
31
32
|
current.hasRan = true;
|
|
32
33
|
// Only trigger a re-render if this is not the 1st autorun. Note
|
|
33
34
|
// that if deps has changed, we're inherently in a re-render so also
|
|
34
35
|
// don't need to trigger an additional re-render.
|
|
35
|
-
if (
|
|
36
|
-
|
|
36
|
+
if (oldHasRun && newValue !== oldValue) {
|
|
37
|
+
setTick((tick) => tick + 1);
|
|
37
38
|
}
|
|
38
|
-
tick++;
|
|
39
39
|
});
|
|
40
40
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
41
41
|
}, deps);
|