@samitouri / QOS-React-2 / commits / f796fa13ad

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.

Sebastian Markbåge committed Jun 18, 2022 at 15:02 UTC f796fa13ad13ee9939cb7847bfa90ada860068b2
1 file changed +32 -32
packages/react-server/src/ReactFlightServer.js
+32 -32
@@ -86,7 +86,7 @@ export type ReactModel =
86
87 type ReactModelObject = {+[key: string]: ReactModel};
88
89 -type Segment = {
89 +type Task = {
90 id: number,
91 model: ReactModel,
92 ping: () => void,
@@ -101,7 +101,7 @@ export type Request = {
101 cache: Map<Function, mixed>,
102 nextChunkId: number,
103 pendingChunks: number,
104 - pingedSegments: Array<Segment>,
104 + pingedTasks: Array<Task>,
105 completedModuleChunks: Array<Chunk>,
106 completedJSONChunks: Array<Chunk>,
107 completedErrorChunks: Array<Chunk>,
@@ -132,7 +132,7 @@ export function createRequest(
132 context?: Array<[string, ServerContextJSONValue]>,
133 identifierPrefix?: string,
134 ): Request {
135 - const pingedSegments = [];
135 + const pingedTasks = [];
136 const request = {
137 status: OPEN,
138 fatalError: null,
@@ -141,7 +141,7 @@ export function createRequest(
141 cache: new Map(),
142 nextChunkId: 0,
143 pendingChunks: 0,
144 - pingedSegments: pingedSegments,
144 + pingedTasks: pingedTasks,
145 completedModuleChunks: [],
146 completedJSONChunks: [],
147 completedErrorChunks: [],
@@ -157,8 +157,8 @@ export function createRequest(
157 };
158 request.pendingChunks++;
159 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);
162 return request;
163 }
164
@@ -251,27 +251,27 @@ function attemptResolveElement(
251 );
252 }
253
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) {
258 scheduleWork(() => performWork(request));
259 }
260 }
261
262 -function createSegment(
262 +function createTask(
263 request: Request,
264 model: ReactModel,
265 context: ContextSnapshot,
266 -): Segment {
266 +): Task {
267 const id = request.nextChunkId++;
268 - const segment = {
268 + const task = {
269 id,
270 model,
271 context,
272 - ping: () => pingSegment(request, segment),
272 + ping: () => pingTask(request, task),
273 };
274 - return segment;
274 + return task;
275 }
276
277 function serializeByValueID(id: number): string {
@@ -518,12 +518,12 @@ export function resolveModelToJSON(
518 }
519 } catch (x) {
520 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.
522 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;
525 x.then(ping, ping);
526 - return serializeByRefID(newSegment.id);
526 + return serializeByRefID(newTask.id);
527 } else {
528 logRecoverableError(request, x);
529 // Something errored. We'll still send everything we have up until this point.
@@ -790,10 +790,10 @@ function emitProviderChunk(
790 request.completedJSONChunks.push(processedChunk);
791 }
792
793 -function retrySegment(request: Request, segment: Segment): void {
794 - switchContext(segment.context);
793 +function retryTask(request: Request, task: Task): void {
794 + switchContext(task.context);
795 try {
796 - let value = segment.model;
796 + let value = task.model;
797 while (
798 typeof value === 'object' &&
799 value !== null &&
@@ -802,9 +802,9 @@ function retrySegment(request: Request, segment: Segment): void {
802 // TODO: Concatenate keys of parents onto children.
803 const element: React$Element<any> = (value: any);
804 // 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
806 // also suspends.
807 - segment.model = value;
807 + task.model = value;
808 value = attemptResolveElement(
809 element.type,
810 element.key,
@@ -812,18 +812,18 @@ function retrySegment(request: Request, segment: Segment): void {
812 element.props,
813 );
814 }
815 - const processedChunk = processModelChunk(request, segment.id, value);
815 + const processedChunk = processModelChunk(request, task.id, value);
816 request.completedJSONChunks.push(processedChunk);
817 } catch (x) {
818 if (typeof x === 'object' && x !== null && typeof x.then === 'function') {
819 // Something suspended again, let's pick it back up later.
820 - const ping = segment.ping;
820 + const ping = task.ping;
821 x.then(ping, ping);
822 return;
823 } else {
824 logRecoverableError(request, x);
825 // This errored, we need to serialize this error to the
826 - emitErrorChunk(request, segment.id, x);
826 + emitErrorChunk(request, task.id, x);
827 }
828 }
829 }
@@ -836,11 +836,11 @@ function performWork(request: Request): void {
836 prepareToUseHooksForRequest(request);
837
838 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);
844 }
845 if (request.destination !== null) {
846 flushCompletedChunks(request, request.destination);