[rhir] Patch for reactive computed loads
We need to check reactivity of both the operand and its resolved source (if operand is produced by a LoadLocal / PropertyLoad / ComputedLoad). Both the operand and its source can have reactivity. e.g. ```js const o = makeObject(); // source has no reactivity const x = o[props.x]; // x is reactive ```
mofeiZ committed
Mar 27, 2023 at 13:27 UTC
027f773179756bab308af2b3f0965f2dba36a067
3 files changed
+23
-9
compiler/forget/src/ReactiveScopes/InferReactiveIdentifiers.ts
+13
-3
@@ -45,9 +45,19 @@ class Visitor extends ReactiveFunctionVisitor<State> {
45
if (operand.effect === Effect.Store) {
46
continue;
47
}
48
- const resolvedId: IdentifierId =
49
- state.temporaries.get(operand.identifier.id) ?? operand.identifier.id;
50
- if (state.reactivityMap.get(resolvedId)) {
48
+ const ownId = operand.identifier.id;
49
+ const resolvedId = state.temporaries.get(ownId);
50
+ // We need to check reactivity of both the operand and its resolved source (if operand is
51
+ // produced by a LoadLocal / PropertyLoad / ComputedLoad). Both the operand and its source
52
+ // can have reactivity. e.g.
53
+ // ```js
54
+ // const o = makeObject(); // source has no reactivity
55
+ // const x = o[props.x]; // x is reactive
56
+ // ```
57
+ if (
58
+ state.reactivityMap.get(ownId) ||
59
+ (resolvedId && state.reactivityMap.get(resolvedId))
60
+ ) {
61
hasReactiveInput = true;
62
break;
63
}
compiler/forget/src/__tests__/fixtures/compiler/computed-load-primitive-as-dependency.expect.md
renamed
+10
-6
@@ -18,16 +18,20 @@ function Component(props) {
18
19
```javascript
20
function Component(props) {
21
- const $ = React.unstable_useMemoCache(1);
21
+ const $ = React.unstable_useMemoCache(2);
22
const a = foo();
23
- let t0;
24
- if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
25
- t0 = bar(a[props.a] + 1);
23
+
24
+ const t0 = a[props.a] + 1;
25
+ const c_0 = $[0] !== t0;
26
+ let t1;
27
+ if (c_0) {
28
+ t1 = bar(t0);
29
$[0] = t0;
30
+ $[1] = t1;
31
} else {
28
- t0 = $[0];
32
+ t1 = $[1];
33
}
30
- const b = t0;
34
+ const b = t1;
35
return b;
36
}
37