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

Add tail="hidden" option to SuspenseList #16024

Merged
merged 5 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 7 additions & 7 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ import {
addSubtreeSuspenseContext,
setShallowSuspenseContext,
} from './ReactFiberSuspenseContext';
import {isShowingAnyFallbacks} from './ReactFiberSuspenseComponent';
import {findFirstSuspended} from './ReactFiberSuspenseComponent';
import {
pushProvider,
propagateContextChange,
Expand Down Expand Up @@ -2000,7 +2000,7 @@ function findLastContentRow(firstChild: null | Fiber): null | Fiber {
while (row !== null) {
let currentRow = row.alternate;
// New rows can't be content rows.
if (currentRow !== null && !isShowingAnyFallbacks(currentRow)) {
if (currentRow !== null && findFirstSuspended(currentRow) === null) {
lastContentRow = row;
}
row = row.sibling;
Expand Down Expand Up @@ -2072,12 +2072,12 @@ function validateTailOptions(
) {
if (__DEV__) {
if (tailMode !== undefined && !didWarnAboutTailOptions[tailMode]) {
if (tailMode !== 'collapsed') {
if (tailMode !== 'collapsed' && tailMode !== 'hidden') {
didWarnAboutTailOptions[tailMode] = true;
warning(
false,
'"%s" is not a supported value for tail on <SuspenseList />. ' +
'Did you mean "collapsed"?',
'Did you mean "collapsed" or "hidden"?',
tailMode,
);
} else if (revealOrder !== 'forwards' && revealOrder !== 'backwards') {
Expand Down Expand Up @@ -2242,10 +2242,10 @@ function updateSuspenseListComponent(
pushSuspenseContext(workInProgress, suspenseContext);

if ((workInProgress.mode & BatchedMode) === NoMode) {
workInProgress.memoizedState = null;
} else {
// Outside of batched mode, SuspenseList doesn't work so we just
// use make it a noop by treating it as the default revealOrder.
workInProgress.memoizedState = null;
} else {
switch (revealOrder) {
case 'forwards': {
let lastContentRow = findLastContentRow(workInProgress.child);
Expand Down Expand Up @@ -2281,7 +2281,7 @@ function updateSuspenseListComponent(
while (row !== null) {
let currentRow = row.alternate;
// New rows can't be content rows.
if (currentRow !== null && !isShowingAnyFallbacks(currentRow)) {
if (currentRow !== null && findFirstSuspended(currentRow) === null) {
// This is the beginning of the main content.
workInProgress.child = row;
break;
Expand Down
55 changes: 53 additions & 2 deletions packages/react-reconciler/src/ReactFiberCompleteWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import {
ForceSuspenseFallback,
setDefaultShallowSuspenseContext,
} from './ReactFiberSuspenseContext';
import {isShowingAnyFallbacks} from './ReactFiberSuspenseComponent';
import {findFirstSuspended} from './ReactFiberSuspenseComponent';
import {
isContextProvider as isLegacyContextProvider,
popContext as popLegacyContext,
Expand Down Expand Up @@ -537,6 +537,32 @@ function cutOffTailIfNeeded(
hasRenderedATailFallback: boolean,
) {
switch (renderState.tailMode) {
case 'hidden': {
// Any insertions at the end of the tail list after this point
// should be invisible. If there are already mounted boundaries
// anything before them are not considered for collapsing.
// Therefore we need to go through the whole tail to find if
// there are any.
let tailNode = renderState.tail;
let lastTailNode = null;
while (tailNode !== null) {
if (tailNode.alternate !== null) {
lastTailNode = tailNode;
}
tailNode = tailNode.sibling;
}
// Next we're simply going to delete all insertions after the
// last rendered item.
if (lastTailNode === null) {
// All remaining items in the tail are insertions.
renderState.tail = null;
} else {
// Detach the insertion after the last node that was already
// inserted.
lastTailNode.sibling = null;
}
break;
}
case 'collapsed': {
// Any insertions at the end of the tail list after this point
// should be invisible. If there are already mounted boundaries
Expand Down Expand Up @@ -1033,10 +1059,34 @@ function completeWork(
} else {
// Append the rendered row to the child list.
if (!didSuspendAlready) {
if (isShowingAnyFallbacks(renderedTail)) {
let suspended = findFirstSuspended(renderedTail);
if (suspended !== null) {
workInProgress.effectTag |= DidCapture;
didSuspendAlready = true;
cutOffTailIfNeeded(renderState, true);
// This might have been modified.
if (
renderState.tail === null &&
renderState.tailMode === 'hidden'
) {
// We need to delete the row we just rendered.
// Ensure we transfer the update queue to the parent.
let newThennables = suspended.updateQueue;
if (newThennables !== null) {
workInProgress.updateQueue = newThennables;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this approach something we'll want to rethink later? By always attaching thenables to the SuspenseList instead of the inner boundary, perhaps.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could possibly do that in throwException, yea.

workInProgress.effectTag |= Update;
}
// Reset the effect list to what it w as before we rendered this
// child. The nested children have already appended themselves.
let lastEffect = (workInProgress.lastEffect =
renderState.lastEffect);
// Remove any effects that were appended after this point.
if (lastEffect !== null) {
lastEffect.nextEffect = null;
}
// We're done.
return null;
}
} else if (
now() > renderState.tailExpiration &&
renderExpirationTime > Never
Expand Down Expand Up @@ -1093,6 +1143,7 @@ function completeWork(
let next = renderState.tail;
renderState.rendering = next;
renderState.tail = next.sibling;
renderState.lastEffect = workInProgress.lastEffect;
next.sibling = null;

// Restore the context.
Expand Down
23 changes: 16 additions & 7 deletions packages/react-reconciler/src/ReactFiberSuspenseComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
*/

import type {Fiber} from './ReactFiber';
import {SuspenseComponent} from 'shared/ReactWorkTags';
import {SuspenseComponent, SuspenseListComponent} from 'shared/ReactWorkTags';
import {NoEffect, DidCapture} from 'shared/ReactSideEffectTags';

// TODO: This is now an empty object. Should we switch this to a boolean?
// Alternatively we can make this use an effect tag similar to SuspenseList.
export type SuspenseState = {||};

export type SuspenseListTailMode = 'collapsed' | void;
export type SuspenseListTailMode = 'collapsed' | 'hidden' | void;

export type SuspenseListRenderState = {|
isBackwards: boolean,
Expand All @@ -28,6 +29,9 @@ export type SuspenseListRenderState = {|
tailExpiration: number,
// Tail insertions setting.
tailMode: SuspenseListTailMode,
// Last Effect before we rendered the "rendering" item.
// Used to remove new effects added by the rendered item.
lastEffect: null | Fiber,
|};

export function shouldCaptureSuspense(
Expand Down Expand Up @@ -58,30 +62,35 @@ export function shouldCaptureSuspense(
return true;
}

export function isShowingAnyFallbacks(row: Fiber): boolean {
export function findFirstSuspended(row: Fiber): null | Fiber {
let node = row;
while (node !== null) {
if (node.tag === SuspenseComponent) {
const state: SuspenseState | null = node.memoizedState;
if (state !== null) {
return true;
return node;
}
} else if (node.tag === SuspenseListComponent) {
let didSuspend = (node.effectTag & DidCapture) !== NoEffect;
if (didSuspend) {
return node;
}
} else if (node.child !== null) {
node.child.return = node;
node = node.child;
continue;
}
if (node === row) {
return false;
return null;
}
while (node.sibling === null) {
if (node.return === null || node.return === row) {
return false;
return null;
}
node = node.return;
}
node.sibling.return = node.return;
node = node.sibling;
}
return false;
return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ describe('ReactSuspenseList', () => {

expect(() => Scheduler.unstable_flushAll()).toWarnDev([
'Warning: "collapse" is not a supported value for tail on ' +
'<SuspenseList />. Did you mean "collapsed"?' +
'<SuspenseList />. Did you mean "collapsed" or "hidden"?' +
'\n in SuspenseList (at **)' +
'\n in Foo (at **)',
]);
Expand Down Expand Up @@ -1733,4 +1733,67 @@ describe('ReactSuspenseList', () => {
</Fragment>,
);
});

it('only shows no initial loading state "hidden" tail insertions', async () => {
let A = createAsyncText('A');
let B = createAsyncText('B');
let C = createAsyncText('C');

function Foo() {
return (
<SuspenseList revealOrder="forwards" tail="hidden">
<Suspense fallback={<Text text="Loading A" />}>
<A />
</Suspense>
<Suspense fallback={<Text text="Loading B" />}>
<B />
</Suspense>
<Suspense fallback={<Text text="Loading C" />}>
<C />
</Suspense>
</SuspenseList>
);
}

ReactNoop.render(<Foo />);

expect(Scheduler).toFlushAndYield(['Suspend! [A]', 'Loading A']);

expect(ReactNoop).toMatchRenderedOutput(null);

await A.resolve();

expect(Scheduler).toFlushAndYield(['A', 'Suspend! [B]', 'Loading B']);

// Incremental loading is suspended.
jest.advanceTimersByTime(500);

expect(ReactNoop).toMatchRenderedOutput(<span>A</span>);

await B.resolve();

expect(Scheduler).toFlushAndYield(['B', 'Suspend! [C]', 'Loading C']);

// Incremental loading is suspended.
jest.advanceTimersByTime(500);

expect(ReactNoop).toMatchRenderedOutput(
<Fragment>
<span>A</span>
<span>B</span>
</Fragment>,
);

await C.resolve();

expect(Scheduler).toFlushAndYield(['C']);

expect(ReactNoop).toMatchRenderedOutput(
<Fragment>
<span>A</span>
<span>B</span>
<span>C</span>
</Fragment>,
);
});
});