writeChangeDetectionStatements method

List<Statement> writeChangeDetectionStatements()

Implementation

List<o.Statement> writeChangeDetectionStatements() {
  var statements = <o.Statement>[];
  if (detectChangesInInputsMethod.isEmpty &&
      _updateContentQueriesMethod.isEmpty &&
      afterContentLifecycleCallbacksMethod.isEmpty &&
      detectChangesRenderPropertiesMethod.isEmpty &&
      _updateViewQueriesMethod.isEmpty &&
      afterViewLifecycleCallbacksMethod.isEmpty &&
      // Host views have a default implementation of `detectChangesInternal()`
      // that change detects their only child component view, so the presence
      // of child views is only an indicator for generating change detection
      // statements for component and embedded views.
      (viewType == ViewType.host || viewChildren.isEmpty) &&
      viewContainers.isEmpty) {
    return statements;
  }

  // Declare variables for locals used in this method.
  statements.addAll(nameResolver.getLocalDeclarations());

  // Add @Input change detectors.
  statements.addAll(detectChangesInInputsMethod.finish());

  // Add content child change detection calls.
  for (var contentChild in viewContainers) {
    statements.add(
        contentChild.callMethod('detectChangesInNestedViews', []).toStmt());
  }

  // Add Content query updates.
  var afterContentStmts =
      List<o.Statement>.from(_updateContentQueriesMethod.finish())
        ..addAll(afterContentLifecycleCallbacksMethod.finish());
  if (afterContentStmts.isNotEmpty) {
    statements.add(o.IfStmt(notThrowOnChanges, afterContentStmts));
  }

  // Add render properties change detectors.
  statements.addAll(detectChangesRenderPropertiesMethod.finish());

  // Add view child change detection calls.
  for (var viewChild in viewChildren) {
    statements.add(
        viewChild.componentView!.callMethod('detectChanges', []).toStmt());
  }

  var afterViewStmts =
      List<o.Statement>.from(_updateViewQueriesMethod.finish())
        ..addAll(afterViewLifecycleCallbacksMethod.finish());
  if (afterViewStmts.isNotEmpty) {
    statements.add(o.IfStmt(notThrowOnChanges, afterViewStmts));
  }
  var varStmts = <Object>[];
  var readVars = o.findReadVarNames(statements);
  var writeVars = o.findWriteVarNames(statements);
  varStmts.addAll(maybeCachedCtxDeclarationStatement(readVars: readVars));

  if (readVars.contains(DetectChangesVars.changed.name) ||
      writeVars.contains(DetectChangesVars.changed.name)) {
    varStmts.add(DetectChangesVars.changed
        .set(o.literal(false))
        .toDeclStmt(o.BOOL_TYPE));
  }
  if (readVars.contains(DetectChangesVars.firstCheck.name)) {
    varStmts.add(o.DeclareVarStmt(DetectChangesVars.firstCheck.name!,
        o.THIS_EXPR.prop('firstCheck'), o.BOOL_TYPE));
  }
  return List.from(varStmts)..addAll(statements);
}