Skip to content
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

Suspense for CPU-bound trees #19936

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions packages/react-reconciler/src/ReactFiberBeginWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import {
SyncLane,
OffscreenLane,
DefaultHydrationLane,
SomeRetryLane,
NoTimestamp,
includesSomeLane,
laneToLanes,
Expand Down Expand Up @@ -1658,6 +1659,7 @@ function updateSuspenseOffscreenState(
};
}

// TODO: Probably should inline this back
function shouldRemainOnFallback(
suspenseContext: SuspenseContext,
current: null | Fiber,
Expand Down Expand Up @@ -1790,9 +1792,9 @@ function updateSuspenseComponent(current, workInProgress, renderLanes) {
}
}

const nextPrimaryChildren = nextProps.children;
const nextFallbackChildren = nextProps.fallback;
if (showFallback) {
const nextPrimaryChildren = nextProps.children;
const nextFallbackChildren = nextProps.fallback;
const fallbackFragment = mountSuspenseFallbackChildren(
workInProgress,
nextPrimaryChildren,
Expand All @@ -1805,8 +1807,36 @@ function updateSuspenseComponent(current, workInProgress, renderLanes) {
);
workInProgress.memoizedState = SUSPENDED_MARKER;
return fallbackFragment;
} else if (typeof nextProps.unstable_expectedLoadTime === 'number') {
// This is a CPU-bound tree. Skip this tree and show a placeholder to
// unblock the surrounding content. Then immediately retry after the
// initial commit.
const fallbackFragment = mountSuspenseFallbackChildren(
workInProgress,
nextPrimaryChildren,
nextFallbackChildren,
renderLanes,
);
const primaryChildFragment: Fiber = (workInProgress.child: any);
primaryChildFragment.memoizedState = mountSuspenseOffscreenState(
renderLanes,
);
workInProgress.memoizedState = SUSPENDED_MARKER;

// Since nothing actually suspended, there will nothing to ping this to
// get it started back up to attempt the next item. While in terms of
// priority this work has the same priority as this current render, it's
// not part of the same transition once the transition has committed. If
// it's sync, we still want to yield so that it can be painted.
// Conceptually, this is really the same as pinging. We can use any
// RetryLane even if it's the one currently rendering since we're leaving
// it behind on this node.
workInProgress.lanes = SomeRetryLane;
if (enableSchedulerTracing) {
markSpawnedWork(SomeRetryLane);
}
return fallbackFragment;
} else {
const nextPrimaryChildren = nextProps.children;
return mountSuspensePrimaryChildren(
workInProgress,
nextPrimaryChildren,
Expand Down
36 changes: 33 additions & 3 deletions packages/react-reconciler/src/ReactFiberBeginWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import {
SyncLane,
OffscreenLane,
DefaultHydrationLane,
SomeRetryLane,
NoTimestamp,
includesSomeLane,
laneToLanes,
Expand Down Expand Up @@ -1657,6 +1658,7 @@ function updateSuspenseOffscreenState(
};
}

// TODO: Probably should inline this back
function shouldRemainOnFallback(
suspenseContext: SuspenseContext,
current: null | Fiber,
Expand Down Expand Up @@ -1789,9 +1791,9 @@ function updateSuspenseComponent(current, workInProgress, renderLanes) {
}
}

const nextPrimaryChildren = nextProps.children;
const nextFallbackChildren = nextProps.fallback;
if (showFallback) {
const nextPrimaryChildren = nextProps.children;
const nextFallbackChildren = nextProps.fallback;
const fallbackFragment = mountSuspenseFallbackChildren(
workInProgress,
nextPrimaryChildren,
Expand All @@ -1804,8 +1806,36 @@ function updateSuspenseComponent(current, workInProgress, renderLanes) {
);
workInProgress.memoizedState = SUSPENDED_MARKER;
return fallbackFragment;
} else if (typeof nextProps.unstable_expectedLoadTime === 'number') {
// This is a CPU-bound tree. Skip this tree and show a placeholder to
// unblock the surrounding content. Then immediately retry after the
// initial commit.
const fallbackFragment = mountSuspenseFallbackChildren(
workInProgress,
nextPrimaryChildren,
nextFallbackChildren,
renderLanes,
);
const primaryChildFragment: Fiber = (workInProgress.child: any);
primaryChildFragment.memoizedState = mountSuspenseOffscreenState(
renderLanes,
);
workInProgress.memoizedState = SUSPENDED_MARKER;

// Since nothing actually suspended, there will nothing to ping this to
// get it started back up to attempt the next item. While in terms of
// priority this work has the same priority as this current render, it's
// not part of the same transition once the transition has committed. If
// it's sync, we still want to yield so that it can be painted.
// Conceptually, this is really the same as pinging. We can use any
// RetryLane even if it's the one currently rendering since we're leaving
// it behind on this node.
workInProgress.lanes = SomeRetryLane;
if (enableSchedulerTracing) {
markSpawnedWork(SomeRetryLane);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a test case to DebugTracing-test for this

#19943

}
return fallbackFragment;
} else {
const nextPrimaryChildren = nextProps.children;
return mountSuspensePrimaryChildren(
workInProgress,
nextPrimaryChildren,
Expand Down
11 changes: 11 additions & 0 deletions packages/react-reconciler/src/ReactFiberSuspenseComponent.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow
*/

import type {ReactNodeList, Wakeable} from 'shared/ReactTypes';
import type {Fiber} from './ReactInternalTypes';
import type {SuspenseInstance} from './ReactFiberHostConfig';
import type {Lane} from './ReactFiberLane';
Expand All @@ -17,6 +18,16 @@ import {
isSuspenseInstanceFallback,
} from './ReactFiberHostConfig';

export type SuspenseProps = {|
children?: ReactNodeList,
fallback?: ReactNodeList,

// TODO: Add "unstable_" prefix?
suspenseCallback?: (Set<Wakeable> | null) => mixed,

unstable_expectedLoadTime?: number,
|};

// A null SuspenseState represents an unsuspended normal Suspense boundary.
// A non-null SuspenseState means that it is blocked for one reason or another.
// - A non-null dehydrated field means it's blocked pending hydration.
Expand Down
11 changes: 11 additions & 0 deletions packages/react-reconciler/src/ReactFiberSuspenseComponent.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow
*/

import type {ReactNodeList, Wakeable} from 'shared/ReactTypes';
import type {Fiber} from './ReactInternalTypes';
import type {SuspenseInstance} from './ReactFiberHostConfig';
import type {Lane} from './ReactFiberLane';
Expand All @@ -17,6 +18,16 @@ import {
isSuspenseInstanceFallback,
} from './ReactFiberHostConfig';

export type SuspenseProps = {|
children?: ReactNodeList,
fallback?: ReactNodeList,

// TODO: Add "unstable_" prefix?
suspenseCallback?: (Set<Wakeable> | null) => mixed,

unstable_expectedLoadTime?: number,
|};

// A null SuspenseState represents an unsuspended normal Suspense boundary.
// A non-null SuspenseState means that it is blocked for one reason or another.
// - A non-null dehydrated field means it's blocked pending hydration.
Expand Down
Loading