Skip to content

Commit

Permalink
[Fizz] Use RequestInstance constructor for resuming (#30947)
Browse files Browse the repository at this point in the history
We added enough fields to need a constructor instead of inline object in
V8.

We didn't update the resumeRequest path though so it wasn't using the
constructor and had a different hidden class.
  • Loading branch information
sebmarkbage authored Sep 11, 2024
1 parent 344bc81 commit 1bb0563
Showing 1 changed file with 54 additions and 67 deletions.
121 changes: 54 additions & 67 deletions packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ function noop(): void {}

function RequestInstance(
this: $FlowFixMe,
children: ReactNodeList,
resumableState: ResumableState,
renderState: RenderState,
rootFormatContext: FormatContext,
Expand Down Expand Up @@ -447,38 +446,6 @@ function RequestInstance(
if (__DEV__) {
this.didWarnForKey = null;
}
// This segment represents the root fallback.
const rootSegment = createPendingSegment(
this,
0,
null,
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 = createRenderTask(
this,
null,
children,
-1,
null,
rootSegment,
null,
abortSet,
null,
rootFormatContext,
rootContextSnapshot,
emptyTreeContext,
null,
false,
emptyContextObject,
null,
);
pushComponentStack(rootTask);
pingedTasks.push(rootTask);
}

export function createRequest(
Expand All @@ -496,8 +463,7 @@ export function createRequest(
formState: void | null | ReactFormState<any, any>,
): Request {
// $FlowFixMe[invalid-constructor]: the shapes are exact here but Flow doesn't like constructors
return new RequestInstance(
children,
const request: Request = new RequestInstance(
resumableState,
renderState,
rootFormatContext,
Expand All @@ -510,6 +476,40 @@ export function createRequest(
onPostpone,
formState,
);

// This segment represents the root fallback.
const rootSegment = createPendingSegment(
request,
0,
null,
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 = createRenderTask(
request,
null,
children,
-1,
null,
rootSegment,
null,
request.abortableTasks,
null,
rootFormatContext,
rootContextSnapshot,
emptyTreeContext,
null,
false,
emptyContextObject,
null,
);
pushComponentStack(rootTask);
request.pingedTasks.push(rootTask);
return request;
}

export function createPrerenderRequest(
Expand Down Expand Up @@ -559,35 +559,22 @@ export function resumeRequest(
onFatalError: void | ((error: mixed) => void),
onPostpone: void | ((reason: string, postponeInfo: PostponeInfo) => void),
): Request {
const pingedTasks: Array<Task> = [];
const abortSet: Set<Task> = new Set();
const request: Request = {
destination: null,
flushScheduled: false,
resumableState: postponedState.resumableState,
// $FlowFixMe[invalid-constructor]: the shapes are exact here but Flow doesn't like constructors
const request: Request = new RequestInstance(
postponedState.resumableState,
renderState,
rootFormatContext: postponedState.rootFormatContext,
progressiveChunkSize: postponedState.progressiveChunkSize,
status: OPEN,
fatalError: null,
nextSegmentId: postponedState.nextSegmentId,
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,
formState: null,
};
postponedState.rootFormatContext,
postponedState.progressiveChunkSize,
onError,
onAllReady,
onShellReady,
onShellError,
onFatalError,
onPostpone,
null,
);
request.nextSegmentId = postponedState.nextSegmentId;

if (typeof postponedState.replaySlots === 'number') {
const resumedId = postponedState.replaySlots;
// We have a resume slot at the very root. This is effectively just a full rerender.
Expand All @@ -611,7 +598,7 @@ export function resumeRequest(
null,
rootSegment,
null,
abortSet,
request.abortableTasks,
null,
postponedState.rootFormatContext,
rootContextSnapshot,
Expand All @@ -622,7 +609,7 @@ export function resumeRequest(
null,
);
pushComponentStack(rootTask);
pingedTasks.push(rootTask);
request.pingedTasks.push(rootTask);
return request;
}

Expand All @@ -639,7 +626,7 @@ export function resumeRequest(
-1,
null,
null,
abortSet,
request.abortableTasks,
null,
postponedState.rootFormatContext,
rootContextSnapshot,
Expand All @@ -650,7 +637,7 @@ export function resumeRequest(
null,
);
pushComponentStack(rootTask);
pingedTasks.push(rootTask);
request.pingedTasks.push(rootTask);
return request;
}

Expand Down

0 comments on commit 1bb0563

Please sign in to comment.