addQueryResult method

void addQueryResult(
  1. CompileView origin,
  2. Expression result,
  3. Expression? changeDetectorRef
)

Adds an expression result that originates from an origin view.

The result of a given query is the sum of all invocations to this method. Some of the expressions are simple (i.e. reads from an existing class field) and others require proxy-ing through mapNestedViews in order to determine what <template>s are currently active.

If result is a component instance that uses the OnPush change detection strategy, changeDetectorRef should be its associated ChangeDetectorRef instance. Otherwise this parameter should be null.

Implementation

void addQueryResult(
  CompileView origin,
  o.Expression result,
  o.Expression? changeDetectorRef,
) {
  // Determine if we have a path of embedded templates.
  final elementPath = _resolvePathToRoot(origin);
  var viewValues = _values;

  // If we do, then continue building QueryValues, a tree-like data structure.
  for (final element in elementPath) {
    final valuesOrTemplates = viewValues.valuesOrTemplates;
    final last = valuesOrTemplates.isNotEmpty ? valuesOrTemplates.last : null;
    if (last is _NestedQueryValues && last.view == element.embeddedView) {
      viewValues = last;
    } else {
      assert(element.hasEmbeddedView);
      final newViewValues = _NestedQueryValues(element.embeddedView);
      valuesOrTemplates.add(newViewValues);
      viewValues = newViewValues;
    }
  }

  // Add it to the applicable part of the view (either root or embedded).
  viewValues.valuesOrTemplates.add(_QueryValue(result, changeDetectorRef));

  // Finally, if this result doesn't come from the root, it means that some
  // change in an embedded view needs to invalidate the state of the previous
  // query.
  if (elementPath.isNotEmpty) {
    _setParentQueryAsDirty(origin);
  }
}