Skip to content

fix(Server): ensure server.listen callback is invoked when listening … #216

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
68 changes: 39 additions & 29 deletions src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,20 @@ export default class Server extends EventEmitter {
throw new TypeError('options must be an object or a number');
}

// Add callback as a listener for the listening event
if (typeof cb === 'function') {
this.once('listening', cb);
}
// Add callback as a listener for the listening event
if (typeof cb === 'function') {
this.once('listening', cb);
}

this.once('listening', () => {
this.listening = true;
});
this.once('listening', () => {
this.listening = true;
});

Sockets.listen(this._id, listenOptions);
return this;
// Register internal events (e.g. 'connection', 'error', etc.)
this._registerEvents();

Sockets.listen(this._id, listenOptions);
return this;
}

/**
Expand Down Expand Up @@ -187,25 +190,32 @@ export default class Server extends EventEmitter {
* @private
*/
_registerEvents() {
this._listeningListener = this._eventEmitter.addListener('listening', (evt) => {
if (evt.id !== this._id) return;
this._localAddress = evt.connection.localAddress;
this._localPort = evt.connection.localPort;
this._localFamily = evt.connection.localFamily;
this.emit('listening');
});
this._errorListener = this._eventEmitter.addListener('error', (evt) => {
if (evt.id !== this._id) return;
this.close();
this.emit('error', evt.error);
});
this._connectionsListener = this._eventEmitter.addListener('connection', (evt) => {
if (evt.id !== this._id) return;
const newSocket = this._buildSocket(evt.info);
this._addConnection(newSocket);
this.emit('connection', newSocket);
});
}
this._listeningListener = this._eventEmitter.addListener('listening', (evt) => {
if (evt.id !== this._id) return;
this._localAddress = evt.connection.localAddress;
this._localPort = evt.connection.localPort;
this._localFamily = evt.connection.localFamily;
this.emit('listening');

// 🔧 Patch: Call the callback from listen() async
if (typeof this._listenCallback === 'function') {
setTimeout(this._listenCallback, 0);
}
});

this._errorListener = this._eventEmitter.addListener('error', (evt) => {
if (evt.id !== this._id) return;
this.close();
this.emit('error', evt.error);
});

this._connectionsListener = this._eventEmitter.addListener('connection', (evt) => {
if (evt.id !== this._id) return;
const newSocket = this._buildSocket(evt.info);
this._addConnection(newSocket);
this.emit('connection', newSocket);
});
}

/**
* @private
Expand Down Expand Up @@ -269,4 +279,4 @@ export default class Server extends EventEmitter {
socket.setKeepAlive(this._serverOptions.keepAlive, keepAliveDelay);
}
}
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request only contains a blank space. Was additional content intended?