-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Suspense Boundary Context (and unstable_avoidThisFallback) #15578
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
83 changes: 83 additions & 0 deletions
83
packages/react-reconciler/src/ReactFiberSuspenseContext.js
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,83 @@ | ||
/** | ||
* 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 {Fiber} from './ReactFiber'; | ||
import type {StackCursor} from './ReactFiberStack'; | ||
|
||
import {createCursor, push, pop} from './ReactFiberStack'; | ||
|
||
export opaque type SuspenseContext = number; | ||
export opaque type SubtreeSuspenseContext: SuspenseContext = number; | ||
export opaque type ShallowSuspenseContext: SuspenseContext = number; | ||
|
||
const DefaultSuspenseContext: SuspenseContext = 0b00; | ||
|
||
// The Suspense Context is split into two parts. The lower bits is | ||
// inherited deeply down the subtree. The upper bits only affect | ||
// this immediate suspense boundary and gets reset each new | ||
// boundary or suspense list. | ||
const SubtreeSuspenseContextMask: SuspenseContext = 0b01; | ||
|
||
// Subtree Flags: | ||
|
||
// InvisibleParentSuspenseContext indicates that one of our parent Suspense | ||
// boundaries is not currently showing visible main content. | ||
// Either because it is already showing a fallback or is not mounted at all. | ||
// We can use this to determine if it is desirable to trigger a fallback at | ||
// the parent. If not, then we might need to trigger undesirable boundaries | ||
// and/or suspend the commit to avoid hiding the parent content. | ||
export const InvisibleParentSuspenseContext: SubtreeSuspenseContext = 0b01; | ||
|
||
// Shallow Flags: | ||
|
||
// ForceSuspenseFallback can be used by SuspenseList to force newly added | ||
// items into their fallback state during one of the render passes. | ||
export const ForceSuspenseFallback: ShallowSuspenseContext = 0b10; | ||
|
||
export const suspenseStackCursor: StackCursor<SuspenseContext> = createCursor( | ||
DefaultSuspenseContext, | ||
); | ||
|
||
export function hasSuspenseContext( | ||
parentContext: SuspenseContext, | ||
flag: SuspenseContext, | ||
): boolean { | ||
return (parentContext & flag) !== 0; | ||
} | ||
|
||
export function setDefaultShallowSuspenseContext( | ||
parentContext: SuspenseContext, | ||
): SuspenseContext { | ||
return parentContext & SubtreeSuspenseContextMask; | ||
} | ||
|
||
export function setShallowSuspenseContext( | ||
parentContext: SuspenseContext, | ||
shallowContext: ShallowSuspenseContext, | ||
): SuspenseContext { | ||
return (parentContext & SubtreeSuspenseContextMask) | shallowContext; | ||
} | ||
|
||
export function addSubtreeSuspenseContext( | ||
parentContext: SuspenseContext, | ||
subtreeContext: SubtreeSuspenseContext, | ||
): SuspenseContext { | ||
return parentContext | subtreeContext; | ||
} | ||
|
||
export function pushSuspenseContext( | ||
fiber: Fiber, | ||
newContext: SuspenseContext, | ||
): void { | ||
push(suspenseStackCursor, newContext, fiber); | ||
} | ||
|
||
export function popSuspenseContext(fiber: Fiber): void { | ||
pop(suspenseStackCursor, fiber); | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this name confusing but don't have a better suggestion