From 48a3741d5a4b6275792d655b33554c583dd60cbf Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Fri, 2 Aug 2019 12:38:25 +0200 Subject: [PATCH] refactor(ivy): remove global state access in elementCreate (#31959) PR Close #31959 --- packages/core/src/render3/component_ref.ts | 2 +- .../core/src/render3/instructions/element.ts | 4 ++-- .../core/src/render3/instructions/shared.ts | 23 +++++++------------ 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/packages/core/src/render3/component_ref.ts b/packages/core/src/render3/component_ref.ts index 2f0bebd813..20be059c13 100644 --- a/packages/core/src/render3/component_ref.ts +++ b/packages/core/src/render3/component_ref.ts @@ -138,7 +138,7 @@ export class ComponentFactory extends viewEngine_ComponentFactory { namespaceHTMLInternal(); const hostRNode = rootSelectorOrNode ? locateHostElement(rendererFactory, rootSelectorOrNode) : - elementCreate(this.selector, rendererFactory.createRenderer(null, this.componentDef)); + elementCreate(this.selector, rendererFactory.createRenderer(null, this.componentDef), null); const rootFlags = this.componentDef.onPush ? LViewFlags.Dirty | LViewFlags.IsRoot : LViewFlags.CheckAlways | LViewFlags.IsRoot; diff --git a/packages/core/src/render3/instructions/element.ts b/packages/core/src/render3/instructions/element.ts index e9197f14e8..3b6eb3e411 100644 --- a/packages/core/src/render3/instructions/element.ts +++ b/packages/core/src/render3/instructions/element.ts @@ -16,7 +16,7 @@ import {isContentQueryHost} from '../interfaces/type_checks'; import {BINDING_INDEX, HEADER_OFFSET, LView, RENDERER, TVIEW, T_HOST} from '../interfaces/view'; import {assertNodeType} from '../node_assert'; import {appendChild} from '../node_manipulation'; -import {decreaseElementDepthCount, getElementDepthCount, getIsParent, getLView, getPreviousOrParentTNode, getSelectedIndex, increaseElementDepthCount, setIsNotParent, setPreviousOrParentTNode} from '../state'; +import {decreaseElementDepthCount, getElementDepthCount, getIsParent, getLView, getNamespace, getPreviousOrParentTNode, getSelectedIndex, increaseElementDepthCount, setIsNotParent, setPreviousOrParentTNode} from '../state'; import {registerInitialStylingOnTNode} from '../styling_next/instructions'; import {StylingMapArray, TStylingContext} from '../styling_next/interfaces'; import {getInitialStylingValue, hasClassInput, hasStyleInput} from '../styling_next/util'; @@ -52,8 +52,8 @@ export function ɵɵelementStart( ngDevMode && ngDevMode.rendererCreateElement++; ngDevMode && assertDataInRange(lView, index + HEADER_OFFSET); - const native = lView[index + HEADER_OFFSET] = elementCreate(name); const renderer = lView[RENDERER]; + const native = lView[index + HEADER_OFFSET] = elementCreate(name, renderer, getNamespace()); const tNode = getOrCreateTNode(tView, lView[T_HOST], index, TNodeType.Element, name, attrs || null); diff --git a/packages/core/src/render3/instructions/shared.ts b/packages/core/src/render3/instructions/shared.ts index 27245b4bad..f22dfd9d2c 100644 --- a/packages/core/src/render3/instructions/shared.ts +++ b/packages/core/src/render3/instructions/shared.ts @@ -197,26 +197,19 @@ function refreshChildComponents(hostLView: LView, components: number[]): void { /** * Creates a native element from a tag name, using a renderer. * @param name the tag name - * @param overriddenRenderer Optional A renderer to override the default one + * @param renderer A renderer to use * @returns the element created */ -export function elementCreate(name: string, overriddenRenderer?: Renderer3): RElement { - let native: RElement; - const rendererToUse = overriddenRenderer || getLView()[RENDERER]; - - const namespace = getNamespace(); - - if (isProceduralRenderer(rendererToUse)) { - native = rendererToUse.createElement(name, namespace); +export function elementCreate( + name: string, renderer: Renderer3, namespace: string | null): RElement { + if (isProceduralRenderer(renderer)) { + return renderer.createElement(name, namespace); } else { - if (namespace === null) { - native = rendererToUse.createElement(name); - } else { - native = rendererToUse.createElementNS(namespace, name); - } + return namespace === null ? renderer.createElement(name) : + renderer.createElementNS(namespace, name); } - return native; } + export function createLView( parentLView: LView | null, tView: TView, context: T | null, flags: LViewFlags, host: RElement | null, tHostNode: TViewNode | TElementNode | null,