From b51ce9bc9679d8f6f132350182de336beb4faa45 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Tue, 5 Apr 2022 02:05:43 -0400 Subject: [PATCH] Don't re-add inbound listeners on every reconnect/sync --- node/src/client.js | 69 ++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/node/src/client.js b/node/src/client.js index cb3004d..799d0d1 100644 --- a/node/src/client.js +++ b/node/src/client.js @@ -73,6 +73,38 @@ class UserClient { this.mxid = mxid this.peerClient = peerClient + + this.#talkClient.on("chat", (data, channel) => { + this.log(`Received chat message ${data.chat.logId} in channel ${channel.channelId}`) + return this.write("chat", { + //is_sequential: true, // TODO Make sequential per user & channel (if it isn't already) + chatlog: data.chat, + channelId: channel.channelId, + channelType: channel.info.type, + }) + }) + + /* TODO Many more listeners + this.#talkClient.on("chat_read", (chat, channel, reader) => { + this.log(`chat_read in channel ${channel.channelId}`) + //chat.logId + }) + */ + + this.#talkClient.on("disconnected", (reason) => { + this.log(`Disconnected (reason=${reason})`) + this.disconnect() + return this.write("disconnected", { + reason: reason, + }) + }) + + this.#talkClient.on("switch_server", () => { + this.log(`Server switch requested`) + return this.write("switch_server", { + is_sequential: true, + }) + }) } /** @@ -127,42 +159,7 @@ class UserClient { async connect(credential) { // TODO Don't re-login if possible. But must still return a LoginResult! this.disconnect() - const res = await this.#talkClient.login(credential) - if (!res.success) return res - - this.#talkClient.on("chat", (data, channel) => { - this.log(`Received chat message ${data.chat.logId} in channel ${channel.channelId}`) - return this.write("chat", { - //is_sequential: true, // TODO Make sequential per user & channel (if it isn't already) - chatlog: data.chat, - channelId: channel.channelId, - channelType: channel.info.type, - }) - }) - - /* TODO Many more listeners - this.#talkClient.on("chat_read", (chat, channel, reader) => { - this.log(`chat_read in channel ${channel.channelId}`) - //chat.logId - }) - */ - - this.#talkClient.on("disconnected", (reason) => { - this.log(`Disconnected (reason=${reason})`) - this.disconnect() - return this.write("disconnected", { - reason: reason, - }) - }) - - this.#talkClient.on("switch_server", () => { - this.log(`Server switch requested`) - return this.write("switch_server", { - is_sequential: true, - }) - }) - - return res + return await this.#talkClient.login(credential) } disconnect() {