From 577a45e7104866b9b6719004b5b6d21376b995b0 Mon Sep 17 00:00:00 2001 From: laibo102 Date: Thu, 11 Aug 2022 19:40:15 +0800 Subject: [PATCH] Update: GridItem transform and its UT Signed-off-by: laibo102 Change-Id: I9c036706b2465af4383854a5286a0d19251dbc6e --- compiler/src/pre_define.ts | 3 + compiler/src/process_component_build.ts | 56 +++++++++++++------ .../render_component/item/GridItem.ts | 19 +++++-- .../lazyforeach/lazyforeach.ts | 2 +- 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/compiler/src/pre_define.ts b/compiler/src/pre_define.ts index 556e37a..ae2d3c6 100644 --- a/compiler/src/pre_define.ts +++ b/compiler/src/pre_define.ts @@ -313,3 +313,6 @@ export const FOREACHUPDATEFUNCTION: string = 'forEachUpdateFunction'; export const ALLOCATENEWELMETIDFORNEXTCOMPONENT: string = 'AllocateNewElmetIdForNextComponent'; export const STATE_OBJECTLINK_DECORATORS: string[] = [COMPONENT_STATE_DECORATOR, COMPONENT_OBJECT_LINK_DECORATOR]; export const COMPONENT_INITIAl_RENDER_FUNCTION: string = 'initialRender'; +export const GRID_COMPONENT: string = 'Grid'; +export const GRIDITEM_COMPONENT: string = 'GridItem'; +export const WILLUSEPROXY: string = 'willUseProxy'; diff --git a/compiler/src/process_component_build.ts b/compiler/src/process_component_build.ts index 91889e1..2d7ded8 100644 --- a/compiler/src/process_component_build.ts +++ b/compiler/src/process_component_build.ts @@ -80,7 +80,10 @@ import { FOREACHITEMIDFUNC, __LAZYFOREACHITEMIDFUNC, FOREACHUPDATEFUNCTION, - COMPONENT_INITIAl_RENDER_FUNCTION + COMPONENT_INITIAl_RENDER_FUNCTION, + GRIDITEM_COMPONENT, + GRID_COMPONENT, + WILLUSEPROXY } from './pre_define'; import { INNER_COMPONENT_NAMES, @@ -582,23 +585,9 @@ function createItemBlock( itemRenderInnerStatements: ts.Statement[], deepItemRenderInnerStatements: ts.Statement[] ): ts.Block { - const etsComponent: ts.EtsComponentExpression = getEtsComponentExpression(node); - let isLazyCreate: ts.BooleanLiteral = ts.factory.createTrue(); - if (etsComponent && etsComponent.arguments[0]) { - if (etsComponent.arguments[0] as ts.StringLiteral.text === 'false') { - isLazyCreate = ts.factory.createFalse(); - } - } return ts.factory.createBlock( [ - ts.factory.createVariableStatement( - undefined, - ts.factory.createVariableDeclarationList( - [ts.factory.createVariableDeclaration(ts.factory.createIdentifier(ISLAZYCREATE), - undefined, undefined, isLazyCreate)], - ts.NodeFlags.Const - ) - ), + createIsLazyCreate(node), createItemCreation(node, itemRenderInnerStatements), createObservedShallowRender(node, itemRenderInnerStatements), createObservedDeepRender(node, deepItemRenderInnerStatements), @@ -617,6 +606,39 @@ function createItemBlock( ); } +function createIsLazyCreate(node: ts.ExpressionStatement): ts.VariableStatement { + const etsComponent: ts.EtsComponentExpression = getEtsComponentExpression(node); + if (etsComponent) { + if ((etsComponent.expression as ts.Identifier).escapedText.toString() === GRIDITEM_COMPONENT) { + if (etsComponent.arguments[0] && (etsComponent.arguments[0] as ts.StringLiteral).text === 'false') { + return ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList( + [ts.factory.createVariableDeclaration(ts.factory.createIdentifier(ISLAZYCREATE), + undefined, undefined, ts.factory.createFalse())], ts.NodeFlags.Const)); + } else { + return ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList( + [ts.factory.createVariableDeclaration(ts.factory.createIdentifier(ISLAZYCREATE), undefined, undefined, + ts.factory.createBinaryExpression( + ts.factory.createTrue(), ts.factory.createToken(ts.SyntaxKind.AmpersandAmpersandToken), + ts.factory.createParenthesizedExpression(ts.factory.createBinaryExpression( + ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression( + ts.factory.createIdentifier(GRID_COMPONENT), ts.factory.createIdentifier(WILLUSEPROXY) + ), undefined, []), ts.factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), + ts.factory.createTrue()))))], ts.NodeFlags.Const)); + } + } else { + if (etsComponent.arguments[0] && (etsComponent.arguments[0] as ts.StringLiteral).text === 'false') { + return ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList( + [ts.factory.createVariableDeclaration(ts.factory.createIdentifier(ISLAZYCREATE), + undefined, undefined, ts.factory.createFalse())], ts.NodeFlags.Const)); + } else { + return ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList( + [ts.factory.createVariableDeclaration(ts.factory.createIdentifier(ISLAZYCREATE), + undefined, undefined, ts.factory.createTrue())], ts.NodeFlags.Const)); + } + } + } +} + function createItemCreation( node: ts.ExpressionStatement, itemRenderInnerStatements: ts.Statement[] @@ -823,7 +845,7 @@ function processForEachComponent(node: ts.ExpressionStatement, newStatements: ts ts.factory.createIdentifier(FOREACH_GET_RAW_OBJECT)), undefined, [argumentsArray[0]]); } argumentsArray.splice(0, 1, arrayObserveredObject); - const newArrowNode: ts.ArrowFunction = + const newArrowNode: ts.ArrowFunction = processForEachBlock(node.expression, log, isInnerBuilder) as ts.ArrowFunction; if (newArrowNode) { argumentsArray.splice(1, 1, newArrowNode); diff --git a/compiler/test/utForPartialUpdate/inner_component_transform/render_component/item/GridItem.ts b/compiler/test/utForPartialUpdate/inner_component_transform/render_component/item/GridItem.ts index 8f7aedc..4ad66e0 100644 --- a/compiler/test/utForPartialUpdate/inner_component_transform/render_component/item/GridItem.ts +++ b/compiler/test/utForPartialUpdate/inner_component_transform/render_component/item/GridItem.ts @@ -18,9 +18,11 @@ exports.source = ` @Component struct ParentView { build() { - GridItem('true') { - Text('xx').width(100) - }.width(200).height(100) + Grid() { + GridItem('true') { + Text('xx').width(100) + }.width(200).height(100) + } } } ` @@ -45,8 +47,16 @@ exports.expectResult = this.aboutToBeDeletedInternal(); } initialRender() { + this.observeComponentCreation((elmtId, isInitialRender) => { + ViewStackProcessor.StartGetAccessRecordingFor(elmtId); + Grid.create(); + if (!isInitialRender) { + Grid.pop(); + } + ViewStackProcessor.StopGetAccessRecording(); + }); { - const isLazyCreate = true; + const isLazyCreate = true && (Grid.willUseProxy() === true); const itemCreation = (elmtId, isInitialRender) => { ViewStackProcessor.StartGetAccessRecordingFor(elmtId); GridItem.create(deepRenderFunction, isLazyCreate); @@ -104,6 +114,7 @@ exports.expectResult = observedDeepRender(); } } + Grid.pop(); } rerender() { this.updateDirtyElements(); diff --git a/compiler/test/utForPartialUpdate/inner_component_transform/render_component/lazyforeach/lazyforeach.ts b/compiler/test/utForPartialUpdate/inner_component_transform/render_component/lazyforeach/lazyforeach.ts index 40b64e6..6ad1094 100644 --- a/compiler/test/utForPartialUpdate/inner_component_transform/render_component/lazyforeach/lazyforeach.ts +++ b/compiler/test/utForPartialUpdate/inner_component_transform/render_component/lazyforeach/lazyforeach.ts @@ -208,7 +208,7 @@ class Test extends View { const __lazyForEachItemGenFunction = _item => { const row = _item; { - const isLazyCreate = true; + const isLazyCreate = true && (Grid.willUseProxy() === true); const itemCreation = (elmtId, isInitialRender) => { ViewStackProcessor.StartGetAccessRecordingFor(elmtId); GridItem.create(deepRenderFunction, isLazyCreate);