From 103064a3d03bf54e3d39aa3779aafd3eb38aef44 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Mon, 24 Jun 2019 17:21:02 +0200 Subject: [PATCH] refactor(ivy): remove code duplication from applyXYZ functions (#31182) PR Close #31182 --- .../core/src/render3/node_manipulation.ts | 66 +++++++------------ 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/packages/core/src/render3/node_manipulation.ts b/packages/core/src/render3/node_manipulation.ts index 096ebecdff..b05c7e79c2 100644 --- a/packages/core/src/render3/node_manipulation.ts +++ b/packages/core/src/render3/node_manipulation.ts @@ -764,18 +764,7 @@ function applyView( ngDevMode && assertNodeType(tView.node !, TNodeType.View); let viewRootTNode: TNode|null = tView.node !.child; while (viewRootTNode !== null) { - const viewRootTNodeType = viewRootTNode.type; - if (viewRootTNodeType === TNodeType.ElementContainer) { - applyElementContainer( - renderer, action, lView, viewRootTNode as TElementContainerNode, renderParent, - beforeNode); - } else if (viewRootTNodeType === TNodeType.Projection) { - applyProjection( - renderer, action, lView, viewRootTNode as TProjectionNode, renderParent, beforeNode); - } else { - ngDevMode && assertNodeOfPossibleTypes(viewRootTNode, TNodeType.Element, TNodeType.Container); - executeNodeAction(action, renderer, renderParent, lView[viewRootTNode.index], beforeNode); - } + executeActionOnTNode(renderer, action, lView, viewRootTNode, renderParent, beforeNode); viewRootTNode = viewRootTNode.next; } } @@ -810,20 +799,8 @@ function applyProjection( let projectionTNode: TNode|null = nodeToProject; const projectedComponentLView = componentLView[PARENT] as LView; while (projectionTNode !== null) { - if (projectionTNode.type === TNodeType.ElementContainer) { - applyElementContainer( - renderer, action, projectedComponentLView, projectionTNode as TElementContainerNode, - renderParent, beforeNode); - } else if (projectionTNode.type === TNodeType.Projection) { - applyProjection( - renderer, action, projectedComponentLView, projectionTNode as TProjectionNode, - renderParent, beforeNode); - } else { - const rNode = projectedComponentLView[projectionTNode.index]; - ngDevMode && - assertNodeOfPossibleTypes(projectionTNode, TNodeType.Element, TNodeType.Container); - executeNodeAction(action, renderer, renderParent, rNode, beforeNode); - } + executeActionOnTNode( + renderer, action, projectedComponentLView, projectionTNode, renderParent, beforeNode); projectionTNode = projectionTNode.projectionNext; } } @@ -882,23 +859,24 @@ function applyElementContainer( beforeNode: RNode | null | undefined) { const node = lView[tElementContainerNode.index]; executeNodeAction(action, renderer, renderParent, node, beforeNode); - let elementContainerRootTNode: TNode|null = tElementContainerNode.child; - while (elementContainerRootTNode) { - const elementContainerRootTNodeType = elementContainerRootTNode.type; - if (elementContainerRootTNodeType === TNodeType.ElementContainer) { - applyElementContainer( - renderer, action, lView, elementContainerRootTNode as TElementContainerNode, renderParent, - beforeNode); - } else if (elementContainerRootTNodeType === TNodeType.Projection) { - applyProjection( - renderer, action, lView, elementContainerRootTNode as TProjectionNode, renderParent, - beforeNode); - } else { - ngDevMode && assertNodeOfPossibleTypes( - elementContainerRootTNode, TNodeType.Element, TNodeType.Container); - executeNodeAction( - action, renderer, renderParent, lView[elementContainerRootTNode.index], beforeNode); - } - elementContainerRootTNode = elementContainerRootTNode.next; + let childTNode: TNode|null = tElementContainerNode.child; + while (childTNode) { + executeActionOnTNode(renderer, action, lView, childTNode, renderParent, beforeNode); + childTNode = childTNode.next; + } +} + +function executeActionOnTNode( + renderer: Renderer3, action: WalkTNodeTreeAction, lView: LView, tNode: TNode, + renderParent: RElement | null, beforeNode: RNode | null | undefined): void { + const elementContainerRootTNodeType = tNode.type; + if (elementContainerRootTNodeType === TNodeType.ElementContainer) { + applyElementContainer( + renderer, action, lView, tNode as TElementContainerNode, renderParent, beforeNode); + } else if (elementContainerRootTNodeType === TNodeType.Projection) { + applyProjection(renderer, action, lView, tNode as TProjectionNode, renderParent, beforeNode); + } else { + ngDevMode && assertNodeOfPossibleTypes(tNode, TNodeType.Element, TNodeType.Container); + executeNodeAction(action, renderer, renderParent, lView[tNode.index], beforeNode); } } \ No newline at end of file