Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): async user callback for server.listen #2218

Merged
merged 4 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,13 @@ function startDevServer(config, options) {
throw err;
}

if (options.socket) {
server.listen(options.socket, options.host, (err) => {
if (err) {
throw err;
}
});
} else {
server.listen(options.port, options.host, (err) => {
if (err) {
throw err;
}
});
}
// options.socket does not have a default value, so it will only be set
// if the user sets it explicitly
server.listen(options.socket || options.port, options.host, (err) => {
if (err) {
throw err;
}
});
}

processOptions(config, argv, (config, options) => {
Expand Down
10 changes: 5 additions & 5 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,12 @@ class Server {

// between setupCallback and userCallback should be any other needed handling,
// specifically so that things are done in the right order to prevent
// backwards compatability issues
// backwards compatibility issues
let userCallbackCalled = false;
const userCallback = (err) => {
const userCallback = async (err) => {
if (fn && !userCallbackCalled) {
userCallbackCalled = true;
fn.call(this.listeningApp, err);
await fn.call(this.listeningApp, err);
}
};

Expand All @@ -755,9 +755,9 @@ class Server {
}
};

const fullCallback = (err) => {
const fullCallback = async (err) => {
setupCallback();
userCallback(err);
await userCallback(err);
onListeningCallback();
};

Expand Down
25 changes: 25 additions & 0 deletions test/server/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { noop } = require('webpack-dev-middleware/lib/util');
const Server = require('../../lib/Server');
const config = require('../fixtures/simple-config/webpack.config');
const port = require('../ports-map').Server;
const timer = require('../helpers/timer');

jest.mock('sockjs/lib/transport');

Expand Down Expand Up @@ -178,4 +179,28 @@ describe('Server', () => {
});
});
});

describe('server.listen', () => {
it('should complete async callback before calling onListening', (done) => {
const callOrder = [];
const compiler = webpack(config);
const server = new Server(compiler, {
onListening: () => {
callOrder.push('onListening');
},
...baseDevConfig,
});
server.listen(port, '0.0.0.0', async () => {
await timer(1000);
callOrder.push('user callback');
});

// we need a timeout because even if the server is done listening,
// the compiler might still be compiling
setTimeout(() => {
expect(callOrder).toMatchSnapshot();
server.close(done);
}, 5000);
});
});
});
7 changes: 7 additions & 0 deletions test/server/__snapshots__/Server.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ Array [
},
]
`;

exports[`Server server.listen should complete async callback before calling onListening 1`] = `
Array [
"user callback",
"onListening",
]
`;