-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathlazyActivityPlugin.ts
More file actions
147 lines (125 loc) · 3.44 KB
/
lazyActivityPlugin.ts
File metadata and controls
147 lines (125 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { ActivityComponentType } from "../__internal__/ActivityComponentType";
import type { StackflowReactPlugin } from "../__internal__/StackflowReactPlugin";
// https://github.com/facebook/react/blob/v19.1.1/packages/shared/ReactSymbols.js#L32
const REACT_LAZY_TYPE: symbol = Symbol.for("react.lazy");
const REACT_MEMO_TYPE: symbol = Symbol.for("react.memo");
// https://github.com/facebook/react/blob/v19.1.1/packages/react/src/ReactLazy.js
interface Wakeable {
then(onFulfill: () => unknown, onReject: () => unknown): undefined | Wakeable;
}
interface ThenableImpl<T> {
then(
onFulfill: (value: T) => unknown,
onReject: (error: unknown) => unknown,
): undefined | Wakeable;
}
interface UntrackedThenable<T> extends ThenableImpl<T> {
status?: undefined;
}
interface PendingThenable<T> extends ThenableImpl<T> {
status: "pending";
}
interface FulfilledThenable<T> extends ThenableImpl<T> {
status: "fulfilled";
value: T;
}
interface RejectedThenable<T> extends ThenableImpl<T> {
status: "rejected";
reason: unknown;
}
type Thenable<T> =
| UntrackedThenable<T>
| PendingThenable<T>
| FulfilledThenable<T>
| RejectedThenable<T>;
const Uninitialized = -1;
const Pending = 0;
const Resolved = 1;
const Rejected = 2;
type UninitializedPayload<T> = {
_status: -1;
_result: () => Thenable<{ default: T }>;
};
type PendingPayload = {
_status: 0;
_result: Wakeable;
};
type ResolvedPayload<T> = {
_status: 1;
_result: { default: T };
};
type RejectedPayload = {
_status: 2;
_result: unknown;
};
type Payload<T> =
| UninitializedPayload<T>
| PendingPayload
| ResolvedPayload<T>
| RejectedPayload;
type LazyComponent = {
$$typeof: symbol | number;
_payload: Payload<unknown>;
};
// https://github.com/facebook/react/blob/v19.1.1/packages/react/src/ReactMemo.js
type MemoComponent = {
$$typeof: symbol | number;
type: React.ElementType;
};
function isLazyComponent(component: unknown): component is LazyComponent {
const isLazy =
typeof component === "object" &&
component !== null &&
"$$typeof" in component &&
component.$$typeof === REACT_LAZY_TYPE &&
"_payload" in component;
return isLazy;
}
function isMemoComponent(component: unknown): component is MemoComponent {
const isMemo =
typeof component === "object" &&
component !== null &&
"$$typeof" in component &&
component.$$typeof === REACT_MEMO_TYPE &&
"type" in component;
return isMemo;
}
export function lazyActivityPlugin(activityComponentMap: {
[key: string]: ActivityComponentType;
}): StackflowReactPlugin {
function handleLazyActivity({
actions,
actionParams,
}: {
actions: { pause: () => void; resume: () => void };
actionParams: { activityName: string };
}) {
let Activity = activityComponentMap[actionParams.activityName];
while (isMemoComponent(Activity)) {
Activity = Activity.type as ActivityComponentType;
}
if (
isLazyComponent(Activity) &&
Activity._payload._status === Uninitialized
) {
actions.pause();
Activity._payload._result().then(
() => {
actions.resume();
},
() => {
actions.resume();
},
);
}
}
return () => ({
key: "plugin-lazy-activity",
onBeforePush({ actions, actionParams }) {
handleLazyActivity({ actions, actionParams });
},
onBeforeReplace({ actions, actionParams }) {
handleLazyActivity({ actions, actionParams });
},
});
}