Skip to content

Commit

Permalink
refactor(ivy): remove LNode (#26426)
Browse files Browse the repository at this point in the history
PR Close #26426
  • Loading branch information
kara authored and mhevery committed Oct 15, 2018
1 parent 9afc9a7 commit e76a570
Show file tree
Hide file tree
Showing 27 changed files with 302 additions and 449 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/render3/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import {getComponentDef} from './definition';
import {queueInitHooks, queueLifecycleHooks} from './hooks';
import {CLEAN_PROMISE, baseDirectiveCreate, createLViewData, createNodeAtIndex, createTView, detectChangesInternal, enterView, executeInitAndContentHooks, getOrCreateTView, leaveView, locateHostElement, prefillHostVars, resetComponentState, setHostBindings} from './instructions';
import {ComponentDef, ComponentType} from './interfaces/definition';
import {TNodeFlags, TNodeType} from './interfaces/node';
import {TElementNode, TNodeFlags, TNodeType} from './interfaces/node';
import {PlayerHandler} from './interfaces/player';
import {RElement, RNode, Renderer3, RendererFactory3, domRendererFactory3} from './interfaces/renderer';
import {CONTEXT, HEADER_OFFSET, HOST, INJECTOR, LViewData, LViewFlags, RootContext, RootContextFlags, TVIEW} from './interfaces/view';
import {CONTEXT, HEADER_OFFSET, HOST, HOST_NODE, INJECTOR, LViewData, LViewFlags, RootContext, RootContextFlags, TVIEW} from './interfaces/view';
import {getRootView, readElementValue, readPatchedLViewData, stringify} from './util';


Expand Down Expand Up @@ -166,7 +166,7 @@ export function createRootComponentView(
getOrCreateTView(
def.template, def.consts, def.vars, def.directiveDefs, def.pipeDefs, def.viewQuery),
null, def.onPush ? LViewFlags.Dirty : LViewFlags.CheckAlways, sanitizer);
const tNode = createNodeAtIndex(0, TNodeType.Element, rNode, null, null, componentView);
const tNode = createNodeAtIndex(0, TNodeType.Element, rNode, null, null);

if (tView.firstTemplatePass) {
tView.expandoInstructions = ROOT_EXPANDO_INSTRUCTIONS.slice();
Expand All @@ -177,10 +177,10 @@ export function createRootComponentView(

// Store component view at node index, with node as the HOST
componentView[HOST] = rootView[HEADER_OFFSET];
componentView[HOST_NODE] = tNode as TElementNode;
return rootView[HEADER_OFFSET] = componentView;
}


/**
* Creates a root component and sets it up with features and host bindings. Shared by
* renderComponent() and ViewContainerRef.createComponent().
Expand Down Expand Up @@ -255,7 +255,7 @@ function getRootContext(component: any): RootContext {
* @param component Component for which the host element should be retrieved.
*/
export function getHostElement<T>(component: T): HTMLElement {
return readElementValue(getComponentViewByInstance(component)).native as HTMLElement;
return readElementValue(getComponentViewByInstance(component)) as HTMLElement;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/render3/component_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {Type} from '../type';
import {assertComponentType, assertDefined} from './assert';
import {LifecycleHooksFeature, createRootComponent, createRootComponentView, createRootContext} from './component';
import {getComponentDef} from './definition';
import {adjustBlueprintForNewNode, createLViewData, createNodeAtIndex, createTView, elementCreate, enterView, locateHostElement, renderEmbeddedTemplate} from './instructions';
import {adjustBlueprintForNewNode, createLViewData, createNodeAtIndex, createTView, createViewNode, elementCreate, enterView, locateHostElement, renderEmbeddedTemplate} from './instructions';
import {ComponentDef, RenderFlags} from './interfaces/definition';
import {TElementNode, TNode, TNodeType, TViewNode} from './interfaces/node';
import {RElement, RendererFactory3, domRendererFactory3} from './interfaces/renderer';
Expand Down Expand Up @@ -141,7 +141,7 @@ export class ComponentFactory<T> extends viewEngine_ComponentFactory<T> {
createRootComponentView(hostRNode, this.componentDef, rootView, renderer);
tElementNode = getTNode(0, rootView) as TElementNode;

// Transform the arrays of native nodes into a LNode structure that can be consumed by the
// Transform the arrays of native nodes into a structure that can be consumed by the
// projection instruction. This is needed to support the reprojection of these nodes.
if (projectableNodes) {
let index = 0;
Expand Down Expand Up @@ -223,7 +223,7 @@ export class ComponentRef<T> extends viewEngine_ComponentRef<T> {
super();
this.instance = instance;
this.hostView = this.changeDetectorRef = new RootViewRef<T>(rootView);
this.hostView._tViewNode = createNodeAtIndex(-1, TNodeType.View, null, null, null, rootView);
this.hostView._tViewNode = createViewNode(-1, rootView);
this.injector = injector;
this.componentType = componentType;
}
Expand Down
53 changes: 20 additions & 33 deletions packages/core/src/render3/context_discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import './ng_dev_mode';

import {assertEqual} from './assert';
import {LContext, MONKEY_PATCH_KEY_NAME} from './interfaces/context';
import {LElementNode, TNode, TNodeFlags} from './interfaces/node';
import {TNode, TNodeFlags} from './interfaces/node';
import {RElement} from './interfaces/renderer';
import {CONTEXT, HEADER_OFFSET, HOST, LViewData, TVIEW} from './interfaces/view';
import {getComponentViewByIndex, readElementValue, readPatchedData} from './util';
import {getComponentViewByIndex, getNativeByTNode, readElementValue, readPatchedData} from './util';


/** Returns the matching `LContext` data for a given DOM node, directive or component instance.
Expand Down Expand Up @@ -66,11 +66,11 @@ export function getContext(target: any): LContext|null {
// are expensive. Instead, only the target data (the element, compontent or
// directive details) are filled into the context. If called multiple times
// with different target values then the missing target data will be filled in.
const lNode = getLNodeFromViewData(lViewData, nodeIndex) !;
const existingCtx = readPatchedData(lNode.native);
const native = readElementValue(lViewData[nodeIndex]);
const existingCtx = readPatchedData(native);
const context: LContext = (existingCtx && !Array.isArray(existingCtx)) ?
existingCtx :
createLContext(lViewData, nodeIndex, lNode.native);
createLContext(lViewData, nodeIndex, native);

// only when the component has been discovered then update the monkey-patch
if (component && context.component === undefined) {
Expand Down Expand Up @@ -114,9 +114,9 @@ export function getContext(target: any): LContext|null {

const index = findViaNativeElement(lViewData, rElement);
if (index >= 0) {
const lNode = getLNodeFromViewData(lViewData, index) !;
const context = createLContext(lViewData, index, lNode.native);
attachPatchData(lNode.native, context);
const native = readElementValue(lViewData[index]);
const context = createLContext(lViewData, index, native);
attachPatchData(native, context);
mpValue = context;
break;
}
Expand All @@ -129,10 +129,10 @@ export function getContext(target: any): LContext|null {
/**
* Creates an empty instance of a `LContext` context
*/
function createLContext(lViewData: LViewData, lNodeIndex: number, native: RElement): LContext {
function createLContext(lViewData: LViewData, nodeIndex: number, native: RElement): LContext {
return {
lViewData,
nodeIndex: lNodeIndex, native,
nodeIndex: nodeIndex, native,
component: undefined,
directives: undefined,
localRefs: undefined,
Expand All @@ -150,9 +150,9 @@ export function getComponentViewByInstance(componentInstance: {}): LViewData {
let view: LViewData;

if (Array.isArray(lViewData)) {
const lNodeIndex = findViaComponent(lViewData, componentInstance);
view = getComponentViewByIndex(lNodeIndex, lViewData);
const context = createLContext(lViewData, lNodeIndex, (view[HOST] as LElementNode).native);
const nodeIndex = findViaComponent(lViewData, componentInstance);
view = getComponentViewByIndex(nodeIndex, lViewData);
const context = createLContext(lViewData, nodeIndex, view[HOST] as RElement);
context.component = componentInstance;
attachPatchData(componentInstance, context);
attachPatchData(context.native, context);
Expand Down Expand Up @@ -182,11 +182,11 @@ export function isDirectiveInstance(instance: any): boolean {
/**
* Locates the element within the given LViewData and returns the matching index
*/
function findViaNativeElement(lViewData: LViewData, native: RElement): number {
function findViaNativeElement(lViewData: LViewData, target: RElement): number {
let tNode = lViewData[TVIEW].firstChild;
while (tNode) {
const lNode = getLNodeFromViewData(lViewData, tNode.index) !;
if (lNode.native === native) {
const native = getNativeByTNode(tNode, lViewData) !;
if (native === target) {
return tNode.index;
}
tNode = traverseNextElement(tNode);
Expand Down Expand Up @@ -261,18 +261,6 @@ function assertDomElement(element: any) {
assertEqual(element.nodeType, 1, 'The provided value must be an instance of an HTMLElement');
}

/**
* Retruns the instance of the LElementNode at the given index in the LViewData.
*
* This function will also unwrap the inner value incase it's stuffed into an
* array (which is what happens when [style] and [class] bindings are present
* in the view instructions for the element being returned).
*/
function getLNodeFromViewData(lViewData: LViewData, lElementIndex: number): LElementNode|null {
const value = lViewData[lElementIndex];
return value ? readElementValue(value) : null;
}

/**
* Returns a list of directives extracted from the given view based on the
* provided list of directive index values.
Expand All @@ -294,17 +282,16 @@ export function discoverDirectives(
* Returns a map of local references (local reference name => element or directive instance) that
* exist on a given element.
*/
export function discoverLocalRefs(lViewData: LViewData, lNodeIndex: number): {[key: string]: any}|
export function discoverLocalRefs(lViewData: LViewData, nodeIndex: number): {[key: string]: any}|
null {
const tNode = lViewData[TVIEW].data[lNodeIndex] as TNode;
const tNode = lViewData[TVIEW].data[nodeIndex] as TNode;
if (tNode && tNode.localNames) {
const result: {[key: string]: any} = {};
for (let i = 0; i < tNode.localNames.length; i += 2) {
const localRefName = tNode.localNames[i];
const directiveIndex = tNode.localNames[i + 1] as number;
result[localRefName] = directiveIndex === -1 ?
getLNodeFromViewData(lViewData, lNodeIndex) !.native :
lViewData[directiveIndex];
result[localRefName] =
directiveIndex === -1 ? getNativeByTNode(tNode, lViewData) ! : lViewData[directiveIndex];
}
return result;
}
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/render3/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@

import {getInjectableDef, getInjectorDef} from '../di/defs';
import {InjectionToken} from '../di/injection_token';
import {InjectFlags, Injector, NullInjector, inject, setCurrentInjector} from '../di/injector';
import {Renderer2} from '../render';
import {InjectFlags, Injector, inject, setCurrentInjector} from '../di/injector';
import {Type} from '../type';

import {assertDefined} from './assert';
import {getComponentDef, getDirectiveDef, getPipeDef} from './definition';
import {NG_ELEMENT_ID} from './fields';
import {_getViewData, assertPreviousIsParent, getPreviousOrParentTNode, resolveDirective, setEnvironment} from './instructions';
import {_getViewData, getPreviousOrParentTNode, resolveDirective, setEnvironment} from './instructions';
import {DirectiveDef} from './interfaces/definition';
import {InjectorLocationFlags, PARENT_INJECTOR, TNODE,} from './interfaces/injector';
import {AttributeMarker, TContainerNode, TElementContainerNode, TElementNode, TNode, TNodeFlags, TNodeType} from './interfaces/node';
import {DECLARATION_VIEW, HOST_NODE, INJECTOR, LViewData, PARENT, RENDERER, TData, TVIEW, TView} from './interfaces/view';
import {DECLARATION_VIEW, HOST_NODE, INJECTOR, LViewData, TData, TVIEW, TView} from './interfaces/view';
import {assertNodeOfPossibleTypes} from './node_assert';

/**
Expand Down
15 changes: 8 additions & 7 deletions packages/core/src/render3/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
*/

import {assertEqual, assertLessThan} from './assert';
import {NO_CHANGE, _getViewData, adjustBlueprintForNewNode, bindingUpdated, bindingUpdated2, bindingUpdated3, bindingUpdated4, createNodeAtIndex, getRenderer, load, loadElement, resetComponentState} from './instructions';
import {NO_CHANGE, _getViewData, adjustBlueprintForNewNode, bindingUpdated, bindingUpdated2, bindingUpdated3, bindingUpdated4, createNodeAtIndex, getRenderer, load, resetComponentState} from './instructions';
import {LContainer, NATIVE, RENDER_PARENT} from './interfaces/container';
import {LContainerNode, LNode, TElementNode, TNode, TNodeType} from './interfaces/node';
import {TElementNode, TNode, TNodeType} from './interfaces/node';
import {RComment, RElement} from './interfaces/renderer';
import {StylingContext} from './interfaces/styling';
import {BINDING_INDEX, HEADER_OFFSET, HOST_NODE, TVIEW} from './interfaces/view';
import {appendChild, createTextNode, removeChild} from './node_manipulation';
import {getNative, getTNode, isLContainer, stringify} from './util';
import {getNativeByIndex, getNativeByTNode, getTNode, isLContainer, stringify} from './util';



Expand Down Expand Up @@ -273,7 +274,7 @@ function appendI18nNode(tNode: TNode, parentTNode: TNode, previousTNode: TNode):
}
}

appendChild(getNative(tNode, viewData), tNode, viewData);
appendChild(getNativeByTNode(tNode, viewData), tNode, viewData);

const slotValue = viewData[tNode.index];
if (tNode.type !== TNodeType.Container && isLContainer(slotValue)) {
Expand Down Expand Up @@ -364,11 +365,11 @@ export function i18nApply(startIndex: number, instructions: I18nInstruction[]):
ngDevMode.rendererRemoveNode++;
}
const removeIndex = instruction & I18nInstructions.IndexMask;
const removedNode: LNode|LContainerNode = loadElement(removeIndex);
const removedElement: RElement|RComment = getNativeByIndex(removeIndex, viewData);
const removedTNode = getTNode(removeIndex, viewData);
removeChild(removedTNode, removedNode.native || null, viewData);
removeChild(removedTNode, removedElement || null, viewData);

const slotValue = load(removeIndex) as LNode | LContainer | StylingContext;
const slotValue = load(removeIndex) as RElement | RComment | LContainer | StylingContext;
if (isLContainer(slotValue)) {
const lContainer = slotValue as LContainer;
if (removedTNode.type !== TNodeType.Container) {
Expand Down

0 comments on commit e76a570

Please sign in to comment.