From 8aa94991cee5518567d6254eec04b23f81510257 Mon Sep 17 00:00:00 2001 From: Edouard Benauw Date: Fri, 3 Feb 2023 13:19:30 +0100 Subject: [PATCH] feat: add description to the disconnecting and disconnect events (#4622) See also: https://github.com/socketio/socket.io-client/commit/b862924b7f1720979e5db2f0154906b305d420e3 --- lib/client.ts | 8 ++++++-- lib/socket.ts | 11 ++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/client.ts b/lib/client.ts index 3ce7d5a226..056d4b5a26 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -311,9 +311,13 @@ 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 @@ -321,7 +325,7 @@ export class Client< // `nsps` and `sockets` are cleaned up seamlessly for (const socket of this.sockets.values()) { - socket._onclose(reason); + socket._onclose(reason, description); } this.sockets.clear(); diff --git a/lib/socket.ts b/lib/socket.ts index 59138cfe9e..97c264f559 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -56,8 +56,8 @@ const RECOVERABLE_DISCONNECT_REASONS: ReadonlySet = 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; } @@ -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); @@ -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; }