beforeChildren method

void beforeChildren()

Implementation

void beforeChildren() {
  if (hasViewContainer &&
      !_providers.containsLocalProvider(Identifiers.ViewContainerRefToken)) {
    _providers.add(Identifiers.ViewContainerRefToken, appViewContainer!);
  }

  // Access builtins with special visibility.
  _providers.add(
      Identifiers.ChangeDetectorRefToken, componentView ?? o.THIS_EXPR);

  // ComponentLoader is currently just an alias for ViewContainerRef with
  // a smaller API that is also usable outside of the context of a
  // structural directive.
  if (appViewContainer != null) {
    _providers.add(Identifiers.ComponentLoaderToken, appViewContainer!);
  }

  _providers.addDirectiveProviders(_resolvedProvidersArray, _directives);
  view!.registerDirectives(this, _directiveInstances);

  directiveInstances = <ProviderSource?>[];
  for (var directive in _directives) {
    var directiveInstance = getDirectiveSource(directive);
    directiveInstances.add(directiveInstance);
    for (var queryMeta in directive.queries) {
      _addQuery(queryMeta, directiveInstance);
    }
  }

  var queriesWithReads = <_QueryWithRead>[];
  for (var resolvedProvider in _resolvedProvidersArray) {
    var queriesForProvider = _getQueriesFor(resolvedProvider.token);
    queriesWithReads.addAll(queriesForProvider
        .map((query) => _QueryWithRead(query, resolvedProvider.token)));
  }

  // For each reference token create CompileTokenMetadata to read query.
  if (referenceTokens.isNotEmpty) {
    referenceTokens.forEach((String varName, token) {
      var varValue = token != null
          ? _providers.get(token)!.build()
          : renderNode.toReadExpr();
      view!.nameResolver.addLocal(varName, varValue);
      var varToken = CompileTokenMetadata(value: varName);
      queriesWithReads.addAll(_getQueriesFor(varToken)
          .map((query) => _QueryWithRead(query, varToken)));
    });
  }

  // For all @ViewChild(... read: Type) and references that map to locals,
  // resolve value.
  for (var queryWithRead in queriesWithReads) {
    o.Expression? value;
    o.Expression? changeDetectorRef;

    if (queryWithRead.read.identifier != null) {
      // Query for an identifier.
      var providerSource = _providers.get(queryWithRead.read);
      if (providerSource != null) {
        value = providerSource.build();
        changeDetectorRef = providerSource.buildChangeDetectorRef();
      }
    } else {
      // Query for a reference.
      var token = referenceTokens.isNotEmpty
          ? referenceTokens[queryWithRead.read.value as String]
          : null;
      if (token != null) {
        var providerSource = _providers.get(token);
        if (providerSource != null) {
          value = providerSource.build();
          changeDetectorRef = providerSource.buildChangeDetectorRef();
        }
      } else {
        // If we can't find a valid query type, then we fall back to
        // ElementRef. HOWEVER, if specifically typed as Element or
        // HtmlElement, use that.
        value = queryWithRead.query.metadata.isElementType
            ? renderNode.toReadExpr()
            : elementRef;
      }
    }

    if (value != null) {
      queryWithRead.query.addQueryResult(view!, value, changeDetectorRef);
    }
  }
}