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
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) {
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
);