MCPcopy Create free account
hub / github.com/angular/angular / locateDirectiveOrProvider

Function locateDirectiveOrProvider

packages/core/src/render3/di.ts:686–722  ·  view source on GitHub ↗
(
  tNode: TNode,
  tView: TView,
  token: ProviderToken<T> | string,
  canAccessViewProviders: boolean,
  isHostSpecialCase: boolean | number,
)

Source from the content-addressed store, hash-verified

684 * @returns Index of a found directive or provider, or null when none found.
685 */
686export function locateDirectiveOrProvider<T>(
687 tNode: TNode,
688 tView: TView,
689 token: ProviderToken<T> | string,
690 canAccessViewProviders: boolean,
691 isHostSpecialCase: boolean | number,
692): number | null {
693 const nodeProviderIndexes = tNode.providerIndexes;
694 const tInjectables = tView.data;
695
696 const injectablesStart = nodeProviderIndexes & TNodeProviderIndexes.ProvidersStartIndexMask;
697 const directivesStart = tNode.directiveStart;
698 const directiveEnd = tNode.directiveEnd;
699 const cptViewProvidersCount =
700 nodeProviderIndexes >> TNodeProviderIndexes.CptViewProvidersCountShift;
701 const startingIndex = canAccessViewProviders
702 ? injectablesStart
703 : injectablesStart + cptViewProvidersCount;
704 // When the host special case applies, only the viewProviders and the component are visible
705 const endIndex = isHostSpecialCase ? injectablesStart + cptViewProvidersCount : directiveEnd;
706 for (let i = startingIndex; i < endIndex; i++) {
707 const providerTokenOrDef = tInjectables[i] as ProviderToken<any> | DirectiveDef<any> | string;
708 if (
709 (i < directivesStart && token === providerTokenOrDef) ||
710 (i >= directivesStart && (providerTokenOrDef as DirectiveDef<any>).type === token)
711 ) {
712 return i;
713 }
714 }
715 if (isHostSpecialCase) {
716 const dirDef = tInjectables[directivesStart] as DirectiveDef<any>;
717 if (dirDef && isComponentDef(dirDef) && dirDef.type === token) {
718 return directivesStart;
719 }
720 }
721 return null;
722}
723
724/**
725 * Used in ngDevMode to keep the injection path in case of cycles in DI.

Callers 3

matchTNodeMethod · 0.90
searchTokensOnInjectorFunction · 0.85

Calls 1

isComponentDefFunction · 0.90

Tested by

no test coverage detected