Skip to content

Commit 2c509da

Browse files
committed
Throttle reveals
1 parent fa27471 commit 2c509da

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetInlineCodeStrings.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetShared.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export function clientRenderBoundary(
4747
}
4848
}
4949

50+
const FALLBACK_THROTTLE_MS = 300;
51+
5052
export function completeBoundary(suspenseBoundaryID, contentID) {
5153
const contentNodeOuter = document.getElementById(contentID);
5254
if (!contentNodeOuter) {
@@ -69,6 +71,7 @@ export function completeBoundary(suspenseBoundaryID, contentID) {
6971
}
7072

7173
function revealCompletedBoundaries() {
74+
window['$RT'] = performance.now();
7275
const batch = window['$RB'];
7376
window['$RB'] = [];
7477
for (let i = 0; i < batch.length; i += 2) {
@@ -132,7 +135,18 @@ export function completeBoundary(suspenseBoundaryID, contentID) {
132135
// Queue this boundary for the next batch
133136
window['$RB'].push(suspenseIdNodeOuter, contentNodeOuter);
134137

135-
revealCompletedBoundaries();
138+
if (window['$RB'].length === 2) {
139+
// This is the first time we've pushed to the batch. We need to schedule a callback
140+
// to flush the batch. This is delayed by the throttle heuristic.
141+
const globalMostRecentFallbackTime =
142+
typeof window['$RT'] !== 'number' ? 0 : window['$RT'];
143+
const msUntilTimeout =
144+
globalMostRecentFallbackTime + FALLBACK_THROTTLE_MS - performance.now();
145+
// We always schedule the flush in a timer even if it's very low or negative to allow
146+
// for multiple completeBoundary calls that are already queued to have a chance to
147+
// make the batch.
148+
setTimeout(revealCompletedBoundaries, msUntilTimeout);
149+
}
136150
}
137151

138152
export function completeBoundaryWithStyles(

packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ describe('ReactDOMFizzServer', () => {
317317
div.innerHTML = bufferedContent;
318318
await insertNodesAndExecuteScripts(div, streamingContainer, CSPnonce);
319319
}
320+
// Let throttled boundaries reveal
321+
jest.runAllTimers();
320322
}
321323

322324
function resolveText(text) {

packages/react-dom/src/__tests__/ReactDOMFloat-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ describe('ReactDOMFloat', () => {
233233
div.innerHTML = bufferedContent;
234234
await insertNodesAndExecuteScripts(div, streamingContainer, CSPnonce);
235235
}
236+
// Let throttled boundaries reveal
237+
jest.runAllTimers();
236238
}
237239

238240
function getMeaningfulChildren(element) {

0 commit comments

Comments
 (0)