Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flight] Rename Segment to Task #24753

Merged
merged 1 commit into from
Jun 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export type ReactModel =

type ReactModelObject = {+[key: string]: ReactModel};

type Segment = {
type Task = {
id: number,
model: ReactModel,
ping: () => void,
Expand All @@ -101,7 +101,7 @@ export type Request = {
cache: Map<Function, mixed>,
nextChunkId: number,
pendingChunks: number,
pingedSegments: Array<Segment>,
pingedTasks: Array<Task>,
completedModuleChunks: Array<Chunk>,
completedJSONChunks: Array<Chunk>,
completedErrorChunks: Array<Chunk>,
Expand Down Expand Up @@ -132,7 +132,7 @@ export function createRequest(
context?: Array<[string, ServerContextJSONValue]>,
identifierPrefix?: string,
): Request {
const pingedSegments = [];
const pingedTasks = [];
const request = {
status: OPEN,
fatalError: null,
Expand All @@ -141,7 +141,7 @@ export function createRequest(
cache: new Map(),
nextChunkId: 0,
pendingChunks: 0,
pingedSegments: pingedSegments,
pingedTasks: pingedTasks,
completedModuleChunks: [],
completedJSONChunks: [],
completedErrorChunks: [],
Expand All @@ -157,8 +157,8 @@ export function createRequest(
};
request.pendingChunks++;
const rootContext = createRootContext(context);
const rootSegment = createSegment(request, model, rootContext);
pingedSegments.push(rootSegment);
const rootTask = createTask(request, model, rootContext);
pingedTasks.push(rootTask);
return request;
}

Expand Down Expand Up @@ -251,27 +251,27 @@ function attemptResolveElement(
);
}

function pingSegment(request: Request, segment: Segment): void {
const pingedSegments = request.pingedSegments;
pingedSegments.push(segment);
if (pingedSegments.length === 1) {
function pingTask(request: Request, task: Task): void {
const pingedTasks = request.pingedTasks;
pingedTasks.push(task);
if (pingedTasks.length === 1) {
scheduleWork(() => performWork(request));
}
}

function createSegment(
function createTask(
request: Request,
model: ReactModel,
context: ContextSnapshot,
): Segment {
): Task {
const id = request.nextChunkId++;
const segment = {
const task = {
id,
model,
context,
ping: () => pingSegment(request, segment),
ping: () => pingTask(request, task),
};
return segment;
return task;
}

function serializeByValueID(id: number): string {
Expand Down Expand Up @@ -518,12 +518,12 @@ export function resolveModelToJSON(
}
} catch (x) {
if (typeof x === 'object' && x !== null && typeof x.then === 'function') {
// Something suspended, we'll need to create a new segment and resolve it later.
// Something suspended, we'll need to create a new task and resolve it later.
request.pendingChunks++;
const newSegment = createSegment(request, value, getActiveContext());
const ping = newSegment.ping;
const newTask = createTask(request, value, getActiveContext());
const ping = newTask.ping;
x.then(ping, ping);
return serializeByRefID(newSegment.id);
return serializeByRefID(newTask.id);
} else {
logRecoverableError(request, x);
// Something errored. We'll still send everything we have up until this point.
Expand Down Expand Up @@ -790,10 +790,10 @@ function emitProviderChunk(
request.completedJSONChunks.push(processedChunk);
}

function retrySegment(request: Request, segment: Segment): void {
switchContext(segment.context);
function retryTask(request: Request, task: Task): void {
switchContext(task.context);
try {
let value = segment.model;
let value = task.model;
while (
typeof value === 'object' &&
value !== null &&
Expand All @@ -802,28 +802,28 @@ function retrySegment(request: Request, segment: Segment): void {
// TODO: Concatenate keys of parents onto children.
const element: React$Element<any> = (value: any);
// Attempt to render the server component.
// Doing this here lets us reuse this same segment if the next component
// Doing this here lets us reuse this same task if the next component
// also suspends.
segment.model = value;
task.model = value;
value = attemptResolveElement(
element.type,
element.key,
element.ref,
element.props,
);
}
const processedChunk = processModelChunk(request, segment.id, value);
const processedChunk = processModelChunk(request, task.id, value);
request.completedJSONChunks.push(processedChunk);
} catch (x) {
if (typeof x === 'object' && x !== null && typeof x.then === 'function') {
// Something suspended again, let's pick it back up later.
const ping = segment.ping;
const ping = task.ping;
x.then(ping, ping);
return;
} else {
logRecoverableError(request, x);
// This errored, we need to serialize this error to the
emitErrorChunk(request, segment.id, x);
emitErrorChunk(request, task.id, x);
}
}
}
Expand All @@ -836,11 +836,11 @@ function performWork(request: Request): void {
prepareToUseHooksForRequest(request);

try {
const pingedSegments = request.pingedSegments;
request.pingedSegments = [];
for (let i = 0; i < pingedSegments.length; i++) {
const segment = pingedSegments[i];
retrySegment(request, segment);
const pingedTasks = request.pingedTasks;
request.pingedTasks = [];
for (let i = 0; i < pingedTasks.length; i++) {
const task = pingedTasks[i];
retryTask(request, task);
}
if (request.destination !== null) {
flushCompletedChunks(request, request.destination);
Expand Down