GeezerVison is showing github.com — Use SyncLane for discrete event hydration (#21038) · react/react@1fafac0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMServerPartialHydration-test.internal.js +63-56Lines changed: 63 additions & 56 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2187,17 +2187,17 @@ describe('ReactDOMServerPartialHydration', () => { | 2187 | 2187 | expect(container.textContent).toBe('Click meHello'); | 2188 | 2188 |
| 2189 | 2189 | // We're now partially hydrated. | 2190 | | - a.click(); | | 2190 | + await act(async () => { | | 2191 | + a.click(); | | 2192 | + }); | 2191 | 2193 | expect(clicks).toBe(0); | 2192 | 2194 |
| 2193 | 2195 | // Resolving the promise so that rendering can complete. | 2194 | | - suspend = false; | 2195 | | - resolve(); | 2196 | | - await promise; | 2197 | | - | 2198 | | - Scheduler.unstable_flushAll(); | 2199 | | - jest.runAllTimers(); | 2200 | | - | | 2196 | + await act(async () => { | | 2197 | + suspend = false; | | 2198 | + resolve(); | | 2199 | + await promise; | | 2200 | + }); | 2201 | 2201 | expect(clicks).toBe(1); | 2202 | 2202 |
| 2203 | 2203 | expect(container.textContent).toBe('Hello'); | @@ -2270,18 +2270,19 @@ describe('ReactDOMServerPartialHydration', () => { | 2270 | 2270 | jest.runAllTimers(); | 2271 | 2271 |
| 2272 | 2272 | // We're now partially hydrated. | 2273 | | - a.click(); | | 2273 | + await act(async () => { | | 2274 | + a.click(); | | 2275 | + }); | 2274 | 2276 | // We should not have invoked the event yet because we're not | 2275 | 2277 | // yet hydrated. | 2276 | 2278 | expect(onEvent).toHaveBeenCalledTimes(0); | 2277 | 2279 |
| 2278 | 2280 | // Resolving the promise so that rendering can complete. | 2279 | | - suspend = false; | 2280 | | - resolve(); | 2281 | | - await promise; | 2282 | | - | 2283 | | - Scheduler.unstable_flushAll(); | 2284 | | - jest.runAllTimers(); | | 2281 | + await act(async () => { | | 2282 | + suspend = false; | | 2283 | + resolve(); | | 2284 | + await promise; | | 2285 | + }); | 2285 | 2286 |
| 2286 | 2287 | expect(onEvent).toHaveBeenCalledTimes(2); | 2287 | 2288 |
| @@ -2344,25 +2345,27 @@ describe('ReactDOMServerPartialHydration', () => { | 2344 | 2345 | root.render(<App />); | 2345 | 2346 |
| 2346 | 2347 | // We'll do one click before hydrating. | 2347 | | - a.click(); | | 2348 | + await act(async () => { | | 2349 | + a.click(); | | 2350 | + }); | 2348 | 2351 | // This should be delayed. | 2349 | 2352 | expect(clicks).toBe(0); | 2350 | 2353 |
| 2351 | 2354 | Scheduler.unstable_flushAll(); | 2352 | 2355 | jest.runAllTimers(); | 2353 | 2356 |
| 2354 | 2357 | // We're now partially hydrated. | 2355 | | - a.click(); | | 2358 | + await act(async () => { | | 2359 | + a.click(); | | 2360 | + }); | 2356 | 2361 | expect(clicks).toBe(0); | 2357 | 2362 |
| 2358 | 2363 | // Resolving the promise so that rendering can complete. | 2359 | | - suspend = false; | 2360 | | - resolve(); | 2361 | | - await promise; | 2362 | | - | 2363 | | - Scheduler.unstable_flushAll(); | 2364 | | - jest.runAllTimers(); | 2365 | | - | | 2364 | + await act(async () => { | | 2365 | + suspend = false; | | 2366 | + resolve(); | | 2367 | + await promise; | | 2368 | + }); | 2366 | 2369 | expect(clicks).toBe(2); | 2367 | 2370 |
| 2368 | 2371 | document.body.removeChild(container); | @@ -2436,19 +2439,19 @@ describe('ReactDOMServerPartialHydration', () => { | 2436 | 2439 | jest.runAllTimers(); | 2437 | 2440 |
| 2438 | 2441 | // We're now partially hydrated. | 2439 | | - a.click(); | | 2442 | + await act(async () => { | | 2443 | + a.click(); | | 2444 | + }); | 2440 | 2445 | // We should not have invoked the event yet because we're not | 2441 | 2446 | // yet hydrated. | 2442 | 2447 | expect(onEvent).toHaveBeenCalledTimes(0); | 2443 | 2448 |
| 2444 | 2449 | // Resolving the promise so that rendering can complete. | 2445 | | - suspend = false; | 2446 | | - resolve(); | 2447 | | - await promise; | 2448 | | - | 2449 | | - Scheduler.unstable_flushAll(); | 2450 | | - jest.runAllTimers(); | 2451 | | - | | 2450 | + await act(async () => { | | 2451 | + suspend = false; | | 2452 | + resolve(); | | 2453 | + await promise; | | 2454 | + }); | 2452 | 2455 | expect(onEvent).toHaveBeenCalledTimes(2); | 2453 | 2456 |
| 2454 | 2457 | document.body.removeChild(container); | @@ -2510,17 +2513,18 @@ describe('ReactDOMServerPartialHydration', () => { | 2510 | 2513 | jest.runAllTimers(); | 2511 | 2514 |
| 2512 | 2515 | // We're now partially hydrated. | 2513 | | - span.click(); | | 2516 | + await act(async () => { | | 2517 | + span.click(); | | 2518 | + }); | 2514 | 2519 | expect(clicksOnChild).toBe(0); | 2515 | 2520 | expect(clicksOnParent).toBe(0); | 2516 | 2521 |
| 2517 | 2522 | // Resolving the promise so that rendering can complete. | 2518 | | - suspend = false; | 2519 | | - resolve(); | 2520 | | - await promise; | 2521 | | - | 2522 | | - Scheduler.unstable_flushAll(); | 2523 | | - jest.runAllTimers(); | | 2523 | + await act(async () => { | | 2524 | + suspend = false; | | 2525 | + resolve(); | | 2526 | + await promise; | | 2527 | + }); | 2524 | 2528 |
| 2525 | 2529 | expect(clicksOnChild).toBe(1); | 2526 | 2530 | // This will be zero due to the stopPropagation. | @@ -2589,16 +2593,17 @@ describe('ReactDOMServerPartialHydration', () => { | 2589 | 2593 | Scheduler.unstable_flushAll(); | 2590 | 2594 |
| 2591 | 2595 | // The Suspense boundary is not yet hydrated. | 2592 | | - a.click(); | | 2596 | + await act(async () => { | | 2597 | + a.click(); | | 2598 | + }); | 2593 | 2599 | expect(clicks).toBe(0); | 2594 | 2600 |
| 2595 | 2601 | // Resolving the promise so that rendering can complete. | 2596 | | - suspend = false; | 2597 | | - resolve(); | 2598 | | - await promise; | 2599 | | - | 2600 | | - Scheduler.unstable_flushAll(); | 2601 | | - jest.runAllTimers(); | | 2602 | + await act(async () => { | | 2603 | + suspend = false; | | 2604 | + resolve(); | | 2605 | + await promise; | | 2606 | + }); | 2602 | 2607 |
| 2603 | 2608 | // We're now full hydrated. | 2604 | 2609 |
| @@ -2858,20 +2863,22 @@ describe('ReactDOMServerPartialHydration', () => { | 2858 | 2863 | expect(container.textContent).toBe('Click meHello'); | 2859 | 2864 |
| 2860 | 2865 | // We're now partially hydrated. | 2861 | | - form.dispatchEvent( | 2862 | | - new Event('submit', { | 2863 | | - bubbles: true, | 2864 | | - }), | 2865 | | - ); | | 2866 | + await act(async () => { | | 2867 | + form.dispatchEvent( | | 2868 | + new Event('submit', { | | 2869 | + bubbles: true, | | 2870 | + }), | | 2871 | + ); | | 2872 | + }); | 2866 | 2873 | expect(submits).toBe(0); | 2867 | 2874 |
| 2868 | 2875 | // Resolving the promise so that rendering can complete. | 2869 | | - suspend = false; | 2870 | | - resolve(); | 2871 | | - await promise; | | 2876 | + await act(async () => { | | 2877 | + suspend = false; | | 2878 | + resolve(); | | 2879 | + await promise; | | 2880 | + }); | 2872 | 2881 |
| 2873 | | - Scheduler.unstable_flushAll(); | 2874 | | - jest.runAllTimers(); | 2875 | 2882 | expect(submits).toBe(1); | 2876 | 2883 | expect(container.textContent).toBe('Hello'); | 2877 | 2884 | document.body.removeChild(container); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMServerSelectiveHydration-test.internal.js +45-36Lines changed: 45 additions & 36 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -255,22 +255,25 @@ describe('ReactDOMServerSelectiveHydration', () => { | 255 | 255 | expect(Scheduler).toHaveYielded([]); | 256 | 256 |
| 257 | 257 | // This click target cannot be hydrated yet because it's suspended. | 258 | | - const result = dispatchClickEvent(spanD); | 259 | | - | 260 | | - expect(Scheduler).toHaveYielded(['App']); | 261 | | - | 262 | | - expect(result).toBe(true); | 263 | | - | 264 | | - // Continuing rendering will render B next. | 265 | | - expect(Scheduler).toFlushAndYield(['B', 'C']); | 266 | | - | 267 | | - suspend = false; | 268 | | - resolve(); | 269 | | - await promise; | | 258 | + await act(async () => { | | 259 | + const result = dispatchClickEvent(spanD); | | 260 | + expect(result).toBe(true); | | 261 | + }); | | 262 | + expect(Scheduler).toHaveYielded([ | | 263 | + 'App', | | 264 | + // Continuing rendering will render B next. | | 265 | + 'B', | | 266 | + 'C', | | 267 | + ]); | 270 | 268 |
| | 269 | + await act(async () => { | | 270 | + suspend = false; | | 271 | + resolve(); | | 272 | + await promise; | | 273 | + }); | 271 | 274 | // After the click, we should prioritize D and the Click first, | 272 | 275 | // and only after that render A and C. | 273 | | - expect(Scheduler).toFlushAndYield(['D', 'Clicked D', 'A']); | | 276 | + expect(Scheduler).toHaveYielded(['D', 'Clicked D', 'A']); | 274 | 277 |
| 275 | 278 | document.body.removeChild(container); | 276 | 279 | }); | @@ -348,13 +351,15 @@ describe('ReactDOMServerSelectiveHydration', () => { | 348 | 351 |
| 349 | 352 | expect(Scheduler).toHaveYielded(['App']); | 350 | 353 |
| 351 | | - suspend = false; | 352 | | - resolve(); | 353 | | - await promise; | | 354 | + await act(async () => { | | 355 | + suspend = false; | | 356 | + resolve(); | | 357 | + await promise; | | 358 | + }); | 354 | 359 |
| 355 | 360 | // We should prioritize hydrating A, C and D first since we clicked in | 356 | 361 | // them. Only after they're done will we hydrate B. | 357 | | - expect(Scheduler).toFlushAndYield([ | | 362 | + expect(Scheduler).toHaveYielded([ | 358 | 363 | 'A', | 359 | 364 | 'Clicked A', | 360 | 365 | 'C', | @@ -506,21 +511,21 @@ describe('ReactDOMServerSelectiveHydration', () => { | 506 | 511 | // Nothing has been hydrated so far. | 507 | 512 | expect(Scheduler).toHaveYielded([]); | 508 | 513 |
| 509 | | - const target = createEventTarget(spanD); | 510 | | - target.virtualclick(); | 511 | | - | 512 | | - expect(Scheduler).toHaveYielded(['App']); | 513 | | - | 514 | 514 | // Continuing rendering will render B next. | 515 | | - expect(Scheduler).toFlushAndYield(['B', 'C']); | 516 | | - | 517 | | - suspend = false; | 518 | | - resolve(); | 519 | | - await promise; | | 515 | + await act(async () => { | | 516 | + const target = createEventTarget(spanD); | | 517 | + target.virtualclick(); | | 518 | + }); | | 519 | + expect(Scheduler).toHaveYielded(['App', 'B', 'C']); | 520 | 520 |
| 521 | 521 | // After the click, we should prioritize D and the Click first, | 522 | 522 | // and only after that render A and C. | 523 | | - expect(Scheduler).toFlushAndYield(['D', 'Clicked D', 'A']); | | 523 | + await act(async () => { | | 524 | + suspend = false; | | 525 | + resolve(); | | 526 | + await promise; | | 527 | + }); | | 528 | + expect(Scheduler).toHaveYielded(['D', 'Clicked D', 'A']); | 524 | 529 |
| 525 | 530 | document.body.removeChild(container); | 526 | 531 | }); | @@ -602,13 +607,15 @@ describe('ReactDOMServerSelectiveHydration', () => { | 602 | 607 |
| 603 | 608 | expect(Scheduler).toHaveYielded(['App']); | 604 | 609 |
| 605 | | - suspend = false; | 606 | | - resolve(); | 607 | | - await promise; | | 610 | + await act(async () => { | | 611 | + suspend = false; | | 612 | + resolve(); | | 613 | + await promise; | | 614 | + }); | 608 | 615 |
| 609 | 616 | // We should prioritize hydrating A, C and D first since we clicked in | 610 | 617 | // them. Only after they're done will we hydrate B. | 611 | | - expect(Scheduler).toFlushAndYield([ | | 618 | + expect(Scheduler).toHaveYielded([ | 612 | 619 | 'A', | 613 | 620 | 'Clicked A', | 614 | 621 | 'C', | @@ -701,17 +708,19 @@ describe('ReactDOMServerSelectiveHydration', () => { | 701 | 708 |
| 702 | 709 | expect(Scheduler).toHaveYielded(['App']); | 703 | 710 |
| 704 | | - suspend = false; | 705 | | - resolve(); | 706 | | - await promise; | | 711 | + await act(async () => { | | 712 | + suspend = false; | | 713 | + resolve(); | | 714 | + await promise; | | 715 | + }); | 707 | 716 |
| 708 | 717 | // We should prioritize hydrating D first because we clicked it. | 709 | 718 | // Next we should hydrate C since that's the current hover target. | 710 | 719 | // To simplify implementation details we hydrate both B and C at | 711 | 720 | // the same time since B was already scheduled. | 712 | 721 | // This is ok because it will at least not continue for nested | 713 | 722 | // boundary. See the next test below. | 714 | | - expect(Scheduler).toFlushAndYield([ | | 723 | + expect(Scheduler).toHaveYielded([ | 715 | 724 | 'D', | 716 | 725 | 'Clicked D', | 717 | 726 | 'B', // Ideally this should be later. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/__tests__/DOMPluginEventSystem-test.internal.js +9-7Lines changed: 9 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -67,6 +67,7 @@ describe('DOMPluginEventSystem', () => { | 67 | 67 | ReactDOM = require('react-dom'); | 68 | 68 | Scheduler = require('scheduler'); | 69 | 69 | ReactDOMServer = require('react-dom/server'); | | 70 | + act = require('react-dom/test-utils').unstable_concurrentAct; | 70 | 71 | container = document.createElement('div'); | 71 | 72 | document.body.appendChild(container); | 72 | 73 | startNativeEventListenerClearDown(); | @@ -636,16 +637,17 @@ describe('DOMPluginEventSystem', () => { | 636 | 637 | Scheduler.unstable_flushAll(); | 637 | 638 |
| 638 | 639 | // The Suspense boundary is not yet hydrated. | 639 | | - a.click(); | | 640 | + await act(async () => { | | 641 | + a.click(); | | 642 | + }); | 640 | 643 | expect(clicks).toBe(0); | 641 | 644 |
| 642 | 645 | // Resolving the promise so that rendering can complete. | 643 | | - suspend = false; | 644 | | - resolve(); | 645 | | - await promise; | 646 | | - | 647 | | - Scheduler.unstable_flushAll(); | 648 | | - jest.runAllTimers(); | | 646 | + await act(async () => { | | 647 | + suspend = false; | | 648 | + resolve(); | | 649 | + await promise; | | 650 | + }); | 649 | 651 |
| 650 | 652 | // We're now full hydrated. | 651 | 653 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberReconciler.new.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,7 +78,6 @@ import { | 78 | 78 | import {StrictLegacyMode} from './ReactTypeOfMode'; | 79 | 79 | import { | 80 | 80 | SyncLane, | 81 | | - InputDiscreteHydrationLane, | 82 | 81 | SelectiveHydrationLane, | 83 | 82 | NoTimestamp, | 84 | 83 | getHighestPriorityPendingLanes, | @@ -388,7 +387,7 @@ export function attemptSynchronousHydration(fiber: Fiber): void { | 388 | 387 | // If we're still blocked after this, we need to increase | 389 | 388 | // the priority of any promises resolving within this | 390 | 389 | // boundary so that they next attempt also has higher pri. | 391 | | - const retryLane = InputDiscreteHydrationLane; | | 390 | + const retryLane = SyncLane; | 392 | 391 | markRetryLaneIfNotHydrated(fiber, retryLane); | 393 | 392 | break; | 394 | 393 | } | @@ -422,7 +421,7 @@ export function attemptDiscreteHydration(fiber: Fiber): void { | 422 | 421 | return; | 423 | 422 | } | 424 | 423 | const eventTime = requestEventTime(); | 425 | | - const lane = InputDiscreteHydrationLane; | | 424 | + const lane = SyncLane; | 426 | 425 | scheduleUpdateOnFiber(fiber, lane, eventTime); | 427 | 426 | markRetryLaneIfNotHydrated(fiber, lane); | 428 | 427 | } | |
|