GeezerVison is showing github.com — [Selective Hydration] Prioritize the last continuous target (#16937) · react/react@bb680a0
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__/ReactDOMServerSelectiveHydration-test.internal.js +150Lines changed: 150 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,57 @@ let Scheduler; | 16 | 16 | let ReactFeatureFlags; | 17 | 17 | let Suspense; | 18 | 18 |
| | 19 | +function dispatchMouseHoverEvent(to, from) { | | 20 | + if (!to) { | | 21 | + to = null; | | 22 | + } | | 23 | + if (!from) { | | 24 | + from = null; | | 25 | + } | | 26 | + if (from) { | | 27 | + const mouseOutEvent = document.createEvent('MouseEvents'); | | 28 | + mouseOutEvent.initMouseEvent( | | 29 | + 'mouseout', | | 30 | + true, | | 31 | + true, | | 32 | + window, | | 33 | + 0, | | 34 | + 50, | | 35 | + 50, | | 36 | + 50, | | 37 | + 50, | | 38 | + false, | | 39 | + false, | | 40 | + false, | | 41 | + false, | | 42 | + 0, | | 43 | + to, | | 44 | + ); | | 45 | + from.dispatchEvent(mouseOutEvent); | | 46 | + } | | 47 | + if (to) { | | 48 | + const mouseOverEvent = document.createEvent('MouseEvents'); | | 49 | + mouseOverEvent.initMouseEvent( | | 50 | + 'mouseover', | | 51 | + true, | | 52 | + true, | | 53 | + window, | | 54 | + 0, | | 55 | + 50, | | 56 | + 50, | | 57 | + 50, | | 58 | + 50, | | 59 | + false, | | 60 | + false, | | 61 | + false, | | 62 | + false, | | 63 | + 0, | | 64 | + from, | | 65 | + ); | | 66 | + to.dispatchEvent(mouseOverEvent); | | 67 | + } | | 68 | +} | | 69 | + | 19 | 70 | function dispatchClickEvent(target) { | 20 | 71 | const mouseOutEvent = document.createEvent('MouseEvents'); | 21 | 72 | mouseOutEvent.initMouseEvent( | @@ -290,4 +341,103 @@ describe('ReactDOMServerSelectiveHydration', () => { | 290 | 341 |
| 291 | 342 | document.body.removeChild(container); | 292 | 343 | }); | | 344 | + | | 345 | + it('hydrates the last target as higher priority for continuous events', async () => { | | 346 | + let suspend = false; | | 347 | + let resolve; | | 348 | + let promise = new Promise(resolvePromise => (resolve = resolvePromise)); | | 349 | + | | 350 | + function Child({text}) { | | 351 | + if ((text === 'A' || text === 'D') && suspend) { | | 352 | + throw promise; | | 353 | + } | | 354 | + Scheduler.unstable_yieldValue(text); | | 355 | + return ( | | 356 | + <span | | 357 | + onClick={e => { | | 358 | + e.preventDefault(); | | 359 | + Scheduler.unstable_yieldValue('Clicked ' + text); | | 360 | + }} | | 361 | + onMouseEnter={e => { | | 362 | + e.preventDefault(); | | 363 | + Scheduler.unstable_yieldValue('Hover ' + text); | | 364 | + }}> | | 365 | + {text} | | 366 | + </span> | | 367 | + ); | | 368 | + } | | 369 | + | | 370 | + function App() { | | 371 | + Scheduler.unstable_yieldValue('App'); | | 372 | + return ( | | 373 | + <div> | | 374 | + <Suspense fallback="Loading..."> | | 375 | + <Child text="A" /> | | 376 | + </Suspense> | | 377 | + <Suspense fallback="Loading..."> | | 378 | + <Child text="B" /> | | 379 | + </Suspense> | | 380 | + <Suspense fallback="Loading..."> | | 381 | + <Child text="C" /> | | 382 | + </Suspense> | | 383 | + <Suspense fallback="Loading..."> | | 384 | + <Child text="D" /> | | 385 | + </Suspense> | | 386 | + </div> | | 387 | + ); | | 388 | + } | | 389 | + | | 390 | + let finalHTML = ReactDOMServer.renderToString(<App />); | | 391 | + | | 392 | + expect(Scheduler).toHaveYielded(['App', 'A', 'B', 'C', 'D']); | | 393 | + | | 394 | + let container = document.createElement('div'); | | 395 | + // We need this to be in the document since we'll dispatch events on it. | | 396 | + document.body.appendChild(container); | | 397 | + | | 398 | + container.innerHTML = finalHTML; | | 399 | + | | 400 | + let spanB = container.getElementsByTagName('span')[1]; | | 401 | + let spanC = container.getElementsByTagName('span')[2]; | | 402 | + let spanD = container.getElementsByTagName('span')[3]; | | 403 | + | | 404 | + suspend = true; | | 405 | + | | 406 | + // A and D will be suspended. We'll click on D which should take | | 407 | + // priority, after we unsuspend. | | 408 | + let root = ReactDOM.unstable_createRoot(container, {hydrate: true}); | | 409 | + root.render(<App />); | | 410 | + | | 411 | + // Nothing has been hydrated so far. | | 412 | + expect(Scheduler).toHaveYielded([]); | | 413 | + | | 414 | + // Click D | | 415 | + dispatchMouseHoverEvent(spanD, null); | | 416 | + dispatchClickEvent(spanD); | | 417 | + // Hover over B and then C. | | 418 | + dispatchMouseHoverEvent(spanB, spanD); | | 419 | + dispatchMouseHoverEvent(spanC, spanB); | | 420 | + | | 421 | + expect(Scheduler).toHaveYielded(['App']); | | 422 | + | | 423 | + suspend = false; | | 424 | + resolve(); | | 425 | + await promise; | | 426 | + | | 427 | + // We should prioritize hydrating D first because we clicked it. | | 428 | + // Next we should hydrate C since that's the current hover target. | | 429 | + // Next it doesn't matter if we hydrate A or B first but as an | | 430 | + // implementation detail we're currently hydrating B first since | | 431 | + // we at one point hovered over it and we never deprioritized it. | | 432 | + expect(Scheduler).toFlushAndYield([ | | 433 | + 'D', | | 434 | + 'Clicked D', | | 435 | + 'C', | | 436 | + 'Hover C', | | 437 | + 'B', | | 438 | + 'A', | | 439 | + ]); | | 440 | + | | 441 | + document.body.removeChild(container); | | 442 | + }); | 293 | 443 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOM.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,6 +41,7 @@ import { | 41 | 41 | IsThisRendererActing, | 42 | 42 | attemptSynchronousHydration, | 43 | 43 | attemptUserBlockingHydration, | | 44 | + attemptContinuousHydration, | 44 | 45 | } from 'react-reconciler/inline.dom'; | 45 | 46 | import {createPortal as createPortalImpl} from 'shared/ReactPortal'; | 46 | 47 | import {canUseDOM} from 'shared/ExecutionEnvironment'; | @@ -79,6 +80,7 @@ import {dispatchEvent} from '../events/ReactDOMEventListener'; | 79 | 80 | import { | 80 | 81 | setAttemptSynchronousHydration, | 81 | 82 | setAttemptUserBlockingHydration, | | 83 | + setAttemptContinuousHydration, | 82 | 84 | } from '../events/ReactDOMEventReplaying'; | 83 | 85 | import {eagerlyTrapReplayableEvents} from '../events/ReactDOMEventReplaying'; | 84 | 86 | import { | @@ -91,6 +93,7 @@ import {ROOT_ATTRIBUTE_NAME} from '../shared/DOMProperty'; | 91 | 93 |
| 92 | 94 | setAttemptSynchronousHydration(attemptSynchronousHydration); | 93 | 95 | setAttemptUserBlockingHydration(attemptUserBlockingHydration); | | 96 | +setAttemptContinuousHydration(attemptContinuousHydration); | 94 | 97 |
| 95 | 98 | const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner; | 96 | 99 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/ReactDOMEventReplaying.js +35-15Lines changed: 35 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,6 +43,12 @@ export function setAttemptUserBlockingHydration(fn: (fiber: Object) => void) { | 43 | 43 | attemptUserBlockingHydration = fn; | 44 | 44 | } | 45 | 45 |
| | 46 | +let attemptContinuousHydration: (fiber: Object) => void; | | 47 | + | | 48 | +export function setAttemptContinuousHydration(fn: (fiber: Object) => void) { | | 49 | + attemptContinuousHydration = fn; | | 50 | +} | | 51 | + | 46 | 52 | // TODO: Upgrade this definition once we're on a newer version of Flow that | 47 | 53 | // has this definition built-in. | 48 | 54 | type PointerEvent = Event & { | @@ -305,7 +311,7 @@ export function clearIfContinuousEvent( | 305 | 311 | } | 306 | 312 | } | 307 | 313 |
| 308 | | -function accumulateOrCreateQueuedReplayableEvent( | | 314 | +function accumulateOrCreateContinuousQueuedReplayableEvent( | 309 | 315 | existingQueuedEvent: null | QueuedReplayableEvent, | 310 | 316 | blockedOn: null | Container | SuspenseInstance, | 311 | 317 | topLevelType: DOMTopLevelEventType, | @@ -316,12 +322,20 @@ function accumulateOrCreateQueuedReplayableEvent( | 316 | 322 | existingQueuedEvent === null || | 317 | 323 | existingQueuedEvent.nativeEvent !== nativeEvent | 318 | 324 | ) { | 319 | | - return createQueuedReplayableEvent( | | 325 | + let queuedEvent = createQueuedReplayableEvent( | 320 | 326 | blockedOn, | 321 | 327 | topLevelType, | 322 | 328 | eventSystemFlags, | 323 | 329 | nativeEvent, | 324 | 330 | ); | | 331 | + if (blockedOn !== null) { | | 332 | + let fiber = getInstanceFromNode(blockedOn); | | 333 | + if (fiber !== null) { | | 334 | + // Attempt to increase the priority of this target. | | 335 | + attemptContinuousHydration(fiber); | | 336 | + } | | 337 | + } | | 338 | + return queuedEvent; | 325 | 339 | } | 326 | 340 | // If we have already queued this exact event, then it's because | 327 | 341 | // the different event systems have different DOM event listeners. | @@ -343,7 +357,7 @@ export function queueIfContinuousEvent( | 343 | 357 | switch (topLevelType) { | 344 | 358 | case TOP_FOCUS: { | 345 | 359 | const focusEvent = ((nativeEvent: any): FocusEvent); | 346 | | - queuedFocus = accumulateOrCreateQueuedReplayableEvent( | | 360 | + queuedFocus = accumulateOrCreateContinuousQueuedReplayableEvent( | 347 | 361 | queuedFocus, | 348 | 362 | blockedOn, | 349 | 363 | topLevelType, | @@ -354,7 +368,7 @@ export function queueIfContinuousEvent( | 354 | 368 | } | 355 | 369 | case TOP_DRAG_ENTER: { | 356 | 370 | const dragEvent = ((nativeEvent: any): DragEvent); | 357 | | - queuedDrag = accumulateOrCreateQueuedReplayableEvent( | | 371 | + queuedDrag = accumulateOrCreateContinuousQueuedReplayableEvent( | 358 | 372 | queuedDrag, | 359 | 373 | blockedOn, | 360 | 374 | topLevelType, | @@ -365,7 +379,7 @@ export function queueIfContinuousEvent( | 365 | 379 | } | 366 | 380 | case TOP_MOUSE_OVER: { | 367 | 381 | const mouseEvent = ((nativeEvent: any): MouseEvent); | 368 | | - queuedMouse = accumulateOrCreateQueuedReplayableEvent( | | 382 | + queuedMouse = accumulateOrCreateContinuousQueuedReplayableEvent( | 369 | 383 | queuedMouse, | 370 | 384 | blockedOn, | 371 | 385 | topLevelType, | @@ -379,7 +393,7 @@ export function queueIfContinuousEvent( | 379 | 393 | const pointerId = pointerEvent.pointerId; | 380 | 394 | queuedPointers.set( | 381 | 395 | pointerId, | 382 | | - accumulateOrCreateQueuedReplayableEvent( | | 396 | + accumulateOrCreateContinuousQueuedReplayableEvent( | 383 | 397 | queuedPointers.get(pointerId) || null, | 384 | 398 | blockedOn, | 385 | 399 | topLevelType, | @@ -394,7 +408,7 @@ export function queueIfContinuousEvent( | 394 | 408 | const pointerId = pointerEvent.pointerId; | 395 | 409 | queuedPointerCaptures.set( | 396 | 410 | pointerId, | 397 | | - accumulateOrCreateQueuedReplayableEvent( | | 411 | + accumulateOrCreateContinuousQueuedReplayableEvent( | 398 | 412 | queuedPointerCaptures.get(pointerId) || null, | 399 | 413 | blockedOn, | 400 | 414 | topLevelType, | @@ -408,7 +422,9 @@ export function queueIfContinuousEvent( | 408 | 422 | return false; | 409 | 423 | } | 410 | 424 |
| 411 | | -function attemptReplayQueuedEvent(queuedEvent: QueuedReplayableEvent): boolean { | | 425 | +function attemptReplayContinuousQueuedEvent( | | 426 | + queuedEvent: QueuedReplayableEvent, | | 427 | +): boolean { | 412 | 428 | if (queuedEvent.blockedOn !== null) { | 413 | 429 | return false; | 414 | 430 | } | @@ -419,18 +435,22 @@ function attemptReplayQueuedEvent(queuedEvent: QueuedReplayableEvent): boolean { | 419 | 435 | ); | 420 | 436 | if (nextBlockedOn !== null) { | 421 | 437 | // We're still blocked. Try again later. | | 438 | + let fiber = getInstanceFromNode(nextBlockedOn); | | 439 | + if (fiber !== null) { | | 440 | + attemptContinuousHydration(fiber); | | 441 | + } | 422 | 442 | queuedEvent.blockedOn = nextBlockedOn; | 423 | 443 | return false; | 424 | 444 | } | 425 | 445 | return true; | 426 | 446 | } | 427 | 447 |
| 428 | | -function attemptReplayQueuedEventInMap( | | 448 | +function attemptReplayContinuousQueuedEventInMap( | 429 | 449 | queuedEvent: QueuedReplayableEvent, | 430 | 450 | key: number, | 431 | 451 | map: Map<number, QueuedReplayableEvent>, | 432 | 452 | ): void { | 433 | | - if (attemptReplayQueuedEvent(queuedEvent)) { | | 453 | + if (attemptReplayContinuousQueuedEvent(queuedEvent)) { | 434 | 454 | map.delete(key); | 435 | 455 | } | 436 | 456 | } | @@ -464,17 +484,17 @@ function replayUnblockedEvents() { | 464 | 484 | } | 465 | 485 | } | 466 | 486 | // Next replay any continuous events. | 467 | | - if (queuedFocus !== null && attemptReplayQueuedEvent(queuedFocus)) { | | 487 | + if (queuedFocus !== null && attemptReplayContinuousQueuedEvent(queuedFocus)) { | 468 | 488 | queuedFocus = null; | 469 | 489 | } | 470 | | - if (queuedDrag !== null && attemptReplayQueuedEvent(queuedDrag)) { | | 490 | + if (queuedDrag !== null && attemptReplayContinuousQueuedEvent(queuedDrag)) { | 471 | 491 | queuedDrag = null; | 472 | 492 | } | 473 | | - if (queuedMouse !== null && attemptReplayQueuedEvent(queuedMouse)) { | | 493 | + if (queuedMouse !== null && attemptReplayContinuousQueuedEvent(queuedMouse)) { | 474 | 494 | queuedMouse = null; | 475 | 495 | } | 476 | | - queuedPointers.forEach(attemptReplayQueuedEventInMap); | 477 | | - queuedPointerCaptures.forEach(attemptReplayQueuedEventInMap); | | 496 | + queuedPointers.forEach(attemptReplayContinuousQueuedEventInMap); | | 497 | + queuedPointerCaptures.forEach(attemptReplayContinuousQueuedEventInMap); | 478 | 498 | } | 479 | 499 |
| 480 | 500 | function scheduleCallbackIfUnblocked( | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberExpirationTime.js +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,10 @@ export const Never = 1; | 32 | 32 | // Idle is slightly higher priority than Never. It must completely finish in | 33 | 33 | // order to be consistent. | 34 | 34 | export const Idle = 2; | | 35 | +// Continuous Hydration is a moving priority. It is slightly higher than Idle | | 36 | +// and is used to increase priority of hover targets. It is increasing with | | 37 | +// each usage so that last always wins. | | 38 | +let ContinuousHydration = 3; | 35 | 39 | export const Sync = MAX_SIGNED_31_BIT_INT; | 36 | 40 | export const Batched = Sync - 1; | 37 | 41 |
| @@ -115,6 +119,15 @@ export function computeInteractiveExpiration(currentTime: ExpirationTime) { | 115 | 119 | ); | 116 | 120 | } | 117 | 121 |
| | 122 | +export function computeContinuousHydrationExpiration( | | 123 | + currentTime: ExpirationTime, | | 124 | +) { | | 125 | + // Each time we ask for a new one of these we increase the priority. | | 126 | + // This ensures that the last one always wins since we can't deprioritize | | 127 | + // once we've scheduled work already. | | 128 | + return ContinuousHydration++; | | 129 | +} | | 130 | + | 118 | 131 | export function inferPriorityFromExpirationTime( | 119 | 132 | currentTime: ExpirationTime, | 120 | 133 | expirationTime: ExpirationTime, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberReconciler.js +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,7 +78,11 @@ import { | 78 | 78 | current as ReactCurrentFiberCurrent, | 79 | 79 | } from './ReactCurrentFiber'; | 80 | 80 | import {StrictMode} from './ReactTypeOfMode'; | 81 | | -import {Sync, computeInteractiveExpiration} from './ReactFiberExpirationTime'; | | 81 | +import { | | 82 | + Sync, | | 83 | + computeInteractiveExpiration, | | 84 | + computeContinuousHydrationExpiration, | | 85 | +} from './ReactFiberExpirationTime'; | 82 | 86 | import {requestCurrentSuspenseConfig} from './ReactFiberSuspenseConfig'; | 83 | 87 | import { | 84 | 88 | scheduleRefresh, | @@ -421,6 +425,19 @@ export function attemptUserBlockingHydration(fiber: Fiber): void { | 421 | 425 | markRetryTimeIfNotHydrated(fiber, expTime); | 422 | 426 | } | 423 | 427 |
| | 428 | +export function attemptContinuousHydration(fiber: Fiber): void { | | 429 | + if (fiber.tag !== SuspenseComponent) { | | 430 | + // We ignore HostRoots here because we can't increase | | 431 | + // their priority and they should not suspend on I/O, | | 432 | + // since you have to wrap anything that might suspend in | | 433 | + // Suspense. | | 434 | + return; | | 435 | + } | | 436 | + let expTime = computeContinuousHydrationExpiration(requestCurrentTime()); | | 437 | + scheduleWork(fiber, expTime); | | 438 | + markRetryTimeIfNotHydrated(fiber, expTime); | | 439 | +} | | 440 | + | 424 | 441 | export {findHostInstance}; | 425 | 442 |
| 426 | 443 | export {findHostInstanceWithWarning}; | |
|