Skip to content

Commit

Permalink
Split createRequest into createRequest, createPrerenderRequest and re…
Browse files Browse the repository at this point in the history
…sumeRequest

This is what encodes what type of work we'll do.
  • Loading branch information
sebmarkbage committed Sep 7, 2023
1 parent ccb8c47 commit c4459cb
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 24 deletions.
7 changes: 3 additions & 4 deletions packages/react-dom/src/server/ReactDOMFizzServerBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ReactVersion from 'shared/ReactVersion';

import {
createRequest,
resumeRequest,
startWork,
startFlowing,
abort,
Expand Down Expand Up @@ -171,16 +172,14 @@ function resume(
allReady.catch(() => {});
reject(error);
}
const request = createRequest(
const request = resumeRequest(
children,
postponedState.resumableState,
postponedState,
createRenderState(
postponedState.resumableState,
options ? options.nonce : undefined,
undefined, // importMap
),
postponedState.rootFormatContext,
postponedState.progressiveChunkSize,
options ? options.onError : undefined,
onAllReady,
onShellReady,
Expand Down
7 changes: 3 additions & 4 deletions packages/react-dom/src/server/ReactDOMFizzServerEdge.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ReactVersion from 'shared/ReactVersion';

import {
createRequest,
resumeRequest,
startWork,
startFlowing,
abort,
Expand Down Expand Up @@ -171,16 +172,14 @@ function resume(
allReady.catch(() => {});
reject(error);
}
const request = createRequest(
const request = resumeRequest(
children,
postponedState.resumableState,
postponedState,
createRenderState(
postponedState.resumableState,
options ? options.nonce : undefined,
undefined, // importMap
),
postponedState.rootFormatContext,
postponedState.progressiveChunkSize,
options ? options.onError : undefined,
onAllReady,
onShellReady,
Expand Down
7 changes: 3 additions & 4 deletions packages/react-dom/src/server/ReactDOMFizzServerNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ReactVersion from 'shared/ReactVersion';

import {
createRequest,
resumeRequest,
startWork,
startFlowing,
abort,
Expand Down Expand Up @@ -140,16 +141,14 @@ function resumeRequestImpl(
postponedState: PostponedState,
options: void | ResumeOptions,
) {
return createRequest(
return resumeRequest(
children,
postponedState.resumableState,
postponedState,
createRenderState(
postponedState.resumableState,
options ? options.nonce : undefined,
undefined, // importMap
),
postponedState.rootFormatContext,
postponedState.progressiveChunkSize,
options ? options.onError : undefined,
options ? options.onAllReady : undefined,
options ? options.onShellReady : undefined,
Expand Down
4 changes: 2 additions & 2 deletions packages/react-dom/src/server/ReactDOMFizzStaticBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {ImportMap} from '../shared/ReactDOMTypes';
import ReactVersion from 'shared/ReactVersion';

import {
createRequest,
createPrerenderRequest,
startWork,
startFlowing,
abort,
Expand Down Expand Up @@ -80,7 +80,7 @@ function prerender(
options ? options.bootstrapModules : undefined,
options ? options.unstable_externalRuntimeSrc : undefined,
);
const request = createRequest(
const request = createPrerenderRequest(
children,
resources,
createRenderState(
Expand Down
4 changes: 2 additions & 2 deletions packages/react-dom/src/server/ReactDOMFizzStaticEdge.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {ImportMap} from '../shared/ReactDOMTypes';
import ReactVersion from 'shared/ReactVersion';

import {
createRequest,
createPrerenderRequest,
startWork,
startFlowing,
abort,
Expand Down Expand Up @@ -80,7 +80,7 @@ function prerender(
options ? options.bootstrapModules : undefined,
options ? options.unstable_externalRuntimeSrc : undefined,
);
const request = createRequest(
const request = createPrerenderRequest(
children,
resources,
createRenderState(
Expand Down
4 changes: 2 additions & 2 deletions packages/react-dom/src/server/ReactDOMFizzStaticNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {Writable, Readable} from 'stream';
import ReactVersion from 'shared/ReactVersion';

import {
createRequest,
createPrerenderRequest,
startWork,
startFlowing,
abort,
Expand Down Expand Up @@ -94,7 +94,7 @@ function prerenderToNodeStream(
options ? options.bootstrapModules : undefined,
options ? options.unstable_externalRuntimeSrc : undefined,
);
const request = createRequest(
const request = createPrerenderRequest(
children,
resumableState,
createRenderState(
Expand Down
106 changes: 100 additions & 6 deletions packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,106 @@ export function createRequest(
return request;
}

export function createPrerenderRequest(
children: ReactNodeList,
resumableState: ResumableState,
renderState: RenderState,
rootFormatContext: FormatContext,
progressiveChunkSize: void | number,
onError: void | ((error: mixed) => ?string),
onAllReady: void | (() => void),
onShellReady: void | (() => void),
onShellError: void | ((error: mixed) => void),
onFatalError: void | ((error: mixed) => void),
onPostpone: void | ((reason: string) => void),
): Request {
const request = createRequest(
children,
resumableState,
renderState,
rootFormatContext,
progressiveChunkSize,
onError,
onAllReady,
onShellReady,
onShellError,
onFatalError,
onPostpone,
);
// Start tracking postponed holes during this render.
request.trackedPostpones = {workingMap: new Map(), root: []};
return request;
}

export function resumeRequest(
children: ReactNodeList,
postponedState: PostponedState,
renderState: RenderState,
onError: void | ((error: mixed) => ?string),
onAllReady: void | (() => void),
onShellReady: void | (() => void),
onShellError: void | ((error: mixed) => void),
onFatalError: void | ((error: mixed) => void),
onPostpone: void | ((reason: string) => void),
): Request {
prepareHostDispatcher();
const pingedTasks: Array<Task> = [];
const abortSet: Set<Task> = new Set();
const request: Request = {
destination: null,
flushScheduled: false,
resumableState: postponedState.resumableState,
renderState,
rootFormatContext: postponedState.rootFormatContext,
progressiveChunkSize: postponedState.progressiveChunkSize,
status: OPEN,
fatalError: null,
nextSegmentId: 0,
allPendingTasks: 0,
pendingRootTasks: 0,
completedRootSegment: null,
abortableTasks: abortSet,
pingedTasks: pingedTasks,
clientRenderedBoundaries: ([]: Array<SuspenseBoundary>),
completedBoundaries: ([]: Array<SuspenseBoundary>),
partialBoundaries: ([]: Array<SuspenseBoundary>),
trackedPostpones: null,
onError: onError === undefined ? defaultErrorHandler : onError,
onPostpone: onPostpone === undefined ? noop : onPostpone,
onAllReady: onAllReady === undefined ? noop : onAllReady,
onShellReady: onShellReady === undefined ? noop : onShellReady,
onShellError: onShellError === undefined ? noop : onShellError,
onFatalError: onFatalError === undefined ? noop : onFatalError,
};
// This segment represents the root fallback.
const rootSegment = createPendingSegment(
request,
0,
null,
postponedState.rootFormatContext,
// Root segments are never embedded in Text on either edge
false,
false,
);
// There is no parent so conceptually, we're unblocked to flush this segment.
rootSegment.parentFlushed = true;
const rootTask = createTask(
request,
null,
children,
null,
rootSegment,
abortSet,
null,
postponedState.rootFormatContext,
emptyContextObject,
rootContextSnapshot,
emptyTreeContext,
);
pingedTasks.push(rootTask);
return request;
}

let currentRequest: null | Request = null;

export function resolveRequest(): null | Request {
Expand Down Expand Up @@ -2760,12 +2860,6 @@ export function startWork(request: Request): void {
}
}

export function startPrerenderWork(request: Request): void {
// Start tracking postponed holes during this render.
request.trackedPostpones = {workingMap: new Map(), root: []};
startWork(request);
}

function enqueueFlush(request: Request): void {
if (
request.flushScheduled === false &&
Expand Down

0 comments on commit c4459cb

Please sign in to comment.