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

Support sync thenables for lazy() #14626

Merged
merged 2 commits into from Jan 18, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/react-reconciler/src/ReactFiberLazyComponent.js
Expand Up @@ -47,6 +47,7 @@ export function readLazyComponentType<T>(lazyComponent: LazyComponent<T>): T {
lazyComponent._status = Pending;
const ctor = lazyComponent._ctor;
const thenable = ctor();
lazyComponent._result = thenable;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is still needed because we read thenable from lazyComponent._result while it's pending. If we keep the assignment, it will overwrite sync thenables. But if we remove it at all, we will throw null instead of thenable. This fixes both cases.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You still need the assignment, but I don't think you need to move it up here. If you keep it right after the early return branch you added on line 78-80, then there's always a single assignment.

if (lazyComponent._status === Resolved) {
  return lazyComponent._result;
}
lazyComponent._result = thenable;
throw thenable;

thenable.then(
moduleObject => {
if (lazyComponent._status === Pending) {
Expand All @@ -73,7 +74,10 @@ export function readLazyComponentType<T>(lazyComponent: LazyComponent<T>): T {
}
},
);
lazyComponent._result = thenable;
// Check if it resolved synchronously
if (lazyComponent._status === Resolved) {
return lazyComponent._result;
}
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 also do the same for failure. But doesn't seem worth the extra code to me. Could be a recursive call but then there's more risk of messing it up and going into a stack overflow. Meh.

throw thenable;
}
}
Expand Down
17 changes: 17 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js
Expand Up @@ -61,6 +61,23 @@ describe('ReactLazy', () => {
expect(root).toMatchRenderedOutput('Hi again');
});

it('can resolve synchronously without suspending', async () => {
const LazyText = lazy(() => ({
then(cb) {
cb({default: Text});
},
}));

const root = ReactTestRenderer.create(
<Suspense fallback={<Text text="Loading..." />}>
<LazyText text="Hi" />
</Suspense>,
);

expect(ReactTestRenderer).toHaveYielded(['Hi']);
expect(root).toMatchRenderedOutput('Hi');
});

it('multiple lazy components', async () => {
function Foo() {
return <Text text="Foo" />;
Expand Down