Skip to content

Commit f23946a

Browse files
crisbetokirjs
authored andcommitted
refactor(core): move component logic out of host directives resolution (#60075)
In order to mark a TNode as a component, we need to store the index of the component definition. Currently this happens in the logic that resolves host directives, because the component's host directives can move affect the index. These changes move the logic out into the directive initialization logic since it doesn't have much to do with host directives. PR Close #60075
1 parent fa6d9c2 commit f23946a

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

packages/core/src/render3/view/directives.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,8 @@ export function resolveDirectives(
7676
const matchedDirectiveDefs = directiveMatcher(tView, tNode);
7777

7878
if (matchedDirectiveDefs !== null) {
79-
const [directiveDefs, hostDirectiveDefs, hostDirectiveRanges] = resolveHostDirectives(
80-
tView,
81-
tNode,
82-
matchedDirectiveDefs,
83-
);
79+
const [directiveDefs, hostDirectiveDefs, hostDirectiveRanges] =
80+
resolveHostDirectives(matchedDirectiveDefs);
8481

8582
initializeDirectives(
8683
tView,
@@ -120,8 +117,6 @@ function cacheMatchingLocalNames(
120117
}
121118

122119
function resolveHostDirectives(
123-
tView: TView,
124-
tNode: TNode,
125120
matches: DirectiveDef<unknown>[],
126121
): [
127122
matches: DirectiveDef<unknown>[],
@@ -150,7 +145,7 @@ function resolveHostDirectives(
150145
hostDirectiveDefs ??= new Map();
151146
resolveHostDirectivesForDef(def, allDirectiveDefs, hostDirectiveRanges, hostDirectiveDefs);
152147
}
153-
markAsComponentHost(tView, tNode, allDirectiveDefs.push(def) - 1);
148+
allDirectiveDefs.push(def);
154149
}
155150

156151
// If there's a component, we already processed it above so we can skip it here.
@@ -217,11 +212,17 @@ function initializeDirectives(
217212
ngDevMode && assertFirstCreatePass(tView);
218213

219214
const directivesLength = directives.length;
215+
let hasSeenComponent = false;
220216

221217
// Publishes the directive types to DI so they can be injected. Needs to
222218
// happen in a separate pass before the TNode flags have been initialized.
223219
for (let i = 0; i < directivesLength; i++) {
224-
diPublicInInjector(getOrCreateNodeInjectorForNode(tNode, lView), tView, directives[i].type);
220+
const def = directives[i];
221+
if (!hasSeenComponent && isComponentDef(def)) {
222+
hasSeenComponent = true;
223+
markAsComponentHost(tView, tNode, i);
224+
}
225+
diPublicInInjector(getOrCreateNodeInjectorForNode(tNode, lView), tView, def.type);
225226
}
226227

227228
initTNodeFlags(tNode, tView.data.length, directivesLength);

0 commit comments

Comments
 (0)