Repository: angular/angular
File: packages/compiler/src/render3/view/query_generation.ts (TypeScript)

Code before:
}

// Define and update any view queries
export function createViewQueriesFunction(
  viewQueries: R3QueryMetadata[],
  constantPool: ConstantPool,
  name?: string,
): o.Expression {
  const createStatements: o.Statement[] = [];
  const updateStatements: (o.Statement | typeof queryAdvancePlaceholder)[] = [];
  const tempAllocator = temporaryAllocator((st) => updateStatements.push(st), TEMPORARY_NAME);
  let viewQueryCall: o.Expression | null = null;
  let viewQuerySignalCall: o.Expression | null = null;

  viewQueries.forEach((query: R3QueryMetadata) => {
    // creation call, e.g. r3.viewQuery(somePredicate, true) or
    //                r3.viewQuerySignal(ctx.prop, somePredicate, true);
    const params = getQueryCreateParameters(query, constantPool);

    if (query.isSignal) {
      viewQueryCall ??= o.importExpr(R3.viewQuerySignal);
      viewQueryCall = viewQueryCall.callFn(params);
    } else {
      viewQuerySignalCall ??= o.importExpr(R3.viewQuery);
      viewQuerySignalCall = viewQuerySignalCall.callFn(params);
    }

    // Signal queries update lazily and we just advance the index.
    if (query.isSignal) {
      updateStatements.push(queryAdvancePlaceholder);
      return;
    }

    // update, e.g. (r3.queryRefresh(tmp = r3.loadQuery()) && (ctx.someDir = tmp));
    const temporary = tempAllocator();
    const getQueryList = o.importExpr(R3.loadQuery).callFn([]);
    const refresh = o.importExpr(R3.queryRefresh).callFn([temporary.set(getQueryList)]);
    const updateDirective = o
      .variable(CONTEXT_NAME)
      .prop(query.propertyName)
      .set(query.first ? temporary.prop('first') : temporary);
    updateStatements.push(refresh.and(updateDirective).toStmt());
  });

  if (viewQueryCall !== null) {
    createStatements.push(new o.ExpressionStatement(viewQueryCall));
  }

  if (viewQuerySignalCall !== null) {
    createStatements.push(new o.ExpressionStatement(viewQuerySignalCall));
  }

Review comment (suggestion):
The `viewQueryCall`/`viewQuerySignalCall` assignments have been swapped here, as well as for content queries below.

Code after:
}

// Define and update any view queries
export function createViewQueriesFunction(
  viewQueries: R3QueryMetadata[],
  constantPool: ConstantPool,
  name?: string,
): o.Expression {
  const createStatements: o.Statement[] = [];
  const updateStatements: (o.Statement | typeof queryAdvancePlaceholder)[] = [];
  const tempAllocator = temporaryAllocator((st) => updateStatements.push(st), TEMPORARY_NAME);
  let viewQuerySignalCall: o.Expression | null = null;
  let viewQueryCall: o.Expression | null = null;

  viewQueries.forEach((query: R3QueryMetadata) => {
    // creation call, e.g. r3.viewQuery(somePredicate, true) or
    //                r3.viewQuerySignal(ctx.prop, somePredicate, true);
    const params = getQueryCreateParameters(query, constantPool);

    if (query.isSignal) {
      viewQuerySignalCall ??= o.importExpr(R3.viewQuerySignal);
      viewQuerySignalCall = viewQuerySignalCall.callFn(params);
    } else {
      viewQueryCall ??= o.importExpr(R3.viewQuery);
      viewQueryCall = viewQueryCall.callFn(params);
    }

    // Signal queries update lazily and we just advance the index.
    if (query.isSignal) {
      updateStatements.push(queryAdvancePlaceholder);
      return;
    }

    // update, e.g. (r3.queryRefresh(tmp = r3.loadQuery()) && (ctx.someDir = tmp));
    const temporary = tempAllocator();
    const getQueryList = o.importExpr(R3.loadQuery).callFn([]);
    const refresh = o.importExpr(R3.queryRefresh).callFn([temporary.set(getQueryList)]);
    const updateDirective = o
      .variable(CONTEXT_NAME)
      .prop(query.propertyName)
      .set(query.first ? temporary.prop('first') : temporary);
    updateStatements.push(refresh.and(updateDirective).toStmt());
  });

  if (viewQuerySignalCall !== null) {
    createStatements.push(new o.ExpressionStatement(viewQuerySignalCall));
  }

  if (viewQueryCall !== null) {
    createStatements.push(new o.ExpressionStatement(viewQueryCall));
  }