refactor(core): unify setPreviousOrParentTNode and setIsParent into single setPreviousOrParentTNode (#30453)

PR Close #30453
This commit is contained in:
Misko Hevery 2019-05-14 21:47:11 -07:00 committed by Jason Aden
parent e122b44269
commit 2f336f15be
12 changed files with 48 additions and 57 deletions

View File

@ -26,7 +26,7 @@ import {SanitizerFn} from './interfaces/sanitization';
import {StylingContext} from './interfaces/styling'; import {StylingContext} from './interfaces/styling';
import {BINDING_INDEX, HEADER_OFFSET, LView, RENDERER, TVIEW, TView, T_HOST} from './interfaces/view'; import {BINDING_INDEX, HEADER_OFFSET, LView, RENDERER, TVIEW, TView, T_HOST} from './interfaces/view';
import {appendChild, createTextNode, nativeRemoveNode} from './node_manipulation'; import {appendChild, createTextNode, nativeRemoveNode} from './node_manipulation';
import {getIsParent, getLView, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from './state'; import {getIsParent, getLView, getPreviousOrParentTNode, setIsNotParent, setPreviousOrParentTNode} from './state';
import {NO_CHANGE} from './tokens'; import {NO_CHANGE} from './tokens';
import {renderStringify} from './util/misc_utils'; import {renderStringify} from './util/misc_utils';
import {getNativeByIndex, getNativeByTNode, getTNode, isLContainer} from './util/view_utils'; import {getNativeByIndex, getNativeByTNode, getTNode, isLContainer} from './util/view_utils';
@ -701,7 +701,7 @@ function readCreateOpCodes(
previousTNode = currentTNode; previousTNode = currentTNode;
currentTNode = createDynamicNodeAtIndex(textNodeIndex, TNodeType.Element, textRNode, null); currentTNode = createDynamicNodeAtIndex(textNodeIndex, TNodeType.Element, textRNode, null);
visitedNodes.push(textNodeIndex); visitedNodes.push(textNodeIndex);
setIsParent(false); setIsNotParent();
} else if (typeof opCode == 'number') { } else if (typeof opCode == 'number') {
switch (opCode & I18nMutateOpCode.MASK_OPCODE) { switch (opCode & I18nMutateOpCode.MASK_OPCODE) {
case I18nMutateOpCode.AppendChild: case I18nMutateOpCode.AppendChild:
@ -726,17 +726,13 @@ function readCreateOpCodes(
previousTNode = currentTNode; previousTNode = currentTNode;
currentTNode = getTNode(nodeIndex, viewData); currentTNode = getTNode(nodeIndex, viewData);
if (currentTNode) { if (currentTNode) {
setPreviousOrParentTNode(currentTNode); setPreviousOrParentTNode(currentTNode, currentTNode.type === TNodeType.Element);
if (currentTNode.type === TNodeType.Element) {
setIsParent(true);
}
} }
break; break;
case I18nMutateOpCode.ElementEnd: case I18nMutateOpCode.ElementEnd:
const elementIndex = opCode >>> I18nMutateOpCode.SHIFT_REF; const elementIndex = opCode >>> I18nMutateOpCode.SHIFT_REF;
previousTNode = currentTNode = getTNode(elementIndex, viewData); previousTNode = currentTNode = getTNode(elementIndex, viewData);
setPreviousOrParentTNode(currentTNode); setPreviousOrParentTNode(currentTNode, false);
setIsParent(false);
break; break;
case I18nMutateOpCode.Attr: case I18nMutateOpCode.Attr:
const elementNodeIndex = opCode >>> I18nMutateOpCode.SHIFT_REF; const elementNodeIndex = opCode >>> I18nMutateOpCode.SHIFT_REF;
@ -764,7 +760,7 @@ function readCreateOpCodes(
attachPatchData(commentRNode, viewData); attachPatchData(commentRNode, viewData);
(currentTNode as TIcuContainerNode).activeCaseIndex = null; (currentTNode as TIcuContainerNode).activeCaseIndex = null;
// We will add the case nodes later, during the update phase // We will add the case nodes later, during the update phase
setIsParent(false); setIsNotParent();
break; break;
case ELEMENT_MARKER: case ELEMENT_MARKER:
const tagNameValue = createOpCodes[++i] as string; const tagNameValue = createOpCodes[++i] as string;
@ -785,7 +781,7 @@ function readCreateOpCodes(
} }
} }
setIsParent(false); setIsNotParent();
return visitedNodes; return visitedNodes;
} }

View File

@ -15,7 +15,7 @@ import {LocalRefExtractor, TAttributes, TContainerNode, TNode, TNodeType} from '
import {BINDING_INDEX, HEADER_OFFSET, LView, QUERIES, RENDERER, TVIEW} from '../interfaces/view'; import {BINDING_INDEX, HEADER_OFFSET, LView, QUERIES, RENDERER, TVIEW} from '../interfaces/view';
import {assertNodeType} from '../node_assert'; import {assertNodeType} from '../node_assert';
import {appendChild, removeView} from '../node_manipulation'; import {appendChild, removeView} from '../node_manipulation';
import {getCheckNoChangesMode, getIsParent, getLView, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from '../state'; import {getCheckNoChangesMode, getIsParent, getLView, getPreviousOrParentTNode, setIsNotParent, setPreviousOrParentTNode} from '../state';
import {getNativeByTNode, loadInternal} from '../util/view_utils'; import {getNativeByTNode, loadInternal} from '../util/view_utils';
import {addToViewTree, createDirectivesAndLocals, createLContainer, createNodeAtIndex, createTView} from './shared'; import {addToViewTree, createDirectivesAndLocals, createLContainer, createNodeAtIndex, createTView} from './shared';
@ -37,7 +37,7 @@ export function ɵɵcontainer(index: number): void {
tNode.tViews = []; tNode.tViews = [];
} }
addTContainerToQueries(lView, tNode); addTContainerToQueries(lView, tNode);
setIsParent(false); setIsNotParent();
} }
/** /**
@ -77,7 +77,7 @@ export function ɵɵtemplate(
addTContainerToQueries(lView, tContainerNode); addTContainerToQueries(lView, tContainerNode);
attachPatchData(getNativeByTNode(tContainerNode, lView), lView); attachPatchData(getNativeByTNode(tContainerNode, lView), lView);
registerPostOrderHooks(tView, tContainerNode); registerPostOrderHooks(tView, tContainerNode);
setIsParent(false); setIsNotParent();
} }
/** /**
@ -91,10 +91,8 @@ export function ɵɵcontainerRefreshStart(index: number): void {
const lView = getLView(); const lView = getLView();
const tView = lView[TVIEW]; const tView = lView[TVIEW];
let previousOrParentTNode = loadInternal(tView.data, index) as TNode; let previousOrParentTNode = loadInternal(tView.data, index) as TNode;
setPreviousOrParentTNode(previousOrParentTNode);
ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.Container); ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.Container);
setIsParent(true); setPreviousOrParentTNode(previousOrParentTNode, true);
lView[index + HEADER_OFFSET][ACTIVE_INDEX] = 0; lView[index + HEADER_OFFSET][ACTIVE_INDEX] = 0;
@ -113,12 +111,12 @@ export function ɵɵcontainerRefreshStart(index: number): void {
export function ɵɵcontainerRefreshEnd(): void { export function ɵɵcontainerRefreshEnd(): void {
let previousOrParentTNode = getPreviousOrParentTNode(); let previousOrParentTNode = getPreviousOrParentTNode();
if (getIsParent()) { if (getIsParent()) {
setIsParent(false); setIsNotParent();
} else { } else {
ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.View); ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.View);
ngDevMode && assertHasParent(previousOrParentTNode); ngDevMode && assertHasParent(previousOrParentTNode);
previousOrParentTNode = previousOrParentTNode.parent !; previousOrParentTNode = previousOrParentTNode.parent !;
setPreviousOrParentTNode(previousOrParentTNode); setPreviousOrParentTNode(previousOrParentTNode, false);
} }
ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.Container); ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.Container);

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {validateAgainstEventAttributes} from '../../sanitization/sanitization'; import {validateAgainstEventAttributes} from '../../sanitization/sanitization';
import {assertDataInRange, assertEqual} from '../../util/assert'; import {assertDataInRange, assertDefined, assertEqual} from '../../util/assert';
import {assertHasParent} from '../assert'; import {assertHasParent} from '../assert';
import {attachPatchData} from '../context_discovery'; import {attachPatchData} from '../context_discovery';
import {registerPostOrderHooks} from '../hooks'; import {registerPostOrderHooks} from '../hooks';
@ -18,7 +18,7 @@ import {BINDING_INDEX, QUERIES, RENDERER, TVIEW} from '../interfaces/view';
import {assertNodeType} from '../node_assert'; import {assertNodeType} from '../node_assert';
import {appendChild} from '../node_manipulation'; import {appendChild} from '../node_manipulation';
import {applyOnCreateInstructions} from '../node_util'; import {applyOnCreateInstructions} from '../node_util';
import {decreaseElementDepthCount, getElementDepthCount, getIsParent, getLView, getPreviousOrParentTNode, getSelectedIndex, increaseElementDepthCount, setIsParent, setPreviousOrParentTNode} from '../state'; import {decreaseElementDepthCount, getElementDepthCount, getIsParent, getLView, getPreviousOrParentTNode, getSelectedIndex, increaseElementDepthCount, setIsNotParent, setPreviousOrParentTNode} from '../state';
import {getInitialClassNameValue, getInitialStyleStringValue, initializeStaticContext, patchContextWithStaticAttrs, renderInitialClasses, renderInitialStyles} from '../styling/class_and_style_bindings'; import {getInitialClassNameValue, getInitialStyleStringValue, initializeStaticContext, patchContextWithStaticAttrs, renderInitialClasses, renderInitialStyles} from '../styling/class_and_style_bindings';
import {getStylingContextFromLView, hasClassInput, hasStyleInput} from '../styling/util'; import {getStylingContextFromLView, hasClassInput, hasStyleInput} from '../styling/util';
import {registerInitialStylingIntoContext} from '../styling_next/instructions'; import {registerInitialStylingIntoContext} from '../styling_next/instructions';
@ -139,12 +139,13 @@ export function ɵɵelementStart(
*/ */
export function ɵɵelementEnd(): void { export function ɵɵelementEnd(): void {
let previousOrParentTNode = getPreviousOrParentTNode(); let previousOrParentTNode = getPreviousOrParentTNode();
ngDevMode && assertDefined(previousOrParentTNode, 'No parent node to close.');
if (getIsParent()) { if (getIsParent()) {
setIsParent(false); setIsNotParent();
} else { } else {
ngDevMode && assertHasParent(getPreviousOrParentTNode()); ngDevMode && assertHasParent(getPreviousOrParentTNode());
previousOrParentTNode = previousOrParentTNode.parent !; previousOrParentTNode = previousOrParentTNode.parent !;
setPreviousOrParentTNode(previousOrParentTNode); setPreviousOrParentTNode(previousOrParentTNode, false);
} }
// this is required for all host-level styling-related instructions to run // this is required for all host-level styling-related instructions to run

View File

@ -14,7 +14,8 @@ import {BINDING_INDEX, QUERIES, RENDERER, TVIEW} from '../interfaces/view';
import {assertNodeType} from '../node_assert'; import {assertNodeType} from '../node_assert';
import {appendChild} from '../node_manipulation'; import {appendChild} from '../node_manipulation';
import {applyOnCreateInstructions} from '../node_util'; import {applyOnCreateInstructions} from '../node_util';
import {getIsParent, getLView, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from '../state'; import {getIsParent, getLView, getPreviousOrParentTNode, setIsNotParent, setPreviousOrParentTNode} from '../state';
import {createDirectivesAndLocals, createNodeAtIndex, executeContentQueries, setNodeStylingTemplate} from './shared'; import {createDirectivesAndLocals, createNodeAtIndex, executeContentQueries, setNodeStylingTemplate} from './shared';
/** /**
@ -77,11 +78,11 @@ export function ɵɵelementContainerEnd(): void {
const lView = getLView(); const lView = getLView();
const tView = lView[TVIEW]; const tView = lView[TVIEW];
if (getIsParent()) { if (getIsParent()) {
setIsParent(false); setIsNotParent();
} else { } else {
ngDevMode && assertHasParent(previousOrParentTNode); ngDevMode && assertHasParent(previousOrParentTNode);
previousOrParentTNode = previousOrParentTNode.parent !; previousOrParentTNode = previousOrParentTNode.parent !;
setPreviousOrParentTNode(previousOrParentTNode); setPreviousOrParentTNode(previousOrParentTNode, false);
} }
ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.ElementContainer); ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.ElementContainer);

View File

@ -40,7 +40,7 @@ export function ɵɵembeddedViewStart(
let viewToRender = scanForView(lContainer, lContainer[ACTIVE_INDEX] !, viewBlockId); let viewToRender = scanForView(lContainer, lContainer[ACTIVE_INDEX] !, viewBlockId);
if (viewToRender) { if (viewToRender) {
setIsParent(true); setIsParent();
enterView(viewToRender, viewToRender[TVIEW].node); enterView(viewToRender, viewToRender[TVIEW].node);
} else { } else {
// When we create a new LView, we always reset the state of the instructions. // When we create a new LView, we always reset the state of the instructions.
@ -141,6 +141,5 @@ export function ɵɵembeddedViewEnd(): void {
const lContainer = lView[PARENT] as LContainer; const lContainer = lView[PARENT] as LContainer;
ngDevMode && assertLContainerOrUndefined(lContainer); ngDevMode && assertLContainerOrUndefined(lContainer);
leaveView(lContainer[PARENT] !); leaveView(lContainer[PARENT] !);
setPreviousOrParentTNode(viewHost !); setPreviousOrParentTNode(viewHost !, false);
setIsParent(false);
} }

View File

@ -10,7 +10,7 @@ import {CssSelectorList} from '../interfaces/projection';
import {T_HOST} from '../interfaces/view'; import {T_HOST} from '../interfaces/view';
import {appendProjectedNodes} from '../node_manipulation'; import {appendProjectedNodes} from '../node_manipulation';
import {matchingProjectionSelectorIndex} from '../node_selector_matcher'; import {matchingProjectionSelectorIndex} from '../node_selector_matcher';
import {getLView, setIsParent} from '../state'; import {getLView, setIsNotParent} from '../state';
import {findComponentView} from '../util/view_traversal_utils'; import {findComponentView} from '../util/view_traversal_utils';
import {createNodeAtIndex} from './shared'; import {createNodeAtIndex} from './shared';
@ -88,7 +88,7 @@ export function ɵɵprojection(
if (tProjectionNode.projection === null) tProjectionNode.projection = selectorIndex; if (tProjectionNode.projection === null) tProjectionNode.projection = selectorIndex;
// `<ng-content>` has no content // `<ng-content>` has no content
setIsParent(false); setIsNotParent();
// re-distribution of projectable nodes is stored on a component's view level // re-distribution of projectable nodes is stored on a component's view level
appendProjectedNodes(lView, tProjectionNode, selectorIndex, findComponentView(lView)); appendProjectedNodes(lView, tProjectionNode, selectorIndex, findComponentView(lView));

View File

@ -287,8 +287,7 @@ export function createNodeAtIndex(
} }
} }
setPreviousOrParentTNode(tNode); setPreviousOrParentTNode(tNode, true);
setIsParent(true);
return tNode as TElementNode & TViewNode & TContainerNode & TElementContainerNode & return tNode as TElementNode & TViewNode & TContainerNode & TElementContainerNode &
TProjectionNode & TIcuContainerNode; TProjectionNode & TIcuContainerNode;
} }
@ -351,8 +350,7 @@ export function createEmbeddedViewAndNode<T>(
injectorIndex: number): LView { injectorIndex: number): LView {
const _isParent = getIsParent(); const _isParent = getIsParent();
const _previousOrParentTNode = getPreviousOrParentTNode(); const _previousOrParentTNode = getPreviousOrParentTNode();
setIsParent(true); setPreviousOrParentTNode(null !, true);
setPreviousOrParentTNode(null !);
const lView = createLView(declarationView, tView, context, LViewFlags.CheckAlways, null, null); const lView = createLView(declarationView, tView, context, LViewFlags.CheckAlways, null, null);
lView[DECLARATION_VIEW] = declarationView; lView[DECLARATION_VIEW] = declarationView;
@ -366,8 +364,7 @@ export function createEmbeddedViewAndNode<T>(
tView.node !.injectorIndex = injectorIndex; tView.node !.injectorIndex = injectorIndex;
} }
setIsParent(_isParent); setPreviousOrParentTNode(_previousOrParentTNode, _isParent);
setPreviousOrParentTNode(_previousOrParentTNode);
return lView; return lView;
} }
@ -390,8 +387,7 @@ export function renderEmbeddedTemplate<T>(viewToRender: LView, tView: TView, con
tickRootContext(getRootContext(viewToRender)); tickRootContext(getRootContext(viewToRender));
} else { } else {
try { try {
setIsParent(true); setPreviousOrParentTNode(null !, true);
setPreviousOrParentTNode(null !);
oldView = enterView(viewToRender, viewToRender[T_HOST]); oldView = enterView(viewToRender, viewToRender[T_HOST]);
resetPreOrderHookFlags(viewToRender); resetPreOrderHookFlags(viewToRender);
@ -406,8 +402,7 @@ export function renderEmbeddedTemplate<T>(viewToRender: LView, tView: TView, con
refreshDescendantViews(viewToRender); refreshDescendantViews(viewToRender);
} finally { } finally {
leaveView(oldView !); leaveView(oldView !);
setIsParent(_isParent); setPreviousOrParentTNode(_previousOrParentTNode, _isParent);
setPreviousOrParentTNode(_previousOrParentTNode);
} }
} }
} }
@ -1254,7 +1249,7 @@ function addComponentLogic<T>(
lView, createLView( lView, createLView(
lView, tView, null, def.onPush ? LViewFlags.Dirty : LViewFlags.CheckAlways, lView, tView, null, def.onPush ? LViewFlags.Dirty : LViewFlags.CheckAlways,
lView[previousOrParentTNode.index], previousOrParentTNode as TElementNode, lView[previousOrParentTNode.index], previousOrParentTNode as TElementNode,
rendererFactory, lView[RENDERER_FACTORY].createRenderer(native as RElement, def))); rendererFactory, rendererFactory.createRenderer(native as RElement, def)));
componentView[T_HOST] = previousOrParentTNode as TElementNode; componentView[T_HOST] = previousOrParentTNode as TElementNode;
@ -1695,18 +1690,15 @@ export function storeBindingMetadata(lView: LView, prefix = '', suffix = ''): st
export const CLEAN_PROMISE = _CLEAN_PROMISE; export const CLEAN_PROMISE = _CLEAN_PROMISE;
export function initializeTNodeInputs(tNode: TNode | null): PropertyAliases|null { export function initializeTNodeInputs(tNode: TNode): PropertyAliases|null {
// If tNode.inputs is undefined, a listener has created outputs, but inputs haven't // If tNode.inputs is undefined, a listener has created outputs, but inputs haven't
// yet been checked. // yet been checked.
if (tNode) {
if (tNode.inputs === undefined) { if (tNode.inputs === undefined) {
// mark inputs as checked // mark inputs as checked
tNode.inputs = generatePropertyAliases(tNode, BindingDirection.Input); tNode.inputs = generatePropertyAliases(tNode, BindingDirection.Input);
} }
return tNode.inputs; return tNode.inputs;
} }
return null;
}
export function getCleanup(view: LView): any[] { export function getCleanup(view: LView): any[] {

View File

@ -10,7 +10,7 @@ import {TNodeType} from '../interfaces/node';
import {RText, isProceduralRenderer} from '../interfaces/renderer'; import {RText, isProceduralRenderer} from '../interfaces/renderer';
import {BINDING_INDEX, HEADER_OFFSET, RENDERER, TVIEW} from '../interfaces/view'; import {BINDING_INDEX, HEADER_OFFSET, RENDERER, TVIEW} from '../interfaces/view';
import {appendChild, createTextNode} from '../node_manipulation'; import {appendChild, createTextNode} from '../node_manipulation';
import {getLView, setIsParent} from '../state'; import {getLView, setIsNotParent} from '../state';
import {NO_CHANGE} from '../tokens'; import {NO_CHANGE} from '../tokens';
import {renderStringify} from '../util/misc_utils'; import {renderStringify} from '../util/misc_utils';
import {getNativeByIndex} from '../util/view_utils'; import {getNativeByIndex} from '../util/view_utils';
@ -34,7 +34,7 @@ export function ɵɵtext(index: number, value?: any): void {
const tNode = createNodeAtIndex(index, TNodeType.Element, textNative, null, null); const tNode = createNodeAtIndex(index, TNodeType.Element, textNative, null, null);
// Text nodes are self closing. // Text nodes are self closing.
setIsParent(false); setIsNotParent();
appendChild(textNative, tNode, lView); appendChild(textNative, tNode, lView);
} }

View File

@ -303,8 +303,9 @@ export function getPreviousOrParentTNode(): TNode {
return previousOrParentTNode; return previousOrParentTNode;
} }
export function setPreviousOrParentTNode(tNode: TNode) { export function setPreviousOrParentTNode(tNode: TNode, _isParent: boolean) {
previousOrParentTNode = tNode; previousOrParentTNode = tNode;
isParent = _isParent;
} }
export function setTNodeAndViewData(tNode: TNode, view: LView) { export function setTNodeAndViewData(tNode: TNode, view: LView) {
@ -325,8 +326,11 @@ export function getIsParent(): boolean {
return isParent; return isParent;
} }
export function setIsParent(value: boolean): void { export function setIsNotParent(): void {
isParent = value; isParent = false;
}
export function setIsParent(): void {
isParent = true;
} }

View File

@ -693,7 +693,7 @@
"name": "setInputsFromAttrs" "name": "setInputsFromAttrs"
}, },
{ {
"name": "setIsParent" "name": "setIsNotParent"
}, },
{ {
"name": "setNodeStylingTemplate" "name": "setNodeStylingTemplate"

View File

@ -468,7 +468,7 @@
"name": "setInjectImplementation" "name": "setInjectImplementation"
}, },
{ {
"name": "setIsParent" "name": "setIsNotParent"
}, },
{ {
"name": "setPreviousOrParentTNode" "name": "setPreviousOrParentTNode"

View File

@ -1386,7 +1386,7 @@
"name": "setInputsFromAttrs" "name": "setInputsFromAttrs"
}, },
{ {
"name": "setIsParent" "name": "setIsNotParent"
}, },
{ {
"name": "setNodeStylingTemplate" "name": "setNodeStylingTemplate"