Skip to content

Commit d19cf5d

Browse files
motiz88facebook-github-bot
authored andcommittedSep 20, 2021
Report initialize_done event when server is ready
Summary: Adds a new reportable event, `initialize_done`, that is reported when the bundler has finished initialising everything that blocks handling bundle requests (namely loading the dependency graph and creating the transformer). Reviewed By: GijsWeterings Differential Revision: D30991582 fbshipit-source-id: 7843f261158c3dfbeaade6976aa6ea237c531d23
1 parent a75e292 commit d19cf5d

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed
 

‎packages/metro/src/Bundler.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ export type BundlerOptions = $ReadOnly<{|
2424

2525
class Bundler {
2626
_depGraphPromise: Promise<DependencyGraph>;
27+
_readyPromise: Promise<void>;
2728
_transformer: Transformer;
2829

2930
constructor(config: ConfigT, options?: BundlerOptions) {
3031
this._depGraphPromise = DependencyGraph.load(config, options);
3132

32-
this._depGraphPromise
33+
this._readyPromise = this._depGraphPromise
3334
.then((dependencyGraph: DependencyGraph) => {
3435
this._transformer = new Transformer(
3536
config,
@@ -63,6 +64,11 @@ class Bundler {
6364

6465
return this._transformer.transformFile(filePath, transformOptions);
6566
}
67+
68+
// Waits for the bundler to become ready.
69+
async ready(): Promise<void> {
70+
await this._readyPromise;
71+
}
6672
}
6773

6874
module.exports = Bundler;

‎packages/metro/src/IncrementalBundler.js

+5
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ class IncrementalBundler {
336336

337337
return absoluteEntryFiles;
338338
}
339+
340+
// Wait for the bundler to become ready.
341+
async ready(): Promise<void> {
342+
await this._bundler.ready();
343+
}
339344
}
340345

341346
module.exports = IncrementalBundler;

‎packages/metro/src/Server.js

+5
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,11 @@ class Server {
12361236
_getEntryPointAbsolutePath(entryFile: string) {
12371237
return path.resolve(this._getServerRootDir(), entryFile);
12381238
}
1239+
1240+
// Wait for the server to finish initializing.
1241+
async ready(): Promise<void> {
1242+
await this._bundler.ready();
1243+
}
12391244
}
12401245

12411246
function* zip<X, Y>(xs: Iterable<X>, ys: Iterable<Y>): Iterable<[X, Y]> {

‎packages/metro/src/index.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,38 @@ async function runMetro(
120120
options?: ServerOptions,
121121
): Promise<MetroServer> {
122122
const mergedConfig = await getConfig(config);
123+
const {
124+
reporter,
125+
server: {port},
126+
} = mergedConfig;
123127

124-
mergedConfig.reporter.update({
128+
reporter.update({
125129
hasReducedPerformance: options
126130
? Boolean(options.hasReducedPerformance)
127131
: false,
128-
port: mergedConfig.server.port,
132+
port,
129133
type: 'initialize_started',
130134
});
131135

132-
return new MetroServer(mergedConfig, options);
136+
const server = new MetroServer(mergedConfig, options);
137+
138+
server
139+
.ready()
140+
.then(() => {
141+
reporter.update({
142+
type: 'initialize_done',
143+
port,
144+
});
145+
})
146+
.catch(error => {
147+
reporter.update({
148+
type: 'initialize_failed',
149+
port,
150+
error,
151+
});
152+
});
153+
154+
return server;
133155
}
134156

135157
exports.runMetro = runMetro;

‎packages/metro/src/lib/reporting.js

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ export type ReportableEvent =
4545
error: Error,
4646
...
4747
}
48+
| {
49+
type: 'initialize_done',
50+
port: number,
51+
}
4852
| {
4953
buildID: string,
5054
type: 'bundle_build_done',

0 commit comments

Comments
 (0)
Please sign in to comment.