From b577e19bf9e89b7092c795e47063f50815e16770 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Wed, 23 Feb 2022 00:54:00 -0500 Subject: [PATCH] Split writeChunk into void and return value 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. --- .../ReactDOMLegacyServerStreamConfig.js | 7 +++ .../src/server/ReactDOMServerFormatConfig.js | 60 ++++++++++--------- .../server/ReactNativeServerFormatConfig.js | 23 +++---- .../src/ReactNoopFlightServer.js | 4 ++ .../src/ReactNoopServer.js | 36 ++++++----- .../ReactFlightDOMRelayServerHostConfig.js | 9 ++- .../src/ReactServerStreamConfigFB.js | 7 +++ .../ReactFlightNativeRelayServerHostConfig.js | 9 ++- packages/react-server/src/ReactFizzServer.js | 8 ++- .../react-server/src/ReactFlightServer.js | 11 ++-- .../src/ReactFlightServerConfigStream.js | 1 + .../src/ReactServerStreamConfigBrowser.js | 7 +++ .../src/ReactServerStreamConfigNode.js | 8 +++ .../forks/ReactServerStreamConfig.custom.js | 1 + 14 files changed, 130 insertions(+), 61 deletions(-) diff --git a/packages/react-dom/src/server/ReactDOMLegacyServerStreamConfig.js b/packages/react-dom/src/server/ReactDOMLegacyServerStreamConfig.js index 4cfe16091bc5..55418357f447 100644 --- a/packages/react-dom/src/server/ReactDOMLegacyServerStreamConfig.js +++ b/packages/react-dom/src/server/ReactDOMLegacyServerStreamConfig.js @@ -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; diff --git a/packages/react-dom/src/server/ReactDOMServerFormatConfig.js b/packages/react-dom/src/server/ReactDOMServerFormatConfig.js index 5ea8d772732b..20887c226015 100644 --- a/packages/react-dom/src/server/ReactDOMServerFormatConfig.js +++ b/packages/react-dom/src/server/ReactDOMServerFormatConfig.js @@ -30,6 +30,7 @@ import type { import { writeChunk, + writeChunkAndReturn, stringToChunk, stringToPrecomputedChunk, } from 'react-server/src/ReactServerStreamConfig'; @@ -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 @@ -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. @@ -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, @@ -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('