Skip to content
Closed
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?