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

Function searchTokensOnInjector

packages/core/src/render3/di.ts:628–674  ·  view source on GitHub ↗
(
  injectorIndex: number,
  lView: LView,
  token: ProviderToken<T>,
  previousTView: TView | null,
  flags: InternalInjectFlags,
  hostTElementNode: TNode | null,
)

Source from the content-addressed store, hash-verified

626}
627
628function searchTokensOnInjector<T>(
629 injectorIndex: number,
630 lView: LView,
631 token: ProviderToken<T>,
632 previousTView: TView | null,
633 flags: InternalInjectFlags,
634 hostTElementNode: TNode | null,
635) {
636 const currentTView = lView[TVIEW];
637 const tNode = currentTView.data[injectorIndex + NodeInjectorOffset.TNODE] as TNode;
638 // First, we need to determine if view providers can be accessed by the starting element.
639 // There are two possibilities
640 const canAccessViewProviders =
641 previousTView == null
642 ? // 1) This is the first invocation `previousTView == null` which means that we are at the
643 // `TNode` of where injector is starting to look. In such a case the only time we are allowed
644 // to look into the ViewProviders is if:
645 // - we are on a component
646 // - AND the injector set `includeViewProviders` to true (implying that the token can see
647 // ViewProviders because it is the Component or a Service which itself was declared in
648 // ViewProviders)
649 isComponentHost(tNode) && includeViewProviders
650 : // 2) `previousTView != null` which means that we are now walking across the parent nodes.
651 // In such a case we are only allowed to look into the ViewProviders if:
652 // - We just crossed from child View to Parent View `previousTView != currentTView`
653 // - AND the parent TNode is an Element.
654 // This means that we just came from the Component's View and therefore are allowed to see
655 // into the ViewProviders.
656 previousTView != currentTView && (tNode.type & TNodeType.AnyRNode) !== 0;
657
658 // This special case happens when there is a @host on the inject and when we are searching
659 // on the host element node.
660 const isHostSpecialCase = flags & InternalInjectFlags.Host && hostTElementNode === tNode;
661
662 const injectableIdx = locateDirectiveOrProvider(
663 tNode,
664 currentTView,
665 token,
666 canAccessViewProviders,
667 isHostSpecialCase,
668 );
669 if (injectableIdx !== null) {
670 return getNodeInjectable(lView, currentTView, injectableIdx, tNode as TElementNode, flags);
671 } else {
672 return NOT_FOUND;
673 }
674}
675
676/**
677 * Searches for the given token among the node's directives and providers.

Callers 1

Calls 3

isComponentHostFunction · 0.90
getNodeInjectableFunction · 0.85

Tested by

no test coverage detected