Skip to content

Commit 75662d6

Browse files
authored
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.
1 parent 086fa8e commit 75662d6

File tree

2 files changed

+4
-14
lines changed

2 files changed

+4
-14
lines changed

packages/react-dom/src/server/ReactDOMFizzServerBrowser.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,9 @@ function renderToReadableStream(
4343
return new Promise((resolve, reject) => {
4444
function onCompleteShell() {
4545
const stream = new ReadableStream({
46+
type: 'bytes',
4647
pull(controller) {
47-
// Pull is called immediately even if the stream is not passed to anything.
48-
// That's buffering too early. We want to start buffering once the stream
49-
// is actually used by something so we can give it the best result possible
50-
// at that point.
51-
if (stream.locked) {
52-
startFlowing(request, controller);
53-
}
48+
startFlowing(request, controller);
5449
},
5550
cancel(reason) {},
5651
});

packages/react-server-dom-webpack/src/ReactFlightDOMServerBrowser.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,12 @@ function renderToReadableStream(
3131
options ? options.onError : undefined,
3232
);
3333
const stream = new ReadableStream({
34+
type: 'bytes',
3435
start(controller) {
3536
startWork(request);
3637
},
3738
pull(controller) {
38-
// Pull is called immediately even if the stream is not passed to anything.
39-
// That's buffering too early. We want to start buffering once the stream
40-
// is actually used by something so we can give it the best result possible
41-
// at that point.
42-
if (stream.locked) {
43-
startFlowing(request, controller);
44-
}
39+
startFlowing(request, controller);
4540
},
4641
cancel(reason) {},
4742
});

0 commit comments

Comments
 (0)