Repository: facebook/react
File: packages/react-server-dom-turbopack/src/client/ReactFlightClientConfigBundlerTurbopack.js (JavaScript)

Code before:
  } else if (promise.status === 'fulfilled') {
    // This module was already resolved earlier.
    return null;
  } else {
    // Instrument the Promise to stash the result.
    promise.then(
      value => {
        const fulfilledThenable: FulfilledThenable<mixed> = (promise: any);
        fulfilledThenable.status = 'fulfilled';
        fulfilledThenable.value = value;
      },
      reason => {
        const rejectedThenable: RejectedThenable<mixed> = (promise: any);
        rejectedThenable.status = 'rejected';
        rejectedThenable.reason = reason;
      },
    );
    return promise;
  }
}

// Turbopack will return cached promises for the same chunk.
// We still want to keep track of which chunks we have already instrumented
// and which chunks have already been loaded.
const instrumentedChunks: WeakSet<string> = new WeakSet();
const loadedChunks: WeakSet<string> = new WeakSet();

function ignoreReject() {
  // We rely on rejected promises to be handled by another listener.
}
// Start preloading the modules since we might need them soon.
// This function doesn't suspend.
export function preloadModule<T>(
  metadata: ClientReference<T>,
): null | Thenable<any> {
  const chunks = metadata[CHUNKS];
  const promises = [];
  for (let i = 0; i < chunks.length; i++) {
    const chunkFilename = chunks[i];
    const thenable = loadChunk(chunkFilename);
    if (loadedChunks.has(thenable)) {
      promises.push(null);
    } else {
      promises.push(thenable);
    }

    if (!instrumentedChunks.has(thenable)) {
      // $FlowFixMe[method-unbinding]
      const resolve = loadedChunks.add.bind(loadedChunks, thenable);
      thenable.then(resolve, ignoreReject);
      instrumentedChunks.add(thenable);

Review comment (suggestion):
Question is we still need this or if we're ok with `Promise.all([Promise.resolve(undefined)])` instead of `Promise.all([null])`. Or we change Turbopack to return `null` if the chunk is already loaded.

Code after:
  } else if (promise.status === 'fulfilled') {
    // This module was already resolved earlier.
    return null;
  } else {
    // Instrument the Promise to stash the result.
    promise.then(
      value => {
        const fulfilledThenable: FulfilledThenable<mixed> = (promise: any);
        fulfilledThenable.status = 'fulfilled';
        fulfilledThenable.value = value;
      },
      reason => {
        const rejectedThenable: RejectedThenable<mixed> = (promise: any);
        rejectedThenable.status = 'rejected';
        rejectedThenable.reason = reason;
      },
    );
    return promise;
  }
}

// Turbopack will return cached promises for the same chunk.
// We still want to keep track of which chunks we have already instrumented
// and which chunks have already been loaded until Turbopack returns instrumented
// thenables directly.
const instrumentedChunks: WeakSet<Thenable<any>> = new WeakSet();
const loadedChunks: WeakSet<Thenable<any>> = new WeakSet();

function ignoreReject() {
  // We rely on rejected promises to be handled by another listener.
}
// Start preloading the modules since we might need them soon.
// This function doesn't suspend.
export function preloadModule<T>(
  metadata: ClientReference<T>,
): null | Thenable<any> {
  const chunks = metadata[CHUNKS];
  const promises: Promise<any>[] = [];
  for (let i = 0; i < chunks.length; i++) {
    const chunkFilename = chunks[i];
    const thenable = loadChunk(chunkFilename);
    if (!loadedChunks.has(thenable)) {
      promises.push(thenable);
    }

    if (!instrumentedChunks.has(thenable)) {
      // $FlowFixMe[method-unbinding]
      const resolve = loadedChunks.add.bind(loadedChunks, thenable);
      thenable.then(resolve, ignoreReject);
      instrumentedChunks.add(thenable);
    }