-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdev-runtime-session.ts
More file actions
187 lines (166 loc) · 5.53 KB
/
Copy pathdev-runtime-session.ts
File metadata and controls
187 lines (166 loc) · 5.53 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type { RsbuildDevServer } from '@rsbuild/core';
import { PLUGIN_NAME } from './constants.js';
import type { ReactRouterDevRuntime } from './dev-generation.js';
import type { DevCompilerPair } from './dev-runtime-compilation.js';
import { normalizeEffectError } from './effect-runtime.js';
export type RuntimeBinding = {
id: number;
server: RsbuildDevServer;
runtime: ReactRouterDevRuntime;
compilers?: DevCompilerPair;
};
type CloseOutcome = { ok: true } | { ok: false; cause: unknown };
type CloseObservation = {
binding?: RuntimeBinding;
promise?: Promise<void>;
outcome?: CloseOutcome;
};
export type ControllerState =
| { status: 'idle' }
| { status: 'active'; binding: RuntimeBinding }
| { status: 'closing'; binding: RuntimeBinding }
| { status: 'terminal'; error: Error };
type CloseBinding = (binding: RuntimeBinding, error?: Error) => void;
export type DevRuntimeSessionManager = {
getState(): ControllerState;
getActiveBinding(): RuntimeBinding | undefined;
observeClose(server: RsbuildDevServer): void;
assertCanStart(): void;
createBinding(
server: RsbuildDevServer,
runtime: ReactRouterDevRuntime
): RuntimeBinding;
bindCloseObservation(binding: RuntimeBinding): void;
markClosing(binding: RuntimeBinding): void;
terminate(binding: RuntimeBinding, error: Error): void;
};
export const createDevRuntimeSessionManager = (
closeBinding: CloseBinding
): DevRuntimeSessionManager => {
let state: ControllerState = { status: 'idle' };
let nextSessionId = 1;
const closeObservationByServer = new WeakMap<
RsbuildDevServer,
CloseObservation
>();
const getState = (): ControllerState => state;
const getActiveBinding = (): RuntimeBinding | undefined =>
state.status === 'active' ? state.binding : undefined;
const isCurrentBinding = (binding: RuntimeBinding): boolean =>
(state.status === 'active' || state.status === 'closing') &&
state.binding === binding;
const completeClose = (binding: RuntimeBinding): void => {
if (!isCurrentBinding(binding)) {
return;
}
if (state.status === 'active') {
closeBinding(binding);
}
state = { status: 'idle' };
};
const failClose = (binding: RuntimeBinding, cause: unknown): void => {
if (!isCurrentBinding(binding)) {
return;
}
const error = new Error(
`[${PLUGIN_NAME}] The previous development server failed to close. Restart the process before retrying because Rsbuild may not have finished tearing down its compiler and watchers.`,
{ cause }
);
closeBinding(binding, error);
state = { status: 'terminal', error };
};
const applyCloseOutcome = (
observation: CloseObservation,
outcome: CloseOutcome
): void => {
observation.outcome = outcome;
const { binding } = observation;
if (!binding) {
return;
}
if (outcome.ok) {
completeClose(binding);
} else {
failClose(binding, outcome.cause);
}
observation.binding = undefined;
};
const observeClose = (server: RsbuildDevServer): CloseObservation => {
const existing = closeObservationByServer.get(server);
if (existing) {
return existing;
}
const observation: CloseObservation = {};
const close = server.close.bind(server);
server.close = () => {
if (observation.promise) {
return observation.promise;
}
let closePromise: Promise<void>;
try {
closePromise = Promise.resolve(close());
} catch (cause) {
closePromise = Promise.reject(normalizeEffectError(cause));
}
observation.promise = closePromise
.then(() => applyCloseOutcome(observation, { ok: true }))
.catch(cause => {
const error = normalizeEffectError(cause);
applyCloseOutcome(observation, { ok: false, cause: error });
throw error;
});
return observation.promise;
};
closeObservationByServer.set(server, observation);
return observation;
};
return {
getState,
getActiveBinding,
observeClose(server: RsbuildDevServer): void {
observeClose(server);
},
assertCanStart(): void {
if (state.status === 'terminal') {
throw state.error;
}
if (state.status === 'active') {
throw new Error(
`[${PLUGIN_NAME}] A development server is already active. Await its close() before calling createDevServer() again. If startup failed before returning the server, restart the process before retrying.`
);
}
if (state.status === 'closing') {
throw new Error(
`[${PLUGIN_NAME}] The previous development server is still closing. Await its close() before calling createDevServer() again.`
);
}
},
createBinding(
server: RsbuildDevServer,
runtime: ReactRouterDevRuntime
): RuntimeBinding {
const binding = { id: nextSessionId++, server, runtime };
state = { status: 'active', binding };
return binding;
},
bindCloseObservation(binding: RuntimeBinding): void {
const observation = observeClose(binding.server);
observation.binding = binding;
if (observation.outcome) {
applyCloseOutcome(observation, observation.outcome);
}
},
markClosing(binding: RuntimeBinding): void {
if (isCurrentBinding(binding)) {
state = { status: 'closing', binding };
}
},
terminate(binding: RuntimeBinding, error: Error): void {
if (!isCurrentBinding(binding)) {
return;
}
closeBinding(binding, error);
state = { status: 'terminal', error };
},
};
};