Skip to content

Commit e0d2d23

Browse files
committed
fix(server): Start webserver and browsers after preprocessing completed
When loading many files, it is possible that the webserver and browsers will start before file preprocessing has completed. This causes the browser to attempt to connect and timeout and need to be relaunched. This change will cause the server to wait to start the webserver and browsers until after all files have been preprocessed. When a browser connects to the webserver, the webserver will be able to start serving content immediately and the browser will not experience any connection errors or timeouts.
1 parent 9c5f817 commit e0d2d23

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

lib/server.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ var start = function(injector, config, launcher, globalEmitter, preprocess, file
3131
injector.get('framework:' + framework);
3232
});
3333

34-
var filesPromise = fileList.refresh();
34+
// A map of launched browsers.
35+
var singleRunDoneBrowsers = Object.create(null);
3536

36-
if (config.autoWatch) {
37-
filesPromise.then(function() {
38-
injector.invoke(watcher.watch);
39-
}, function() {
40-
injector.invoke(watcher.watch);
41-
});
42-
}
37+
// Passing fake event emitter, so that it does not emit on the global,
38+
// we don't care about these changes.
39+
var singleRunBrowsers = new BrowserCollection(new EventEmitter());
40+
41+
// Some browsers did not get captured.
42+
var singleRunBrowserNotCaptured = false;
4343

4444
webServer.on('error', function(e) {
4545
if (e.code === 'EADDRINUSE') {
@@ -51,26 +51,24 @@ var start = function(injector, config, launcher, globalEmitter, preprocess, file
5151
}
5252
});
5353

54-
// A map of launched browsers.
55-
var singleRunDoneBrowsers = Object.create(null);
56-
57-
// Passing fake event emitter, so that it does not emit on the global,
58-
// we don't care about these changes.
59-
var singleRunBrowsers = new BrowserCollection(new EventEmitter());
54+
var afterPreprocess = function() {
55+
if (config.autoWatch) {
56+
injector.invoke(watcher.watch);
57+
}
6058

61-
// Some browsers did not get captured.
62-
var singleRunBrowserNotCaptured = false;
59+
webServer.listen(config.port, function() {
60+
log.info('Karma v%s server started at http://%s:%s%s', constant.VERSION, config.hostname,
61+
config.port, config.urlRoot);
6362

64-
webServer.listen(config.port, function() {
65-
log.info('Karma v%s server started at http://%s:%s%s', constant.VERSION, config.hostname,
66-
config.port, config.urlRoot);
63+
if (config.browsers && config.browsers.length) {
64+
injector.invoke(launcher.launch, launcher).forEach(function(browserLauncher) {
65+
singleRunDoneBrowsers[browserLauncher.id] = false;
66+
});
67+
}
68+
});
69+
};
6770

68-
if (config.browsers && config.browsers.length) {
69-
injector.invoke(launcher.launch, launcher).forEach(function(browserLauncher) {
70-
singleRunDoneBrowsers[browserLauncher.id] = false;
71-
});
72-
}
73-
});
71+
fileList.refresh().then(afterPreprocess, afterPreprocess);
7472

7573
globalEmitter.on('browsers_change', function() {
7674
// TODO(vojta): send only to interested browsers

0 commit comments

Comments
 (0)