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

Remove _ctor field from Lazy components #18217

Merged
merged 6 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 3 additions & 7 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@

import type {ThreadID} from './ReactThreadIDAllocator';
import type {ReactElement} from 'shared/ReactElementType';
import type {LazyComponent} from 'shared/ReactLazyComponent';
import type {LazyComponent} from 'react/src/ReactLazy';
import type {ReactProvider, ReactContext} from 'shared/ReactTypes';

import * as React from 'react';
import invariant from 'shared/invariant';
import getComponentName from 'shared/getComponentName';
import describeComponentFrame from 'shared/describeComponentFrame';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {
Resolved,
Rejected,
Pending,
initializeLazyComponentType,
} from 'shared/ReactLazyComponent';
import {initializeLazyComponentType} from 'shared/ReactLazyComponent';
import {Resolved, Rejected, Pending} from 'react/src/ReactLazy';
import {
warnAboutDeprecatedLifecycles,
disableLegacyContext,
Expand Down
5 changes: 3 additions & 2 deletions packages/react-reconciler/src/ReactFiberLazyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
* @flow
*/

import type {LazyComponent} from 'shared/ReactLazyComponent';
import type {LazyComponent} from 'react/src/ReactLazy';

import {Resolved, initializeLazyComponentType} from 'shared/ReactLazyComponent';
import {Resolved} from 'react/src/ReactLazy';
import {initializeLazyComponentType} from 'shared/ReactLazyComponent';

export function resolveDefaultProps(Component: any, baseProps: Object): Object {
if (Component && Component.defaultProps) {
Expand Down
57 changes: 50 additions & 7 deletions packages/react/src/ReactLazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,66 @@
*
* 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 {LazyComponent, Thenable} from 'shared/ReactLazyComponent';

import {REACT_LAZY_TYPE} from 'shared/ReactSymbols';

export function lazy<T, R>(ctor: () => Thenable<T, R>): LazyComponent<T> {
let lazyType = {
type Thenable<T, R> = {
then(resolve: (T) => mixed, reject: (mixed) => mixed): R,
};

export type UninitializedLazyComponent<T> = {
$$typeof: Symbol | number,
_status: -1,
_result: () => Thenable<{default: T, ...} | T, mixed>,
};

export type PendingLazyComponent<T> = {
$$typeof: Symbol | number,
_status: 0,
_result: Thenable<{default: T, ...} | T, mixed>,
};

export type ResolvedLazyComponent<T> = {
$$typeof: Symbol | number,
_status: 1,
_result: T,
};

export type RejectedLazyComponent = {
$$typeof: Symbol | number,
_status: 2,
_result: mixed,
};

export type LazyComponent<T> =
| UninitializedLazyComponent<T>
| PendingLazyComponent<T>
| ResolvedLazyComponent<T>
| RejectedLazyComponent;

export const Uninitialized = -1;
export const Pending = 0;
export const Resolved = 1;
export const Rejected = 2;

export function lazy<T>(
ctor: () => Thenable<{default: T, ...} | T, mixed>,
): LazyComponent<T> {
let lazyType: LazyComponent<T> = {
$$typeof: REACT_LAZY_TYPE,
_ctor: ctor,
// React uses these fields to store the result.
_status: -1,
_result: null,
_status: Uninitialized,
_result: ctor,
};

if (__DEV__) {
// In production, this would just set it on the object.
let defaultProps;
let propTypes;
// $FlowFixMe
Object.defineProperties(lazyType, {
defaultProps: {
configurable: true,
Expand All @@ -36,6 +77,7 @@ export function lazy<T, R>(ctor: () => Thenable<T, R>): LazyComponent<T> {
);
defaultProps = newDefaultProps;
// Match production behavior more closely:
// $FlowFixMe
Object.defineProperty(lazyType, 'defaultProps', {
enumerable: true,
});
Expand All @@ -54,6 +96,7 @@ export function lazy<T, R>(ctor: () => Thenable<T, R>): LazyComponent<T> {
);
propTypes = newPropTypes;
// Match production behavior more closely:
// $FlowFixMe
Object.defineProperty(lazyType, 'propTypes', {
enumerable: true,
});
Expand Down
57 changes: 25 additions & 32 deletions packages/shared/ReactLazyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,35 @@
* @flow
*/

export type Thenable<T, R> = {
then(resolve: (T) => mixed, reject: (mixed) => mixed): R,
...
};
import type {
PendingLazyComponent,
ResolvedLazyComponent,
RejectedLazyComponent,
LazyComponent,
} from 'react/src/ReactLazy';

export type LazyComponent<T> = {
$$typeof: Symbol | number,
_ctor: () => Thenable<{default: T, ...}, mixed>,
_status: 0 | 1 | 2,
_result: any,
...
};

type ResolvedLazyComponent<T> = {
$$typeof: Symbol | number,
_ctor: () => Thenable<{default: T, ...}, mixed>,
_status: 1,
_result: any,
...
};

export const Uninitialized = -1;
export const Pending = 0;
export const Resolved = 1;
export const Rejected = 2;
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 also upstreamed all of this to the isomorphic package since it owns this data structure. I'll think a bit how we can move the other two helpers out of /shared.

import {Uninitialized, Pending, Resolved, Rejected} from 'react/src/ReactLazy';

export function refineResolvedLazyComponent<T>(
lazyComponent: LazyComponent<T>,
): ResolvedLazyComponent<T> | null {
): T | null {
return lazyComponent._status === Resolved ? lazyComponent._result : null;
}

export function initializeLazyComponentType(
lazyComponent: LazyComponent<any>,
): void {
if (lazyComponent._status === Uninitialized) {
lazyComponent._status = Pending;
const ctor = lazyComponent._ctor;
let ctor = lazyComponent._result;
if (!ctor) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Coercion is OK here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When we detect existence of something unknown things we tend to do this type of check.

// TODO: Remove this later. THis only exists in case you use an older "react" package.
ctor = ((lazyComponent: any)._ctor: typeof ctor);
}
const thenable = ctor();
lazyComponent._result = thenable;
// Transition to the next state.
const pending: PendingLazyComponent<any> = (lazyComponent: any);
pending._status = Pending;
pending._result = thenable;
thenable.then(
moduleObject => {
if (lazyComponent._status === Pending) {
Expand All @@ -61,14 +50,18 @@ export function initializeLazyComponentType(
);
}
}
lazyComponent._status = Resolved;
lazyComponent._result = defaultExport;
// Transition to the next state.
const resolved: ResolvedLazyComponent<any> = (lazyComponent: any);
resolved._status = Resolved;
resolved._result = defaultExport;
}
},
error => {
if (lazyComponent._status === Pending) {
lazyComponent._status = Rejected;
lazyComponent._result = error;
// Transition to the next state.
const rejected: RejectedLazyComponent = (lazyComponent: any);
rejected._status = Rejected;
rejected._result = error;
}
},
);
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/getComponentName.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

import type {LazyComponent} from 'shared/ReactLazyComponent';
import type {LazyComponent} from 'react/src/ReactLazy';

import {
REACT_CONTEXT_TYPE,
Expand Down