Skip to content

Commit

Permalink
feat: add description to the disconnecting and disconnect events (#4622)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackatosh authored and darrachequesne committed Feb 4, 2023
1 parent 4e64123 commit 8aa9499
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 6 additions & 2 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,21 @@ export class Client<
* Called upon transport close.
*
* @param reason
* @param description
* @private
*/
private onclose(reason: CloseReason | "forced server close"): void {
private onclose(
reason: CloseReason | "forced server close",
description?: any
): void {
debug("client close with reason %s", reason);

// ignore a potential subsequent `close` event
this.destroy();

// `nsps` and `sockets` are cleaned up seamlessly
for (const socket of this.sockets.values()) {
socket._onclose(reason);
socket._onclose(reason, description);
}
this.sockets.clear();

Expand Down
11 changes: 6 additions & 5 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ const RECOVERABLE_DISCONNECT_REASONS: ReadonlySet<DisconnectReason> = new Set([
]);

export interface SocketReservedEventsMap {
disconnect: (reason: DisconnectReason) => void;
disconnecting: (reason: DisconnectReason) => void;
disconnect: (reason: DisconnectReason, description?: any) => void;
disconnecting: (reason: DisconnectReason, description?: any) => void;
error: (err: Error) => void;
}

Expand Down Expand Up @@ -749,14 +749,15 @@ export class Socket<
* Called upon closing. Called by `Client`.
*
* @param {String} reason
* @param description
* @throw {Error} optional error object
*
* @private
*/
_onclose(reason: DisconnectReason): this | undefined {
_onclose(reason: DisconnectReason, description?: any): this | undefined {
if (!this.connected) return this;
debug("closing socket - reason %s", reason);
this.emitReserved("disconnecting", reason);
this.emitReserved("disconnecting", reason, description);

if (RECOVERABLE_DISCONNECT_REASONS.has(reason)) {
debug("connection state recovery is enabled for sid %s", this.id);
Expand All @@ -772,7 +773,7 @@ export class Socket<
this.nsp._remove(this);
this.client._remove(this);
this.connected = false;
this.emitReserved("disconnect", reason);
this.emitReserved("disconnect", reason, description);
return;
}

Expand Down

0 comments on commit 8aa9499

Please sign in to comment.