Repository: facebook/react
File: packages/react-server/src/forks/ReactServerStreamConfig.dom-fb-experimental.js (JavaScript)

Code before:
  if (byteLengthImpl == null) {
    // eslint-disable-next-line react-internal/prod-error-codes
    throw new Error(
      'byteLengthOfChunk implementation is not configured. Please, provide the implementation via ReactFlightDOMServer.setConfig(...);',
    );
  }
  return byteLengthImpl(chunk);
}

export interface Destination {
  beginWriting(): void;
  write(chunk: Chunk | PrecomputedChunk | BinaryChunk): void;
  completeWriting(): void;
  flushBuffered(): void;
  close(): void;
  onError(error: mixed): void;
}

function handleErrorInNextTick(error: any) {
  setTimeout(() => {
    throw error;
  });
}

export const scheduleMicrotask: (callback: () => void) => void =
  typeof queueMicrotask === 'function'
    ? queueMicrotask
    : callback => {
        Promise.resolve(null).then(callback).catch(handleErrorInNextTick);
      };

export function scheduleWork(callback: () => void) {
  callback();
}

export function beginWriting(destination: Destination) {
  destination.beginWriting();
}

export function writeChunk(
  destination: Destination,
  chunk: Chunk | PrecomputedChunk | BinaryChunk,
): void {
  destination.write(chunk);
}

export function writeChunkAndReturn(
  destination: Destination,
  chunk: Chunk | PrecomputedChunk | BinaryChunk,
): boolean {
  destination.write(chunk);

Review comment (suggestion):
Not sure about Meta's plans here. Similar to the legacy case above the schedule work is sync (why?) so the microtask scheduler should probably also be sync. Unlike with legacy I can't error b/c flight is supported here. 

That said, sync isn't really desirable outside the legacy case for renderToString so I think we should just make scheduleWork use whatever schedule is preferable for Meta

Code after:
  if (byteLengthImpl == null) {
    // eslint-disable-next-line react-internal/prod-error-codes
    throw new Error(
      'byteLengthOfChunk implementation is not configured. Please, provide the implementation via ReactFlightDOMServer.setConfig(...);',
    );
  }
  return byteLengthImpl(chunk);
}

export interface Destination {
  beginWriting(): void;
  write(chunk: Chunk | PrecomputedChunk | BinaryChunk): void;
  completeWriting(): void;
  flushBuffered(): void;
  close(): void;
  onError(error: mixed): void;
}

function handleErrorInNextTick(error: any) {
  setTimeout(() => {
    throw error;
  });
}

const LocalPromise = Promise;

/**
 * Since this environment doesn't have a way to schedule tasks from JS we schedule
 * using a microtask instead. This isn't necessarily ideal since we would like to give
 * other IO a chance to run before performing work typically but it's the best we can
 * do in this environment
 */
export function scheduleWork(callback: () => void) {
  LocalPromise.resolve().then(callback).catch(handleErrorInNextTick);
}

export const scheduleMicrotask: (callback: () => void) => void = scheduleWork;

export function beginWriting(destination: Destination) {
  destination.beginWriting();
}

export function writeChunk(
  destination: Destination,
  chunk: Chunk | PrecomputedChunk | BinaryChunk,
): void {
  destination.write(chunk);
}

export function writeChunkAndReturn(
  destination: Destination,