@samitouri / QOS-React-2 / commits / fa03206ee4

Remove _ctor field from Lazy components (#18217)

* This type is all wrong and nothing cares because it's all any * Refine Flow types of Lazy Components We can type each condition. * Remove _ctor field from Lazy components This field is not needed because it's only used before we've initialized, and we don't have anything else to store before we've initialized. * Check for _ctor in case it's an older isomorphic that created it We try not to break across minors but it's no guarantee. * Move types and constants from shared to isomorphic The "react" package owns the data structure of the Lazy component. It creates it and decides how any downstream renderer may use it. * Move constants to shared Apparently we can't depend on react/src/ because the whole package is considered "external" as far as rollup is concerned.

Sebastian Markbåge committed Mar 4, 2020 at 20:52 UTC fa03206ee4352431198b678c00dca83027e7052d
6 files changed +95 -48
packages/react-dom/src/server/ReactPartialRenderer.js
+3 -7
@@ -9,7 +9,7 @@
9
10 import type {ThreadID} from './ReactThreadIDAllocator';
11 import type {ReactElement} from 'shared/ReactElementType';
12 -import type {LazyComponent} from 'shared/ReactLazyComponent';
12 +import type {LazyComponent} from 'react/src/ReactLazy';
13 import type {ReactProvider, ReactContext} from 'shared/ReactTypes';
14
15 import * as React from 'react';
@@ -17,12 +17,8 @@ import invariant from 'shared/invariant';
17 import getComponentName from 'shared/getComponentName';
18 import describeComponentFrame from 'shared/describeComponentFrame';
19 import ReactSharedInternals from 'shared/ReactSharedInternals';
20 -import {
21 - Resolved,
22 - Rejected,
23 - Pending,
24 - initializeLazyComponentType,
25 -} from 'shared/ReactLazyComponent';
20 +import {initializeLazyComponentType} from 'shared/ReactLazyComponent';
21 +import {Resolved, Rejected, Pending} from 'shared/ReactLazyStatusTags';
22 import {
23 warnAboutDeprecatedLifecycles,
24 disableLegacyContext,
packages/react-reconciler/src/ReactFiberLazyComponent.js
+3 -2
@@ -7,9 +7,10 @@
7 * @flow
8 */
9
10 -import type {LazyComponent} from 'shared/ReactLazyComponent';
10 +import type {LazyComponent} from 'react/src/ReactLazy';
11
12 -import {Resolved, initializeLazyComponentType} from 'shared/ReactLazyComponent';
12 +import {Resolved} from 'shared/ReactLazyStatusTags';
13 +import {initializeLazyComponentType} from 'shared/ReactLazyComponent';
14
15 export function resolveDefaultProps(Component: any, baseProps: Object): Object {
16 if (Component && Component.defaultProps) {
packages/react/src/ReactLazy.js
+44 -6
@@ -3,25 +3,61 @@
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 + *
7 + * @flow
8 */
9
8 -import type {LazyComponent, Thenable} from 'shared/ReactLazyComponent';
9 -
10 import {REACT_LAZY_TYPE} from 'shared/ReactSymbols';
11
12 -export function lazy<T, R>(ctor: () => Thenable<T, R>): LazyComponent<T> {
13 - let lazyType = {
12 +type Thenable<T, R> = {
13 + then(resolve: (T) => mixed, reject: (mixed) => mixed): R,
14 +};
15 +
16 +export type UninitializedLazyComponent<T> = {
17 + $$typeof: Symbol | number,
18 + _status: -1,
19 + _result: () => Thenable<{default: T, ...} | T, mixed>,
20 +};
21 +
22 +export type PendingLazyComponent<T> = {
23 + $$typeof: Symbol | number,
24 + _status: 0,
25 + _result: Thenable<{default: T, ...} | T, mixed>,
26 +};
27 +
28 +export type ResolvedLazyComponent<T> = {
29 + $$typeof: Symbol | number,
30 + _status: 1,
31 + _result: T,
32 +};
33 +
34 +export type RejectedLazyComponent = {
35 + $$typeof: Symbol | number,
36 + _status: 2,
37 + _result: mixed,
38 +};
39 +
40 +export type LazyComponent<T> =
41 + | UninitializedLazyComponent<T>
42 + | PendingLazyComponent<T>
43 + | ResolvedLazyComponent<T>
44 + | RejectedLazyComponent;
45 +
46 +export function lazy<T>(
47 + ctor: () => Thenable<{default: T, ...} | T, mixed>,
48 +): LazyComponent<T> {
49 + let lazyType: LazyComponent<T> = {
50 $$typeof: REACT_LAZY_TYPE,
15 - _ctor: ctor,
51 // React uses these fields to store the result.
52 _status: -1,
18 - _result: null,
53 + _result: ctor,
54 };
55
56 if (__DEV__) {
57 // In production, this would just set it on the object.
58 let defaultProps;
59 let propTypes;
60 + // $FlowFixMe
61 Object.defineProperties(lazyType, {
62 defaultProps: {
63 configurable: true,
@@ -36,6 +72,7 @@ export function lazy<T, R>(ctor: () => Thenable<T, R>): LazyComponent<T> {
72 );
73 defaultProps = newDefaultProps;
74 // Match production behavior more closely:
75 + // $FlowFixMe
76 Object.defineProperty(lazyType, 'defaultProps', {
77 enumerable: true,
78 });
@@ -54,6 +91,7 @@ export function lazy<T, R>(ctor: () => Thenable<T, R>): LazyComponent<T> {
91 );
92 propTypes = newPropTypes;
93 // Match production behavior more closely:
94 + // $FlowFixMe
95 Object.defineProperty(lazyType, 'propTypes', {
96 enumerable: true,
97 });
packages/shared/ReactLazyComponent.js
+30 -32
@@ -7,35 +7,23 @@
7 * @flow
8 */
9
10 -export type Thenable<T, R> = {
11 - then(resolve: (T) => mixed, reject: (mixed) => mixed): R,
12 - ...
13 -};
10 +import type {
11 + PendingLazyComponent,
12 + ResolvedLazyComponent,
13 + RejectedLazyComponent,
14 + LazyComponent,
15 +} from 'react/src/ReactLazy';
16
15 -export type LazyComponent<T> = {
16 - $$typeof: Symbol | number,
17 - _ctor: () => Thenable<{default: T, ...}, mixed>,
18 - _status: 0 | 1 | 2,
19 - _result: any,
20 - ...
21 -};
22 -
23 -type ResolvedLazyComponent<T> = {
24 - $$typeof: Symbol | number,
25 - _ctor: () => Thenable<{default: T, ...}, mixed>,
26 - _status: 1,
27 - _result: any,
28 - ...
29 -};
30 -
31 -export const Uninitialized = -1;
32 -export const Pending = 0;
33 -export const Resolved = 1;
34 -export const Rejected = 2;
17 +import {
18 + Uninitialized,
19 + Pending,
20 + Resolved,
21 + Rejected,
22 +} from './ReactLazyStatusTags';
23
24 export function refineResolvedLazyComponent<T>(
25 lazyComponent: LazyComponent<T>,
38 -): ResolvedLazyComponent<T> | null {
26 +): T | null {
27 return lazyComponent._status === Resolved ? lazyComponent._result : null;
28 }
29
@@ -43,10 +31,16 @@ export function initializeLazyComponentType(
31 lazyComponent: LazyComponent<any>,
32 ): void {
33 if (lazyComponent._status === Uninitialized) {
46 - lazyComponent._status = Pending;
47 - const ctor = lazyComponent._ctor;
34 + let ctor = lazyComponent._result;
35 + if (!ctor) {
36 + // TODO: Remove this later. THis only exists in case you use an older "react" package.
37 + ctor = ((lazyComponent: any)._ctor: typeof ctor);
38 + }
39 const thenable = ctor();
49 - lazyComponent._result = thenable;
40 + // Transition to the next state.
41 + const pending: PendingLazyComponent<any> = (lazyComponent: any);
42 + pending._status = Pending;
43 + pending._result = thenable;
44 thenable.then(
45 moduleObject => {
46 if (lazyComponent._status === Pending) {
@@ -61,14 +55,18 @@ export function initializeLazyComponentType(
55 );
56 }
57 }
64 - lazyComponent._status = Resolved;
65 - lazyComponent._result = defaultExport;
58 + // Transition to the next state.
59 + const resolved: ResolvedLazyComponent<any> = (lazyComponent: any);
60 + resolved._status = Resolved;
61 + resolved._result = defaultExport;
62 }
63 },
64 error => {
65 if (lazyComponent._status === Pending) {
70 - lazyComponent._status = Rejected;
71 - lazyComponent._result = error;
66 + // Transition to the next state.
67 + const rejected: RejectedLazyComponent = (lazyComponent: any);
68 + rejected._status = Rejected;
69 + rejected._result = error;
70 }
71 },
72 );
packages/shared/ReactLazyStatusTags.js new
+14
@@ -0,0 +1,14 @@
1 +/**
2 + * Copyright (c) Facebook, Inc. and its affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + *
7 + * @flow
8 + */
9 +
10 +// TODO: Move this to "react" once we can import from externals.
11 +export const Uninitialized = -1;
12 +export const Pending = 0;
13 +export const Resolved = 1;
14 +export const Rejected = 2;
packages/shared/getComponentName.js
+1 -1
@@ -7,7 +7,7 @@
7 * @flow
8 */
9
10 -import type {LazyComponent} from 'shared/ReactLazyComponent';
10 +import type {LazyComponent} from 'react/src/ReactLazy';
11
12 import {
13 REACT_CONTEXT_TYPE,