[hir] Don't track ref value as a valid dep

This commit is contained in:
Sathya Gunasekaran
2023-05-31 13:54:59 +01:00
parent bedf0cc6c5
commit 91f41f5ee3
5 changed files with 117 additions and 0 deletions
@@ -11,6 +11,7 @@ import {
IdentifierId,
InstructionId,
InstructionKind,
isRefValueType,
isUseRefType,
makeInstructionId,
Place,
@@ -323,6 +324,11 @@ class Context {
return false;
}
// ref value is not a valid dep
if (isRefValueType(maybeDependency.identifier)) {
return false;
}
const identifier = maybeDependency.identifier;
// If this operand is used in a scope, has a dynamic value, and was defined
// before this scope, then its a dependency of the scope.
@@ -0,0 +1,48 @@
## Input
```javascript
function VideoTab() {
const ref = useRef();
const t = ref.current;
let x = () => {
t;
};
return <VideoList videos={x} />;
}
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
function VideoTab() {
const $ = useMemoCache(3);
const ref = useRef();
const t = ref.current;
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = () => {
t;
};
$[0] = t0;
} else {
t0 = $[0];
}
const x = t0;
const c_1 = $[1] !== x;
let t1;
if (c_1) {
t1 = <VideoList videos={x} />;
$[1] = x;
$[2] = t1;
} else {
t1 = $[2];
}
return t1;
}
```
@@ -0,0 +1,9 @@
function VideoTab() {
const ref = useRef();
const t = ref.current;
let x = () => {
t;
};
return <VideoList videos={x} />;
}
@@ -0,0 +1,47 @@
## Input
```javascript
function Foo({ a }) {
const ref = useRef();
const val = ref.current;
const x = { a, val };
return <VideoList videos={x} />;
}
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
function Foo(t21) {
const $ = useMemoCache(4);
const { a } = t21;
const ref = useRef();
const val = ref.current;
const c_0 = $[0] !== a;
let t0;
if (c_0) {
t0 = { a, val };
$[0] = a;
$[1] = t0;
} else {
t0 = $[1];
}
const x = t0;
const c_2 = $[2] !== x;
let t1;
if (c_2) {
t1 = <VideoList videos={x} />;
$[2] = x;
$[3] = t1;
} else {
t1 = $[3];
}
return t1;
}
```
@@ -0,0 +1,7 @@
function Foo({ a }) {
const ref = useRef();
const val = ref.current;
const x = { a, val };
return <VideoList videos={x} />;
}