Skip to content

Commit e76a570

Browse files
karamhevery
authored andcommitted
refactor(ivy): remove LNode (angular#26426)
PR Close angular#26426
1 parent 9afc9a7 commit e76a570

27 files changed

+302
-449
lines changed

packages/core/src/render3/component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import {getComponentDef} from './definition';
1818
import {queueInitHooks, queueLifecycleHooks} from './hooks';
1919
import {CLEAN_PROMISE, baseDirectiveCreate, createLViewData, createNodeAtIndex, createTView, detectChangesInternal, enterView, executeInitAndContentHooks, getOrCreateTView, leaveView, locateHostElement, prefillHostVars, resetComponentState, setHostBindings} from './instructions';
2020
import {ComponentDef, ComponentType} from './interfaces/definition';
21-
import {TNodeFlags, TNodeType} from './interfaces/node';
21+
import {TElementNode, TNodeFlags, TNodeType} from './interfaces/node';
2222
import {PlayerHandler} from './interfaces/player';
2323
import {RElement, RNode, Renderer3, RendererFactory3, domRendererFactory3} from './interfaces/renderer';
24-
import {CONTEXT, HEADER_OFFSET, HOST, INJECTOR, LViewData, LViewFlags, RootContext, RootContextFlags, TVIEW} from './interfaces/view';
24+
import {CONTEXT, HEADER_OFFSET, HOST, HOST_NODE, INJECTOR, LViewData, LViewFlags, RootContext, RootContextFlags, TVIEW} from './interfaces/view';
2525
import {getRootView, readElementValue, readPatchedLViewData, stringify} from './util';
2626

2727

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

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

178178
// Store component view at node index, with node as the HOST
179179
componentView[HOST] = rootView[HEADER_OFFSET];
180+
componentView[HOST_NODE] = tNode as TElementNode;
180181
return rootView[HEADER_OFFSET] = componentView;
181182
}
182183

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

261261
/**

packages/core/src/render3/component_ref.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {Type} from '../type';
1919
import {assertComponentType, assertDefined} from './assert';
2020
import {LifecycleHooksFeature, createRootComponent, createRootComponentView, createRootContext} from './component';
2121
import {getComponentDef} from './definition';
22-
import {adjustBlueprintForNewNode, createLViewData, createNodeAtIndex, createTView, elementCreate, enterView, locateHostElement, renderEmbeddedTemplate} from './instructions';
22+
import {adjustBlueprintForNewNode, createLViewData, createNodeAtIndex, createTView, createViewNode, elementCreate, enterView, locateHostElement, renderEmbeddedTemplate} from './instructions';
2323
import {ComponentDef, RenderFlags} from './interfaces/definition';
2424
import {TElementNode, TNode, TNodeType, TViewNode} from './interfaces/node';
2525
import {RElement, RendererFactory3, domRendererFactory3} from './interfaces/renderer';
@@ -141,7 +141,7 @@ export class ComponentFactory<T> extends viewEngine_ComponentFactory<T> {
141141
createRootComponentView(hostRNode, this.componentDef, rootView, renderer);
142142
tElementNode = getTNode(0, rootView) as TElementNode;
143143

144-
// Transform the arrays of native nodes into a LNode structure that can be consumed by the
144+
// Transform the arrays of native nodes into a structure that can be consumed by the
145145
// projection instruction. This is needed to support the reprojection of these nodes.
146146
if (projectableNodes) {
147147
let index = 0;
@@ -223,7 +223,7 @@ export class ComponentRef<T> extends viewEngine_ComponentRef<T> {
223223
super();
224224
this.instance = instance;
225225
this.hostView = this.changeDetectorRef = new RootViewRef<T>(rootView);
226-
this.hostView._tViewNode = createNodeAtIndex(-1, TNodeType.View, null, null, null, rootView);
226+
this.hostView._tViewNode = createViewNode(-1, rootView);
227227
this.injector = injector;
228228
this.componentType = componentType;
229229
}

packages/core/src/render3/context_discovery.ts

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import './ng_dev_mode';
99

1010
import {assertEqual} from './assert';
1111
import {LContext, MONKEY_PATCH_KEY_NAME} from './interfaces/context';
12-
import {LElementNode, TNode, TNodeFlags} from './interfaces/node';
12+
import {TNode, TNodeFlags} from './interfaces/node';
1313
import {RElement} from './interfaces/renderer';
1414
import {CONTEXT, HEADER_OFFSET, HOST, LViewData, TVIEW} from './interfaces/view';
15-
import {getComponentViewByIndex, readElementValue, readPatchedData} from './util';
15+
import {getComponentViewByIndex, getNativeByTNode, readElementValue, readPatchedData} from './util';
1616

1717

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

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

115115
const index = findViaNativeElement(lViewData, rElement);
116116
if (index >= 0) {
117-
const lNode = getLNodeFromViewData(lViewData, index) !;
118-
const context = createLContext(lViewData, index, lNode.native);
119-
attachPatchData(lNode.native, context);
117+
const native = readElementValue(lViewData[index]);
118+
const context = createLContext(lViewData, index, native);
119+
attachPatchData(native, context);
120120
mpValue = context;
121121
break;
122122
}
@@ -129,10 +129,10 @@ export function getContext(target: any): LContext|null {
129129
/**
130130
* Creates an empty instance of a `LContext` context
131131
*/
132-
function createLContext(lViewData: LViewData, lNodeIndex: number, native: RElement): LContext {
132+
function createLContext(lViewData: LViewData, nodeIndex: number, native: RElement): LContext {
133133
return {
134134
lViewData,
135-
nodeIndex: lNodeIndex, native,
135+
nodeIndex: nodeIndex, native,
136136
component: undefined,
137137
directives: undefined,
138138
localRefs: undefined,
@@ -150,9 +150,9 @@ export function getComponentViewByInstance(componentInstance: {}): LViewData {
150150
let view: LViewData;
151151

152152
if (Array.isArray(lViewData)) {
153-
const lNodeIndex = findViaComponent(lViewData, componentInstance);
154-
view = getComponentViewByIndex(lNodeIndex, lViewData);
155-
const context = createLContext(lViewData, lNodeIndex, (view[HOST] as LElementNode).native);
153+
const nodeIndex = findViaComponent(lViewData, componentInstance);
154+
view = getComponentViewByIndex(nodeIndex, lViewData);
155+
const context = createLContext(lViewData, nodeIndex, view[HOST] as RElement);
156156
context.component = componentInstance;
157157
attachPatchData(componentInstance, context);
158158
attachPatchData(context.native, context);
@@ -182,11 +182,11 @@ export function isDirectiveInstance(instance: any): boolean {
182182
/**
183183
* Locates the element within the given LViewData and returns the matching index
184184
*/
185-
function findViaNativeElement(lViewData: LViewData, native: RElement): number {
185+
function findViaNativeElement(lViewData: LViewData, target: RElement): number {
186186
let tNode = lViewData[TVIEW].firstChild;
187187
while (tNode) {
188-
const lNode = getLNodeFromViewData(lViewData, tNode.index) !;
189-
if (lNode.native === native) {
188+
const native = getNativeByTNode(tNode, lViewData) !;
189+
if (native === target) {
190190
return tNode.index;
191191
}
192192
tNode = traverseNextElement(tNode);
@@ -261,18 +261,6 @@ function assertDomElement(element: any) {
261261
assertEqual(element.nodeType, 1, 'The provided value must be an instance of an HTMLElement');
262262
}
263263

264-
/**
265-
* Retruns the instance of the LElementNode at the given index in the LViewData.
266-
*
267-
* This function will also unwrap the inner value incase it's stuffed into an
268-
* array (which is what happens when [style] and [class] bindings are present
269-
* in the view instructions for the element being returned).
270-
*/
271-
function getLNodeFromViewData(lViewData: LViewData, lElementIndex: number): LElementNode|null {
272-
const value = lViewData[lElementIndex];
273-
return value ? readElementValue(value) : null;
274-
}
275-
276264
/**
277265
* Returns a list of directives extracted from the given view based on the
278266
* provided list of directive index values.
@@ -294,17 +282,16 @@ export function discoverDirectives(
294282
* Returns a map of local references (local reference name => element or directive instance) that
295283
* exist on a given element.
296284
*/
297-
export function discoverLocalRefs(lViewData: LViewData, lNodeIndex: number): {[key: string]: any}|
285+
export function discoverLocalRefs(lViewData: LViewData, nodeIndex: number): {[key: string]: any}|
298286
null {
299-
const tNode = lViewData[TVIEW].data[lNodeIndex] as TNode;
287+
const tNode = lViewData[TVIEW].data[nodeIndex] as TNode;
300288
if (tNode && tNode.localNames) {
301289
const result: {[key: string]: any} = {};
302290
for (let i = 0; i < tNode.localNames.length; i += 2) {
303291
const localRefName = tNode.localNames[i];
304292
const directiveIndex = tNode.localNames[i + 1] as number;
305-
result[localRefName] = directiveIndex === -1 ?
306-
getLNodeFromViewData(lViewData, lNodeIndex) !.native :
307-
lViewData[directiveIndex];
293+
result[localRefName] =
294+
directiveIndex === -1 ? getNativeByTNode(tNode, lViewData) ! : lViewData[directiveIndex];
308295
}
309296
return result;
310297
}

packages/core/src/render3/di.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@
1111

1212
import {getInjectableDef, getInjectorDef} from '../di/defs';
1313
import {InjectionToken} from '../di/injection_token';
14-
import {InjectFlags, Injector, NullInjector, inject, setCurrentInjector} from '../di/injector';
15-
import {Renderer2} from '../render';
14+
import {InjectFlags, Injector, inject, setCurrentInjector} from '../di/injector';
1615
import {Type} from '../type';
1716

1817
import {assertDefined} from './assert';
1918
import {getComponentDef, getDirectiveDef, getPipeDef} from './definition';
2019
import {NG_ELEMENT_ID} from './fields';
21-
import {_getViewData, assertPreviousIsParent, getPreviousOrParentTNode, resolveDirective, setEnvironment} from './instructions';
20+
import {_getViewData, getPreviousOrParentTNode, resolveDirective, setEnvironment} from './instructions';
2221
import {DirectiveDef} from './interfaces/definition';
2322
import {InjectorLocationFlags, PARENT_INJECTOR, TNODE,} from './interfaces/injector';
2423
import {AttributeMarker, TContainerNode, TElementContainerNode, TElementNode, TNode, TNodeFlags, TNodeType} from './interfaces/node';
25-
import {DECLARATION_VIEW, HOST_NODE, INJECTOR, LViewData, PARENT, RENDERER, TData, TVIEW, TView} from './interfaces/view';
24+
import {DECLARATION_VIEW, HOST_NODE, INJECTOR, LViewData, TData, TVIEW, TView} from './interfaces/view';
2625
import {assertNodeOfPossibleTypes} from './node_assert';
2726

2827
/**

packages/core/src/render3/i18n.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
*/
88

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

1819

1920

@@ -273,7 +274,7 @@ function appendI18nNode(tNode: TNode, parentTNode: TNode, previousTNode: TNode):
273274
}
274275
}
275276

276-
appendChild(getNative(tNode, viewData), tNode, viewData);
277+
appendChild(getNativeByTNode(tNode, viewData), tNode, viewData);
277278

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

371-
const slotValue = load(removeIndex) as LNode | LContainer | StylingContext;
372+
const slotValue = load(removeIndex) as RElement | RComment | LContainer | StylingContext;
372373
if (isLContainer(slotValue)) {
373374
const lContainer = slotValue as LContainer;
374375
if (removedTNode.type !== TNodeType.Container) {

0 commit comments

Comments
 (0)