From 9032025920460ede7227aa3ac14622c304b9ecda Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 17 Feb 2019 22:32:37 -0400 Subject: [PATCH] [ADD] Disconnected event --- example.js | 4 ++++ src/client/Client.js | 15 +++++++++++---- src/util/Constants.js | 3 ++- src/util/Injected.js | 1 - 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/example.js b/example.js index ed571df..bc73853 100644 --- a/example.js +++ b/example.js @@ -27,5 +27,9 @@ client.on('message', (msg) => { // Send a new message to the same chat client.sendMessage(msg.from, 'pong'); } +}); + +client.on('disconnected', () => { + console.log('Client was logged out'); }) diff --git a/src/client/Client.js b/src/client/Client.js index 935ce41..d4ce73e 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -48,14 +48,21 @@ class Client extends EventEmitter { await page.evaluate(LoadExtraProps, model.WAppModel, model.extraFields); } - await page.exposeFunction('onAddMessageEvent', (msg) => { + await page.exposeFunction('onAddMessageEvent', msg => { if (msg.id.fromMe) return; - - this.emit('message', new Message(this, msg)); + this.emit(Events.MESSAGE_CREATE, new Message(this, msg)); }); + await page.exposeFunction('onConnectionChangedEvent', (conn, connected) => { + if (!connected) { + this.emit(Events.DISCONNECTED); + this.destroy(); + } + }) + await page.evaluate(() => { Store.Msg.on('add', onAddMessageEvent); + Store.Conn.on('change:connected', onConnectionChangedEvent); }) this.pupBrowser = browser; @@ -78,7 +85,7 @@ class Client extends EventEmitter { let chat = await this.pupPage.evaluate(chatId => { return Store.Chat.get(chatId).serialize(); }, chatId); - + return new Chat(this, chat); } diff --git a/src/util/Constants.js b/src/util/Constants.js index 5860018..2a86400 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -20,7 +20,8 @@ exports.Events = { AUTHENTICATED: 'authenticated', READY: 'ready', MESSAGE_CREATE: 'message', - QR_RECEIVED: 'qr' + QR_RECEIVED: 'qr', + DISCONNECTED: 'disconnected' } exports.MessageTypes = { diff --git a/src/util/Injected.js b/src/util/Injected.js index 6e0a6a6..c52ef38 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -53,7 +53,6 @@ exports.ExposeStore = () => { * Adds extra props to the serialization of a model */ exports.LoadExtraProps = (model, props) => { - console.log(model, props); Store[model].models[0].__props = Store[model].models[0].__props.concat(props); }