Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Revert "refactor(core): reuse directive instantiate logic (#59633)" #59659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 148 additions & 39 deletions packages/core/src/render3/component_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@ import {createElementRef, ElementRef} from '../linker/element_ref';
import {NgModuleRef} from '../linker/ng_module_factory';
import {RendererFactory2} from '../render/api';
import {Sanitizer} from '../sanitization/sanitizer';
import {assertDefined} from '../util/assert';
import {assertDefined, assertGreaterThan, assertIndexInRange} from '../util/assert';

import {assertComponentType, assertNoDuplicateDirectives} from './assert';
import {attachPatchData} from './context_discovery';
import {getComponentDef} from './def_getters';
import {depsTracker} from './deps_tracker/deps_tracker';
import {NodeInjector} from './di';
import {getNodeInjectable, NodeInjector} from './di';
import {registerPostOrderHooks} from './hooks';
import {reportUnknownPropertyError} from './instructions/element_validation';
import {markViewDirty} from './instructions/mark_view_dirty';
import {renderView} from './instructions/render';
import {
createDirectivesInstances,
addToEndOfViewTree,
createLView,
createTView,
getInitialLViewFlagsFromDef,
getOrCreateComponentTView,
initializeDirectives,
invokeDirectivesHostBindings,
locateHostElement,
markAsComponentHost,
setInputsForProperty,
Expand All @@ -59,10 +62,11 @@ import {
TNode,
TNodeType,
} from './interfaces/node';
import {RNode} from './interfaces/renderer_dom';
import {RElement, RNode} from './interfaces/renderer_dom';
import {
CONTEXT,
HEADER_OFFSET,
INJECTOR,
LView,
LViewEnvironment,
LViewFlags,
Expand All @@ -71,25 +75,25 @@ import {
} from './interfaces/view';
import {MATH_ML_NAMESPACE, SVG_NAMESPACE} from './namespaces';

import {ChainedInjector} from './chained_injector';
import {createElementNode, setupStaticAttributes} from './dom_node_manipulation';
import {AttributeMarker} from './interfaces/attribute_marker';
import {unregisterLView} from './interfaces/lview_tracking';
import {CssSelector} from './interfaces/projection';
import {
extractAttrsAndClassesFromSelector,
stringifyCSSSelectorList,
} from './node_selector_matcher';
import {profiler} from './profiler';
import {ProfilerEvent} from './profiler_types';
import {executeContentQueries} from './queries/query_execution';
import {enterView, getCurrentTNode, getLView, leaveView} from './state';
import {computeStaticStyling} from './styling/static_styling';
import {getOrCreateTNode} from './tnode_manipulation';
import {mergeHostAttrs} from './util/attrs_utils';
import {debugStringifyTypeForError, stringifyForError} from './util/stringify_utils';
import {getComponentLViewByIndex, getTNode} from './util/view_utils';
import {getComponentLViewByIndex, getNativeByTNode, getTNode} from './util/view_utils';
import {ViewRef} from './view_ref';
import {ChainedInjector} from './chained_injector';
import {unregisterLView} from './interfaces/lview_tracking';
import {profiler} from './profiler';
import {ProfilerEvent} from './profiler_types';
import {executeContentQueries} from './queries/query_execution';
import {AttributeMarker} from './interfaces/attribute_marker';
import {CssSelector} from './interfaces/projection';
import {getOrCreateTNode} from './tnode_manipulation';

export class ComponentFactoryResolver extends AbstractComponentFactoryResolver {
/**
Expand Down Expand Up @@ -324,7 +328,7 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
null,
null,
);
const rootLView = createLView<T>(
const rootLView = createLView(
null,
rootTView,
null,
Expand All @@ -347,10 +351,10 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
// issues would allow us to drop this.
enterView(rootLView);

let component: T;
let componentView: LView | null = null;

try {
// TODO(pk): this is a good refactoring since it shows how to instantiate a TNOde with a set of known directive defs
const rootComponentDef = this.componentDef;
let rootDirectives: DirectiveDef<unknown>[];
let hostDirectiveDefs: HostDirectiveDefs | null = null;
Expand Down Expand Up @@ -384,18 +388,6 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
tAttributes,
);

// TODO(pk): partial code duplication with resolveDirectives and other existing logic
markAsComponentHost(rootTView, hostTNode, rootDirectives.length - 1);
initializeDirectives(
rootTView,
rootLView,
hostTNode,
rootDirectives,
null,
hostDirectiveDefs,
);
registerPostOrderHooks(rootTView, hostTNode);

for (const def of rootDirectives) {
hostTNode.mergedAttrs = mergeHostAttrs(hostTNode.mergedAttrs, def.hostAttrs);
}
Expand All @@ -408,22 +400,31 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
// tests so that this check can be removed.
if (hostRNode) {
setupStaticAttributes(hostRenderer, hostRNode, hostTNode);
attachPatchData(hostRNode, rootLView);
}

componentView = createRootComponentView(
hostTNode,
hostRNode,
rootComponentDef,
rootDirectives,
rootLView,
environment,
);

if (projectableNodes !== undefined) {
projectNodes(hostTNode, this.ngContentSelectors, projectableNodes);
}

// TODO(pk): this logic is similar to the instruction code where a node can have directives
createDirectivesInstances(rootTView, rootLView, hostTNode);
executeContentQueries(rootTView, hostTNode, rootLView);

componentView = getComponentLViewByIndex(hostTNode.index, rootLView);

// TODO(pk): why do we need this logic?
rootLView[CONTEXT] = componentView[CONTEXT] as T;

// TODO: should LifecycleHooksFeature and other host features be generated by the compiler
// and executed here? Angular 5 reference: https://stackblitz.com/edit/lifecycle-hooks-vcref
component = createRootComponent(
componentView,
rootComponentDef,
rootDirectives,
hostDirectiveDefs,
rootLView,
[LifecycleHooksFeature],
);
renderView(rootTView, rootLView, null);
} catch (e) {
// Stop tracking the views if creation failed since
Expand All @@ -441,7 +442,7 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
const hostTNode = getTNode(rootTView, HEADER_OFFSET) as TElementNode;
return new ComponentRef(
this.componentType,
componentView[CONTEXT] as T,
component,
createElementRef(hostTNode, rootLView),
rootLView,
hostTNode,
Expand Down Expand Up @@ -526,10 +527,118 @@ export class ComponentRef<T> extends AbstractComponentRef<T> {
}
}

// TODO: remove?
/** Represents a HostFeature function. */
type HostFeature = <T>(component: T, componentDef: ComponentDef<T>) => void;

/**
* Creates the root component view and the root component node.
*
* @param hostRNode Render host element.
* @param rootComponentDef ComponentDef
* @param rootView The parent view where the host node is stored
* @param rendererFactory Factory to be used for creating child renderers.
* @param hostRenderer The current renderer
* @param sanitizer The sanitizer, if provided
*
* @returns Component view created
*/
function createRootComponentView(
tNode: TElementNode,
hostRNode: RElement | null,
rootComponentDef: ComponentDef<any>,
rootDirectives: DirectiveDef<any>[],
rootView: LView,
environment: LViewEnvironment,
): LView {
const tView = rootView[TVIEW];

// Hydration info is on the host element and needs to be retrieved
// and passed to the component LView.
let hydrationInfo: DehydratedView | null = null;
if (hostRNode !== null) {
hydrationInfo = retrieveHydrationInfo(hostRNode, rootView[INJECTOR]);
}
const viewRenderer = environment.rendererFactory.createRenderer(hostRNode, rootComponentDef);
const componentView = createLView(
rootView,
getOrCreateComponentTView(rootComponentDef),
null,
getInitialLViewFlagsFromDef(rootComponentDef),
rootView[tNode.index],
tNode,
environment,
viewRenderer,
null,
null,
hydrationInfo,
);

if (tView.firstCreatePass) {
markAsComponentHost(tView, tNode, rootDirectives.length - 1);
}

addToEndOfViewTree(rootView, componentView);

// Store component view at node index, with node as the HOST
return (rootView[tNode.index] = componentView);
}

/**
* Creates a root component and sets it up with features and host bindings.Shared by
* renderComponent() and ViewContainerRef.createComponent().
*/
function createRootComponent<T>(
componentView: LView,
rootComponentDef: ComponentDef<T>,
rootDirectives: DirectiveDef<any>[],
hostDirectiveDefs: HostDirectiveDefs | null,
rootLView: LView,
hostFeatures: HostFeature[] | null,
): any {
const rootTNode = getCurrentTNode() as TElementNode;
ngDevMode && assertDefined(rootTNode, 'tNode should have been already created');
const tView = rootLView[TVIEW];
const native = getNativeByTNode(rootTNode, rootLView);

initializeDirectives(tView, rootLView, rootTNode, rootDirectives, null, hostDirectiveDefs);

for (let i = 0; i < rootDirectives.length; i++) {
const directiveIndex = rootTNode.directiveStart + i;
const directiveInstance = getNodeInjectable(rootLView, tView, directiveIndex, rootTNode);
attachPatchData(directiveInstance, rootLView);
}

invokeDirectivesHostBindings(tView, rootLView, rootTNode);

if (native) {
attachPatchData(native, rootLView);
}

// We're guaranteed for the `componentOffset` to be positive here
// since a root component always matches a component def.
ngDevMode &&
assertGreaterThan(rootTNode.componentOffset, -1, 'componentOffset must be great than -1');
const component = getNodeInjectable(
rootLView,
tView,
rootTNode.directiveStart + rootTNode.componentOffset,
rootTNode,
);
componentView[CONTEXT] = rootLView[CONTEXT] = component;

if (hostFeatures !== null) {
for (const feature of hostFeatures) {
feature(component, rootComponentDef);
}
}

// We want to generate an empty QueryList for root content queries for backwards
// compatibility with ViewEngine.
executeContentQueries(tView, rootTNode, rootLView);

return component;
}

/** Projects the `projectableNodes` that were specified when creating a root component. */
function projectNodes(
tNode: TElementNode,
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/render3/instructions/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ import {getConstant} from '../util/view_utils';

import {validateElementIsKnown} from './element_validation';
import {setDirectiveInputsWhichShadowsStyling} from './property';
import {
createDirectivesInstancesInInstruction,
resolveDirectives,
saveResolvedLocalsInData,
} from './shared';
import {createDirectivesInstances, resolveDirectives, saveResolvedLocalsInData} from './shared';
import {getOrCreateTNode} from '../tnode_manipulation';

function elementStartFirstCreatePass(
Expand Down Expand Up @@ -176,7 +172,7 @@ export function ɵɵelementStart(
increaseElementDepthCount();

if (hasDirectives) {
createDirectivesInstancesInInstruction(tView, lView, tNode);
createDirectivesInstances(tView, lView, tNode);
executeContentQueries(tView, tNode, lView);
}
if (localRefsIndex !== null) {
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/render3/instructions/element_container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ import {
import {computeStaticStyling} from '../styling/static_styling';
import {getConstant} from '../util/view_utils';

import {
createDirectivesInstancesInInstruction,
resolveDirectives,
saveResolvedLocalsInData,
} from './shared';
import {createDirectivesInstances, resolveDirectives, saveResolvedLocalsInData} from './shared';
import {getOrCreateTNode} from '../tnode_manipulation';

function elementContainerStartFirstCreatePass(
Expand Down Expand Up @@ -123,7 +119,7 @@ export function ɵɵelementContainerStart(
attachPatchData(comment, lView);

if (isDirectiveHost(tNode)) {
createDirectivesInstancesInInstruction(tView, lView, tNode);
createDirectivesInstances(tView, lView, tNode);
executeContentQueries(tView, tNode, lView);
}

Expand Down
Loading
Loading