Repository: facebook/react
File: packages/react-server/src/ReactFlightServer.js (JavaScript)

Code before:
  });
}

function enqueueFlush(request: Request): void {
  if (
    request.flushScheduled === false &&
    // If there are pinged tasks we are going to flush anyway after work completes
    request.pingedTasks.length === 0 &&
    // If there is no destination there is nothing we can flush to. A flush will
    // happen when we start flowing again
    request.destination !== null
  ) {
    request.flushScheduled = true;
    // Unlike startWork and pingTask we intetionally use scheduleWork
    // here even during prerenders to allow as much batching as possible
    scheduleWork(() => {
      request.flushScheduled = false;
      const destination = request.destination;
      if (destination) {
        flushCompletedChunks(request, destination);
      }
    });
  }
}

function enqueueOnAllReady(request: Request): void {
  if (request.onAllReadyScheduled === false) {
    request.onAllReadyScheduled = true;

    const callback = () => {
      if (request.abortableTasks.size === 0) {
        const onAllReady = request.onAllReady;
        onAllReady();
      } else {
        request.onAllReadyScheduled = false;
      }
    };

    if (request.type === PRERENDER || request.status === OPENING) {
      scheduleMicrotask(callback);
    } else {
      scheduleWork(callback);
    }
  }
}

export function startFlowing(request: Request, destination: Destination): void {
  if (request.status === CLOSING) {
    request.status = CLOSED;
    closeWithError(destination, request.fatalError);
    return;

Review comment (suggestion):
I think this is unnecessary indirection.

every time we decrement our tasks via retryTask or erroredTask we can check whether we hit size zero and if we do, synchronously call onAllReady then. It's basically just the place where we set task status to COMPLETED in retryTask and the very end of erroredTask.

There are a couple decrement pathways that we hit during aborting but those actually don't need to do this because we already call onAllReady at the end of the abort function if there were any abortable tasks.

Code after:
}

function enqueueFlush(request: Request): void {
  if (
    request.flushScheduled === false &&
    // If there are pinged tasks we are going to flush anyway after work completes
    request.pingedTasks.length === 0 &&
    // If there is no destination there is nothing we can flush to. A flush will
    // happen when we start flowing again
    request.destination !== null
  ) {
    request.flushScheduled = true;
    // Unlike startWork and pingTask we intetionally use scheduleWork
    // here even during prerenders to allow as much batching as possible
    scheduleWork(() => {
      request.flushScheduled = false;
      const destination = request.destination;
      if (destination) {
        flushCompletedChunks(request, destination);
      }
    });
  }
}

function callOnAllReadyIfReady(request: Request): void {
  if (request.abortableTasks.size === 0 && request.abortListeners.size === 0) {
    request.onAllReady();
  }
}

export function startFlowing(request: Request, destination: Destination): void {
  if (request.status === CLOSING) {
    request.status = CLOSED;
    closeWithError(destination, request.fatalError);
    return;
  }
  if (request.status === CLOSED) {
    return;
  }
  if (request.destination !== null) {
    // We're already flowing.
    return;
  }
  request.destination = destination;
  try {
    flushCompletedChunks(request, destination);
  } catch (error) {
    logRecoverableError(request, error, null);
    fatalError(request, error);
  }
}