From 75662d6a7d135df9d10055f49c3a4ca09fe4efcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Mon, 28 Feb 2022 21:35:00 -0500 Subject: [PATCH] Remove hacky stream.locked check, declare as byte stream instead (#23387) We used to check for stream.locked in pull to see if we've been passed to something that reads yet. This was a bad hack because it won't actually call pull again if that changes. The source of this is because the default for "highWaterMark" is 1 on some streams. So it always wants to add one "chunk" (of size 1). If we leave our high water mark as 0, we won't fill up any buffers unless we're asked for more. This web API is somewhat odd because it would be way more efficient if it just told us how much the recipient wants instead of calling us once per chunk. Anyway, I turns out that if we define ourselves as a "bytes" type of stream, the default also happens to be a high water mark of 0 so we can just use that instead. --- .../react-dom/src/server/ReactDOMFizzServerBrowser.js | 9 ++------- .../src/ReactFlightDOMServerBrowser.js | 9 ++------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/packages/react-dom/src/server/ReactDOMFizzServerBrowser.js b/packages/react-dom/src/server/ReactDOMFizzServerBrowser.js index 15fc69437ca15..a07967ca34f98 100644 --- a/packages/react-dom/src/server/ReactDOMFizzServerBrowser.js +++ b/packages/react-dom/src/server/ReactDOMFizzServerBrowser.js @@ -43,14 +43,9 @@ function renderToReadableStream( return new Promise((resolve, reject) => { function onCompleteShell() { const stream = new ReadableStream({ + type: 'bytes', pull(controller) { - // Pull is called immediately even if the stream is not passed to anything. - // That's buffering too early. We want to start buffering once the stream - // is actually used by something so we can give it the best result possible - // at that point. - if (stream.locked) { - startFlowing(request, controller); - } + startFlowing(request, controller); }, cancel(reason) {}, }); diff --git a/packages/react-server-dom-webpack/src/ReactFlightDOMServerBrowser.js b/packages/react-server-dom-webpack/src/ReactFlightDOMServerBrowser.js index 9d632dfb0712e..35518c9d33bf2 100644 --- a/packages/react-server-dom-webpack/src/ReactFlightDOMServerBrowser.js +++ b/packages/react-server-dom-webpack/src/ReactFlightDOMServerBrowser.js @@ -31,17 +31,12 @@ function renderToReadableStream( options ? options.onError : undefined, ); const stream = new ReadableStream({ + type: 'bytes', start(controller) { startWork(request); }, pull(controller) { - // Pull is called immediately even if the stream is not passed to anything. - // That's buffering too early. We want to start buffering once the stream - // is actually used by something so we can give it the best result possible - // at that point. - if (stream.locked) { - startFlowing(request, controller); - } + startFlowing(request, controller); }, cancel(reason) {}, });