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

Unwind the current workInProgress if it's suspended #25247

Merged
merged 1 commit into from
Sep 13, 2022
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
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,9 @@ function prepareFreshStack(root: FiberRoot, lanes: Lanes): Fiber {
}

if (workInProgress !== null) {
let interruptedWork = workInProgress.return;
let interruptedWork = workInProgressIsSuspended
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not 100% sure this flag means what I think it means.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It does, maybe add a one-line comment explaining why this works. I also like your idea to always yield right after the begin phase though it would require more refactoring.

? workInProgress
: workInProgress.return;
while (interruptedWork !== null) {
const current = interruptedWork.alternate;
unwindInterruptedWork(
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,9 @@ function prepareFreshStack(root: FiberRoot, lanes: Lanes): Fiber {
}

if (workInProgress !== null) {
let interruptedWork = workInProgress.return;
let interruptedWork = workInProgressIsSuspended
? workInProgress
: workInProgress.return;
while (interruptedWork !== null) {
const current = interruptedWork.alternate;
unwindInterruptedWork(
Expand Down
47 changes: 47 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactWakeable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,51 @@ describe('ReactWakeable', () => {
expect(Scheduler).toFlushWithoutYielding();
expect(root).toMatchRenderedOutput('AB');
});

// @gate enableUseHook
test('interrupting while yielded should reset contexts', async () => {
let resolve;
const promise = new Promise(r => {
resolve = r;
});

const Context = React.createContext();

const lazy = React.lazy(() => {
return promise;
});

function ContextText() {
return <Text text={use(Context)} />;
}

function App({text}) {
return (
<div>
<Context.Provider value={text}>
{lazy}
<ContextText />
</Context.Provider>
</div>
);
}

const root = ReactNoop.createRoot();
startTransition(() => {
root.render(<App text="world" />);
});
expect(Scheduler).toFlushUntilNextPaint([]);
expect(root).toMatchRenderedOutput(null);

await resolve({default: <Text key="hi" text="Hello " />});

// Higher priority update that interrupts the first render
ReactNoop.flushSync(() => {
root.render(<App text="world!" />);
});

expect(Scheduler).toHaveYielded(['Hello ', 'world!']);

expect(root).toMatchRenderedOutput(<div>Hello world!</div>);
});
});