Skip to content

Commit

Permalink
Split writeChunk into void and return value (#23343)
Browse files Browse the repository at this point in the history
This function was modeled after Node streams where write returns a boolean
whether to keep writing or not. I think we should probably switch this
up and read desired size explicitly in appropriate places.

However, in the meantime, we don't have to return a value where we're
not going to use it. So I split this so that we call writeChunkAndReturn
if we're going to return the boolean.

This should help with the compilation so that they can be inlined.
  • Loading branch information
sebmarkbage authored Feb 23, 2022
1 parent 2c693b2 commit 4035157
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ let prevWasCommentSegmenter = false;
export function writeChunk(
destination: Destination,
chunk: Chunk | PrecomputedChunk,
): void {
writeChunkAndReturn(destination, chunk);
}

export function writeChunkAndReturn(
destination: Destination,
chunk: Chunk | PrecomputedChunk,
): boolean {
if (prevWasCommentSegmenter) {
prevWasCommentSegmenter = false;
Expand Down
60 changes: 32 additions & 28 deletions packages/react-dom/src/server/ReactDOMServerFormatConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {

import {
writeChunk,
writeChunkAndReturn,
stringToChunk,
stringToPrecomputedChunk,
} from 'react-server/src/ReactServerStreamConfig';
Expand Down Expand Up @@ -1427,11 +1428,14 @@ export function writeCompletedRoot(
responseState: ResponseState,
): boolean {
const bootstrapChunks = responseState.bootstrapChunks;
let result = true;
for (let i = 0; i < bootstrapChunks.length; i++) {
result = writeChunk(destination, bootstrapChunks[i]);
let i = 0;
for (; i < bootstrapChunks.length - 1; i++) {
writeChunk(destination, bootstrapChunks[i]);
}
return result;
if (i < bootstrapChunks.length) {
return writeChunkAndReturn(destination, bootstrapChunks[i]);
}
return true;
}

// Structural Nodes
Expand All @@ -1450,7 +1454,7 @@ export function writePlaceholder(
writeChunk(destination, responseState.placeholderPrefix);
const formattedID = stringToChunk(id.toString(16));
writeChunk(destination, formattedID);
return writeChunk(destination, placeholder2);
return writeChunkAndReturn(destination, placeholder2);
}

// Suspense boundaries are encoded as comments.
Expand Down Expand Up @@ -1480,7 +1484,7 @@ export function writeStartCompletedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
): boolean {
return writeChunk(destination, startCompletedSuspenseBoundary);
return writeChunkAndReturn(destination, startCompletedSuspenseBoundary);
}
export function writeStartPendingSuspenseBoundary(
destination: Destination,
Expand All @@ -1496,31 +1500,31 @@ export function writeStartPendingSuspenseBoundary(
}

writeChunk(destination, id);
return writeChunk(destination, startPendingSuspenseBoundary2);
return writeChunkAndReturn(destination, startPendingSuspenseBoundary2);
}
export function writeStartClientRenderedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
): boolean {
return writeChunk(destination, startClientRenderedSuspenseBoundary);
return writeChunkAndReturn(destination, startClientRenderedSuspenseBoundary);
}
export function writeEndCompletedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
): boolean {
return writeChunk(destination, endSuspenseBoundary);
return writeChunkAndReturn(destination, endSuspenseBoundary);
}
export function writeEndPendingSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
): boolean {
return writeChunk(destination, endSuspenseBoundary);
return writeChunkAndReturn(destination, endSuspenseBoundary);
}
export function writeEndClientRenderedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
): boolean {
return writeChunk(destination, endSuspenseBoundary);
return writeChunkAndReturn(destination, endSuspenseBoundary);
}

const startSegmentHTML = stringToPrecomputedChunk('<div hidden id="');
Expand Down Expand Up @@ -1571,25 +1575,25 @@ export function writeStartSegment(
writeChunk(destination, startSegmentHTML);
writeChunk(destination, responseState.segmentPrefix);
writeChunk(destination, stringToChunk(id.toString(16)));
return writeChunk(destination, startSegmentHTML2);
return writeChunkAndReturn(destination, startSegmentHTML2);
}
case SVG_MODE: {
writeChunk(destination, startSegmentSVG);
writeChunk(destination, responseState.segmentPrefix);
writeChunk(destination, stringToChunk(id.toString(16)));
return writeChunk(destination, startSegmentSVG2);
return writeChunkAndReturn(destination, startSegmentSVG2);
}
case MATHML_MODE: {
writeChunk(destination, startSegmentMathML);
writeChunk(destination, responseState.segmentPrefix);
writeChunk(destination, stringToChunk(id.toString(16)));
return writeChunk(destination, startSegmentMathML2);
return writeChunkAndReturn(destination, startSegmentMathML2);
}
case HTML_TABLE_MODE: {
writeChunk(destination, startSegmentTable);
writeChunk(destination, responseState.segmentPrefix);
writeChunk(destination, stringToChunk(id.toString(16)));
return writeChunk(destination, startSegmentTable2);
return writeChunkAndReturn(destination, startSegmentTable2);
}
// TODO: For the rest of these, there will be extra wrapper nodes that never
// get deleted from the document. We need to delete the table too as part
Expand All @@ -1599,19 +1603,19 @@ export function writeStartSegment(
writeChunk(destination, startSegmentTableBody);
writeChunk(destination, responseState.segmentPrefix);
writeChunk(destination, stringToChunk(id.toString(16)));
return writeChunk(destination, startSegmentTableBody2);
return writeChunkAndReturn(destination, startSegmentTableBody2);
}
case HTML_TABLE_ROW_MODE: {
writeChunk(destination, startSegmentTableRow);
writeChunk(destination, responseState.segmentPrefix);
writeChunk(destination, stringToChunk(id.toString(16)));
return writeChunk(destination, startSegmentTableRow2);
return writeChunkAndReturn(destination, startSegmentTableRow2);
}
case HTML_COLGROUP_MODE: {
writeChunk(destination, startSegmentColGroup);
writeChunk(destination, responseState.segmentPrefix);
writeChunk(destination, stringToChunk(id.toString(16)));
return writeChunk(destination, startSegmentColGroup2);
return writeChunkAndReturn(destination, startSegmentColGroup2);
}
default: {
throw new Error('Unknown insertion mode. This is a bug in React.');
Expand All @@ -1625,25 +1629,25 @@ export function writeEndSegment(
switch (formatContext.insertionMode) {
case ROOT_HTML_MODE:
case HTML_MODE: {
return writeChunk(destination, endSegmentHTML);
return writeChunkAndReturn(destination, endSegmentHTML);
}
case SVG_MODE: {
return writeChunk(destination, endSegmentSVG);
return writeChunkAndReturn(destination, endSegmentSVG);
}
case MATHML_MODE: {
return writeChunk(destination, endSegmentMathML);
return writeChunkAndReturn(destination, endSegmentMathML);
}
case HTML_TABLE_MODE: {
return writeChunk(destination, endSegmentTable);
return writeChunkAndReturn(destination, endSegmentTable);
}
case HTML_TABLE_BODY_MODE: {
return writeChunk(destination, endSegmentTableBody);
return writeChunkAndReturn(destination, endSegmentTableBody);
}
case HTML_TABLE_ROW_MODE: {
return writeChunk(destination, endSegmentTableRow);
return writeChunkAndReturn(destination, endSegmentTableRow);
}
case HTML_COLGROUP_MODE: {
return writeChunk(destination, endSegmentColGroup);
return writeChunkAndReturn(destination, endSegmentColGroup);
}
default: {
throw new Error('Unknown insertion mode. This is a bug in React.');
Expand Down Expand Up @@ -1790,7 +1794,7 @@ export function writeCompletedSegmentInstruction(
writeChunk(destination, completeSegmentScript2);
writeChunk(destination, responseState.placeholderPrefix);
writeChunk(destination, formattedID);
return writeChunk(destination, completeSegmentScript3);
return writeChunkAndReturn(destination, completeSegmentScript3);
}

const completeBoundaryScript1Full = stringToPrecomputedChunk(
Expand Down Expand Up @@ -1827,7 +1831,7 @@ export function writeCompletedBoundaryInstruction(
writeChunk(destination, completeBoundaryScript2);
writeChunk(destination, responseState.segmentPrefix);
writeChunk(destination, formattedContentID);
return writeChunk(destination, completeBoundaryScript3);
return writeChunkAndReturn(destination, completeBoundaryScript3);
}

const clientRenderScript1Full = stringToPrecomputedChunk(
Expand Down Expand Up @@ -1858,5 +1862,5 @@ export function writeClientRenderBoundaryInstruction(
}

writeChunk(destination, boundaryID);
return writeChunk(destination, clientRenderScript2);
return writeChunkAndReturn(destination, clientRenderScript2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {

import {
writeChunk,
writeChunkAndReturn,
stringToChunk,
stringToPrecomputedChunk,
} from 'react-server/src/ReactServerStreamConfig';
Expand Down Expand Up @@ -185,15 +186,15 @@ export function writePlaceholder(
id: number,
): boolean {
writeChunk(destination, PLACEHOLDER);
return writeChunk(destination, formatID(id));
return writeChunkAndReturn(destination, formatID(id));
}

// Suspense boundaries are encoded as comments.
export function writeStartCompletedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
): boolean {
return writeChunk(destination, SUSPENSE_COMPLETE);
return writeChunkAndReturn(destination, SUSPENSE_COMPLETE);
}

export function pushStartCompletedSuspenseBoundary(
Expand All @@ -208,19 +209,19 @@ export function writeStartPendingSuspenseBoundary(
id: SuspenseBoundaryID,
): boolean {
writeChunk(destination, SUSPENSE_PENDING);
return writeChunk(destination, formatID(id));
return writeChunkAndReturn(destination, formatID(id));
}
export function writeStartClientRenderedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
): boolean {
return writeChunk(destination, SUSPENSE_CLIENT_RENDER);
return writeChunkAndReturn(destination, SUSPENSE_CLIENT_RENDER);
}
export function writeEndCompletedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
): boolean {
return writeChunk(destination, END);
return writeChunkAndReturn(destination, END);
}
export function pushEndCompletedSuspenseBoundary(
target: Array<Chunk | PrecomputedChunk>,
Expand All @@ -231,13 +232,13 @@ export function writeEndPendingSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
): boolean {
return writeChunk(destination, END);
return writeChunkAndReturn(destination, END);
}
export function writeEndClientRenderedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
): boolean {
return writeChunk(destination, END);
return writeChunkAndReturn(destination, END);
}

export function writeStartSegment(
Expand All @@ -247,13 +248,13 @@ export function writeStartSegment(
id: number,
): boolean {
writeChunk(destination, SEGMENT);
return writeChunk(destination, formatID(id));
return writeChunkAndReturn(destination, formatID(id));
}
export function writeEndSegment(
destination: Destination,
formatContext: FormatContext,
): boolean {
return writeChunk(destination, END);
return writeChunkAndReturn(destination, END);
}

// Instruction Set
Expand All @@ -276,7 +277,7 @@ export function writeCompletedBoundaryInstruction(
): boolean {
writeChunk(destination, SUSPENSE_UPDATE_TO_COMPLETE);
writeChunk(destination, formatID(boundaryID));
return writeChunk(destination, formatID(contentSegmentID));
return writeChunkAndReturn(destination, formatID(contentSegmentID));
}

export function writeClientRenderBoundaryInstruction(
Expand All @@ -285,5 +286,5 @@ export function writeClientRenderBoundaryInstruction(
boundaryID: SuspenseBoundaryID,
): boolean {
writeChunk(destination, SUSPENSE_UPDATE_TO_CLIENT_RENDER);
return writeChunk(destination, formatID(boundaryID));
return writeChunkAndReturn(destination, formatID(boundaryID));
}
4 changes: 4 additions & 0 deletions packages/react-noop-renderer/src/ReactNoopFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const ReactNoopFlightServer = ReactFlightServer({
writeChunk(destination: Destination, chunk: string): void {
destination.push(chunk);
},
writeChunkAndReturn(destination: Destination, chunk: string): boolean {
destination.push(chunk);
return true;
},
completeWriting(destination: Destination): void {},
close(destination: Destination): void {},
closeWithError(destination: Destination, error: mixed): void {},
Expand Down
36 changes: 22 additions & 14 deletions packages/react-noop-renderer/src/ReactNoopServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,34 @@ type Destination = {

const POP = Buffer.from('/', 'utf8');

function write(destination: Destination, buffer: Uint8Array): void {
const stack = destination.stack;
if (buffer === POP) {
stack.pop();
return;
}
// We assume one chunk is one instance.
const instance = JSON.parse(Buffer.from((buffer: any)).toString('utf8'));
if (stack.length === 0) {
destination.root = instance;
} else {
const parent = stack[stack.length - 1];
parent.children.push(instance);
}
stack.push(instance);
}

const ReactNoopServer = ReactFizzServer({
scheduleWork(callback: () => void) {
callback();
},
beginWriting(destination: Destination): void {},
writeChunk(destination: Destination, buffer: Uint8Array): void {
const stack = destination.stack;
if (buffer === POP) {
stack.pop();
return;
}
// We assume one chunk is one instance.
const instance = JSON.parse(Buffer.from((buffer: any)).toString('utf8'));
if (stack.length === 0) {
destination.root = instance;
} else {
const parent = stack[stack.length - 1];
parent.children.push(instance);
}
stack.push(instance);
write(destination, buffer);
},
writeChunkAndReturn(destination: Destination, buffer: Uint8Array): boolean {
write(destination, buffer);
return true;
},
completeWriting(destination: Destination): void {},
close(destination: Destination): void {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,14 @@ export function flushBuffered(destination: Destination) {}

export function beginWriting(destination: Destination) {}

export function writeChunk(destination: Destination, chunk: Chunk): boolean {
export function writeChunk(destination: Destination, chunk: Chunk): void {
emitRow(destination, chunk);
}

export function writeChunkAndReturn(
destination: Destination,
chunk: Chunk,
): boolean {
emitRow(destination, chunk);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export function beginWriting(destination: Destination) {}
export function writeChunk(
destination: Destination,
chunk: Chunk | PrecomputedChunk,
): void {
destination.buffer += chunk;
}

export function writeChunkAndReturn(
destination: Destination,
chunk: Chunk | PrecomputedChunk,
): boolean {
destination.buffer += chunk;
return true;
Expand Down
Loading

0 comments on commit 4035157

Please sign in to comment.