Skip to content

Commit f796fa1

Browse files
authored
Rename Segment to Task in Flight (#24753)
In Fizz this got split into Task and Segment. We don't have a concept of Segment in Flight yet because we don't inline multiple segments into one "Row". We just emit one "Row" for each Segment if something suspends. This makes Flight non-deterministic atm but that's something we'll want to address. Regardless, right now, this is more like a Task than a Segment.
1 parent 0f0aca3 commit f796fa1

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

packages/react-server/src/ReactFlightServer.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export type ReactModel =
8686

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

89-
type Segment = {
89+
type Task = {
9090
id: number,
9191
model: ReactModel,
9292
ping: () => void,
@@ -101,7 +101,7 @@ export type Request = {
101101
cache: Map<Function, mixed>,
102102
nextChunkId: number,
103103
pendingChunks: number,
104-
pingedSegments: Array<Segment>,
104+
pingedTasks: Array<Task>,
105105
completedModuleChunks: Array<Chunk>,
106106
completedJSONChunks: Array<Chunk>,
107107
completedErrorChunks: Array<Chunk>,
@@ -132,7 +132,7 @@ export function createRequest(
132132
context?: Array<[string, ServerContextJSONValue]>,
133133
identifierPrefix?: string,
134134
): Request {
135-
const pingedSegments = [];
135+
const pingedTasks = [];
136136
const request = {
137137
status: OPEN,
138138
fatalError: null,
@@ -141,7 +141,7 @@ export function createRequest(
141141
cache: new Map(),
142142
nextChunkId: 0,
143143
pendingChunks: 0,
144-
pingedSegments: pingedSegments,
144+
pingedTasks: pingedTasks,
145145
completedModuleChunks: [],
146146
completedJSONChunks: [],
147147
completedErrorChunks: [],
@@ -157,8 +157,8 @@ export function createRequest(
157157
};
158158
request.pendingChunks++;
159159
const rootContext = createRootContext(context);
160-
const rootSegment = createSegment(request, model, rootContext);
161-
pingedSegments.push(rootSegment);
160+
const rootTask = createTask(request, model, rootContext);
161+
pingedTasks.push(rootTask);
162162
return request;
163163
}
164164

@@ -251,27 +251,27 @@ function attemptResolveElement(
251251
);
252252
}
253253

254-
function pingSegment(request: Request, segment: Segment): void {
255-
const pingedSegments = request.pingedSegments;
256-
pingedSegments.push(segment);
257-
if (pingedSegments.length === 1) {
254+
function pingTask(request: Request, task: Task): void {
255+
const pingedTasks = request.pingedTasks;
256+
pingedTasks.push(task);
257+
if (pingedTasks.length === 1) {
258258
scheduleWork(() => performWork(request));
259259
}
260260
}
261261

262-
function createSegment(
262+
function createTask(
263263
request: Request,
264264
model: ReactModel,
265265
context: ContextSnapshot,
266-
): Segment {
266+
): Task {
267267
const id = request.nextChunkId++;
268-
const segment = {
268+
const task = {
269269
id,
270270
model,
271271
context,
272-
ping: () => pingSegment(request, segment),
272+
ping: () => pingTask(request, task),
273273
};
274-
return segment;
274+
return task;
275275
}
276276

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

793-
function retrySegment(request: Request, segment: Segment): void {
794-
switchContext(segment.context);
793+
function retryTask(request: Request, task: Task): void {
794+
switchContext(task.context);
795795
try {
796-
let value = segment.model;
796+
let value = task.model;
797797
while (
798798
typeof value === 'object' &&
799799
value !== null &&
@@ -802,28 +802,28 @@ function retrySegment(request: Request, segment: Segment): void {
802802
// TODO: Concatenate keys of parents onto children.
803803
const element: React$Element<any> = (value: any);
804804
// Attempt to render the server component.
805-
// Doing this here lets us reuse this same segment if the next component
805+
// Doing this here lets us reuse this same task if the next component
806806
// also suspends.
807-
segment.model = value;
807+
task.model = value;
808808
value = attemptResolveElement(
809809
element.type,
810810
element.key,
811811
element.ref,
812812
element.props,
813813
);
814814
}
815-
const processedChunk = processModelChunk(request, segment.id, value);
815+
const processedChunk = processModelChunk(request, task.id, value);
816816
request.completedJSONChunks.push(processedChunk);
817817
} catch (x) {
818818
if (typeof x === 'object' && x !== null && typeof x.then === 'function') {
819819
// Something suspended again, let's pick it back up later.
820-
const ping = segment.ping;
820+
const ping = task.ping;
821821
x.then(ping, ping);
822822
return;
823823
} else {
824824
logRecoverableError(request, x);
825825
// This errored, we need to serialize this error to the
826-
emitErrorChunk(request, segment.id, x);
826+
emitErrorChunk(request, task.id, x);
827827
}
828828
}
829829
}
@@ -836,11 +836,11 @@ function performWork(request: Request): void {
836836
prepareToUseHooksForRequest(request);
837837

838838
try {
839-
const pingedSegments = request.pingedSegments;
840-
request.pingedSegments = [];
841-
for (let i = 0; i < pingedSegments.length; i++) {
842-
const segment = pingedSegments[i];
843-
retrySegment(request, segment);
839+
const pingedTasks = request.pingedTasks;
840+
request.pingedTasks = [];
841+
for (let i = 0; i < pingedTasks.length; i++) {
842+
const task = pingedTasks[i];
843+
retryTask(request, task);
844844
}
845845
if (request.destination !== null) {
846846
flushCompletedChunks(request, request.destination);

0 commit comments

Comments
 (0)