-
Notifications
You must be signed in to change notification settings - Fork 47.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resume immediately pinged fiber without unwinding
If a fiber suspends, and is pinged immediately in a microtask (or a regular task that fires before React resumes rendering), try rendering the same fiber again without unwinding the stack. This can be super helpful when working with promises and async-await, because even if the outermost promise hasn't been cached before, the underlying data may have been preloaded. In many cases, we can continue rendering immediately without having to show a fallback. This optimization should work during any concurrent (time-sliced) render. It doesn't work during discrete updates because those are semantically required to finish synchronously — those get the current behavior.
- Loading branch information
Showing
6 changed files
with
338 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {Wakeable} from 'shared/ReactTypes'; | ||
|
||
let suspendedWakeable: Wakeable | null = null; | ||
let wasPinged = false; | ||
let adHocSuspendCount: number = 0; | ||
|
||
const MAX_AD_HOC_SUSPEND_COUNT = 50; | ||
|
||
export function suspendedWakeableWasPinged() { | ||
return wasPinged; | ||
} | ||
|
||
export function trackSuspendedWakeable(wakeable: Wakeable) { | ||
adHocSuspendCount++; | ||
suspendedWakeable = wakeable; | ||
} | ||
|
||
export function attemptToPingSuspendedWakeable(wakeable: Wakeable) { | ||
if (wakeable === suspendedWakeable) { | ||
// This ping is from the wakeable that just suspended. Mark it as pinged. | ||
// When the work loop resumes, we'll immediately try rendering the fiber | ||
// again instead of unwinding the stack. | ||
wasPinged = true; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
export function resetWakeableState() { | ||
suspendedWakeable = null; | ||
wasPinged = false; | ||
adHocSuspendCount = 0; | ||
} | ||
|
||
export function throwIfInfinitePingLoopDetected() { | ||
if (adHocSuspendCount > MAX_AD_HOC_SUSPEND_COUNT) { | ||
// TODO: Guard against an infinite loop by throwing an error if the same | ||
// component suspends too many times in a row. This should be thrown from | ||
// the render phase so that it gets the component stack. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {Wakeable} from 'shared/ReactTypes'; | ||
|
||
let suspendedWakeable: Wakeable | null = null; | ||
let wasPinged = false; | ||
let adHocSuspendCount: number = 0; | ||
|
||
const MAX_AD_HOC_SUSPEND_COUNT = 50; | ||
|
||
export function suspendedWakeableWasPinged() { | ||
return wasPinged; | ||
} | ||
|
||
export function trackSuspendedWakeable(wakeable: Wakeable) { | ||
adHocSuspendCount++; | ||
suspendedWakeable = wakeable; | ||
} | ||
|
||
export function attemptToPingSuspendedWakeable(wakeable: Wakeable) { | ||
if (wakeable === suspendedWakeable) { | ||
// This ping is from the wakeable that just suspended. Mark it as pinged. | ||
// When the work loop resumes, we'll immediately try rendering the fiber | ||
// again instead of unwinding the stack. | ||
wasPinged = true; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
export function resetWakeableState() { | ||
suspendedWakeable = null; | ||
wasPinged = false; | ||
adHocSuspendCount = 0; | ||
} | ||
|
||
export function throwIfInfinitePingLoopDetected() { | ||
if (adHocSuspendCount > MAX_AD_HOC_SUSPEND_COUNT) { | ||
// TODO: Guard against an infinite loop by throwing an error if the same | ||
// component suspends too many times in a row. This should be thrown from | ||
// the render phase so that it gets the component stack. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.