From e0c95e5f3ab3ecd1450c6f57319f1c6e2da8f325 Mon Sep 17 00:00:00 2001 From: GrinZero <774933704@qq.com> Date: Sat, 29 Jun 2024 01:40:13 +0800 Subject: [PATCH] feat: done --- .../ReactiveScopes/CodegenReactiveFunction.ts | 145 +++++++++--------- 1 file changed, 76 insertions(+), 69 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts index 0060c42aff769..387193655386f 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -176,66 +176,6 @@ export function codegenFunction( ), ]) ); - // preface.push( - // t.ifStatement( - // t.binaryExpression( - // "!==", - // t.memberExpression( - // t.identifier(cx.synthesizeName("$")), - // t.numericLiteral(fastRefreshState.cacheIndex), - // true - // ), - // t.stringLiteral(fastRefreshState.hash) - // ), - // t.blockStatement([ - // t.forStatement( - // t.variableDeclaration("let", [ - // t.variableDeclarator(t.identifier(index), t.numericLiteral(0)), - // ]), - // t.binaryExpression( - // "<", - // t.identifier(index), - // t.numericLiteral(cacheCount) - // ), - // t.assignmentExpression( - // "+=", - // t.identifier(index), - // t.numericLiteral(1) - // ), - // t.blockStatement([ - // t.expressionStatement( - // t.assignmentExpression( - // "=", - // t.memberExpression( - // t.identifier(cx.synthesizeName("$")), - // t.identifier(index), - // true - // ), - // t.callExpression( - // t.memberExpression( - // t.identifier("Symbol"), - // t.identifier("for") - // ), - // [t.stringLiteral(MEMO_CACHE_SENTINEL)] - // ) - // ) - // ), - // ]) - // ), - // t.expressionStatement( - // t.assignmentExpression( - // "=", - // t.memberExpression( - // t.identifier(cx.synthesizeName("$")), - // t.numericLiteral(fastRefreshState.cacheIndex), - // true - // ), - // t.stringLiteral(fastRefreshState.hash) - // ) - // ), - // ]) - // ) - // ); } compiled.body.body.unshift(...preface); } @@ -548,14 +488,25 @@ function codegenReactiveScope( if (count === 1) { const indices: t.NumericLiteral[] = []; const newValues: t.Expression[] = []; - const todo: (t.Expression | null | undefined)[] = []; + const updateIndexs: t.NumericLiteral[] = []; + const source: (t.Expression | null | undefined)[] = []; for (const dep of scope.dependencies) { const index = cx.nextCacheIndex; indices.push(t.numericLiteral(index)); newValues.push(codegenDependency(cx, dep)); - - todo.push(codegenDependency(cx, dep)); + const comparison = t.binaryExpression( + "!==", + t.memberExpression( + t.identifier(cx.synthesizeName("$")), + t.numericLiteral(index), + true + ), + codegenDependency(cx, dep) + ); + changeExpressions.push(comparison); + updateIndexs.push(t.numericLiteral(index)); + source.push(codegenDependency(cx, dep)); } const arr: t.Identifier[] = []; @@ -565,22 +516,64 @@ function codegenReactiveScope( arr.push(name); cacheLoads.push({ name, index, value: wrapCacheDep(cx, name) }); } + for (const reassignment of scope.reassignments) { const index = cx.nextCacheIndex; const name = convertIdentifier(reassignment); cacheLoads.push({ name, index, value: wrapCacheDep(cx, name) }); } - // qucik + + let testCondition = (changeExpressions as Array).reduce( + (acc: t.Expression | null, ident: t.Expression) => { + if (acc == null) { + return ident; + } + return t.logicalExpression("||", acc, ident); + }, + null as t.Expression | null + ); + + // quick let computationBlock = codegenBlock(cx, block); const [variableDeclaration] = computationBlock.body; + const bd: (t.Expression | t.SpreadElement | null | undefined)[] = []; if ("declarations" in variableDeclaration) { - todo.push(...variableDeclaration.declarations.map((item) => item.init)); + for (let i = 0; i < variableDeclaration.declarations.length; i++) { + const declaration = variableDeclaration.declarations[i]; + if (declaration.id.type === "ObjectPattern") { + const returnIdentifiers = declaration.id.properties.map((item) => { + if (item.type === "ObjectProperty") { + return item.value; + } + return item.argument; + }) as t.Identifier[]; + bd.push( + t.spreadElement( + t.callExpression( + t.arrowFunctionExpression( + [], + t.blockStatement([ + t.variableDeclaration("const", [ + t.variableDeclarator(declaration.id, declaration.init), + ]), + t.returnStatement(t.arrayExpression(returnIdentifiers)), + ]) + ), + [] + ) + ) + ); + continue; + } + bd.push(declaration.init); + } } const back: t.MemberExpression[] = []; for (const { index, value } of cacheLoads) { - todo.push(value); + source.push(value); + updateIndexs.push(t.numericLiteral(index)); back.push( t.memberExpression( t.identifier(cx.synthesizeName("$")), @@ -590,6 +583,7 @@ function codegenReactiveScope( ); } + const initName = arr.map((item) => item.name); const elements = t.variableDeclaration("const", [ t.variableDeclarator( t.arrayPattern(arr), @@ -597,10 +591,23 @@ function codegenReactiveScope( "||", t.callExpression(t.identifier("u"), [ t.identifier(cx.synthesizeName("$")), + t.arrowFunctionExpression( + [], + t.arrayExpression(bd as t.Expression[]) + ), t.arrayExpression(indices), - t.arrayExpression(newValues), - t.arrayExpression( - todo.slice(0, -1).filter(Boolean) as t.Expression[] + testCondition ? t.arrayExpression(newValues) : t.nullLiteral(), + t.arrayExpression(updateIndexs), + t.arrowFunctionExpression( + [], + t.arrayExpression( + source.filter((s) => { + if (s?.type === "Identifier") { + return !initName.includes(s.name); + } + return true; + }) as t.Expression[] + ) ), ]), t.arrayExpression(back)