diff --git a/example.js b/example.js index ada73c5..2da702e 100644 --- a/example.js +++ b/example.js @@ -259,6 +259,10 @@ client.on('change_battery', (batteryInfo) => { console.log(`Battery: ${battery}% - Charging? ${plugged}`); }); +client.on('change_state', state => { + console.log('CHANGE STATE', state ); +}); + client.on('disconnected', (reason) => { console.log('Client was logged out', reason); }); diff --git a/index.d.ts b/index.d.ts index 0788654..c8022ec 100644 --- a/index.d.ts +++ b/index.d.ts @@ -149,8 +149,8 @@ declare namespace WAWebJS { /** Emitted when the client has been disconnected */ on(event: 'disconnected', listener: ( - /** state that caused the disconnect */ - reason: WAState + /** reason that caused the disconnect */ + reason: WAState | "NAVIGATED" ) => void): this /** Emitted when a user joins the chat via invite link or is added by an admin */ diff --git a/src/Client.js b/src/Client.js index 7ca79c3..c647bcf 100644 --- a/src/Client.js +++ b/src/Client.js @@ -335,7 +335,7 @@ class Client extends EventEmitter { /** * Emitted when the client has been disconnected * @event Client#disconnected - * @param {WAState} reason state that caused the disconnect + * @param {WAState|"NAVIGATION"} reason reason that caused the disconnect */ this.emit(Events.DISCONNECTED, state); this.destroy(); @@ -373,6 +373,13 @@ class Client extends EventEmitter { * @event Client#ready */ this.emit(Events.READY); + + // Disconnect when navigating away + // Because WhatsApp Web now reloads when logging out from the device, this also covers that case + this.pupPage.on('framenavigated', async () => { + this.emit(Events.DISCONNECTED, 'NAVIGATION'); + await this.destroy(); + }); } /**