mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Treat function expression deps as conditional
This commit is contained in:
+27
-1
@@ -63,7 +63,11 @@ export function propagateScopeDependencies(fn: ReactiveFunction): void {
|
||||
});
|
||||
}
|
||||
}
|
||||
visitReactiveFunction(fn, new PropagationVisitor(), context);
|
||||
visitReactiveFunction(
|
||||
fn,
|
||||
new PropagationVisitor(fn.env.config.enableTreatFunctionDepsAsConditional),
|
||||
context
|
||||
);
|
||||
}
|
||||
|
||||
type TemporariesUsedOutsideDefiningScope = {
|
||||
@@ -466,6 +470,14 @@ class Context {
|
||||
}
|
||||
|
||||
class PropagationVisitor extends ReactiveFunctionVisitor<Context> {
|
||||
enableTreatFunctionDepsAsConditional = false;
|
||||
|
||||
constructor(enableTreatFunctionDepsAsConditional: boolean) {
|
||||
super();
|
||||
this.enableTreatFunctionDepsAsConditional =
|
||||
enableTreatFunctionDepsAsConditional;
|
||||
}
|
||||
|
||||
override visitScope(scope: ReactiveScopeBlock, context: Context): void {
|
||||
const scopeDependencies = context.enter(scope.scope, () => {
|
||||
this.visitBlock(scope.instructions, context);
|
||||
@@ -547,6 +559,20 @@ class PropagationVisitor extends ReactiveFunctionVisitor<Context> {
|
||||
this.visitInstructionValue(context, id, value.value, null);
|
||||
break;
|
||||
}
|
||||
case "FunctionExpression": {
|
||||
if (this.enableTreatFunctionDepsAsConditional) {
|
||||
context.enterConditional(() => {
|
||||
for (const operand of eachInstructionValueOperand(value)) {
|
||||
context.visitOperand(operand);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
for (const operand of eachInstructionValueOperand(value)) {
|
||||
context.visitOperand(operand);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
for (const operand of eachInstructionValueOperand(value)) {
|
||||
context.visitOperand(operand);
|
||||
|
||||
+7
-6
@@ -2,6 +2,7 @@
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
// @enableTreatFunctionDepsAsConditional
|
||||
function Component(props) {
|
||||
function getLength() {
|
||||
return props.bar.length;
|
||||
@@ -12,7 +13,7 @@ function Component(props) {
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ bar: [] }],
|
||||
params: [{ bar: null }],
|
||||
};
|
||||
|
||||
```
|
||||
@@ -20,15 +21,15 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
import { unstable_useMemoCache as useMemoCache } from "react"; // @enableTreatFunctionDepsAsConditional
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(5);
|
||||
let t0;
|
||||
if ($[0] !== props.bar.length) {
|
||||
if ($[0] !== props) {
|
||||
t0 = function getLength() {
|
||||
return props.bar.length;
|
||||
};
|
||||
$[0] = props.bar.length;
|
||||
$[0] = props;
|
||||
$[1] = t0;
|
||||
} else {
|
||||
t0 = $[1];
|
||||
@@ -48,10 +49,10 @@ function Component(props) {
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ bar: [] }],
|
||||
params: [{ bar: null }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
### Eval output
|
||||
(kind: ok) 0
|
||||
(kind: ok) null
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
// @enableTreatFunctionDepsAsConditional
|
||||
function Component(props) {
|
||||
function getLength() {
|
||||
return props.bar.length;
|
||||
@@ -8,5 +9,5 @@ function Component(props) {
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ bar: [] }],
|
||||
params: [{ bar: null }],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user