diff --git a/client/js/options.js b/client/js/options.js index a979b96e..4f57a6a2 100644 --- a/client/js/options.js +++ b/client/js/options.js @@ -3,7 +3,6 @@ const $ = require("jquery"); const escapeRegExp = require("lodash/escapeRegExp"); const storage = require("./localStorage"); -const tz = require("./libs/handlebars/tz"); const socket = require("./socket"); const {vueApp} = require("./vue"); require("../js/autocompletion"); diff --git a/client/js/socket-events/init.js b/client/js/socket-events/init.js index 1f153f88..f65f9747 100644 --- a/client/js/socket-events/init.js +++ b/client/js/socket-events/init.js @@ -18,35 +18,39 @@ socket.on("init", function(data) { const networks = new Set(JSON.parse(storage.get("thelounge.networks.collapsed"))); for (const network of data.networks) { + network.isCollapsed = networks.has(network.uuid); + network.channels.forEach(initChannel); + const currentNetwork = vueApp.networks.find((n) => n.uuid === network.uuid); - // TODO: Make this updating more efficient - if (currentNetwork) { - network.isJoinChannelShown = currentNetwork.isJoinChannelShown; - network.isCollapsed = currentNetwork.isCollapsed; - - for (const channel of network.channels) { - const currentChannel = currentNetwork.channels.find((c) => c.id === channel.id); - - if (currentChannel) { - channel.scrolledToBottom = currentChannel.scrolledToBottom; - channel.pendingMessage = currentChannel.pendingMessage; - - if (currentChannel.messages) { - channel.messages = currentChannel.messages.concat(channel.messages); - } - - if (currentChannel.moreHistoryAvailable) { - channel.moreHistoryAvailable = true; - } - } - } - } else { + // If we are reconnecting, merge existing state variables because they don't exist on the server + if (!currentNetwork) { network.isJoinChannelShown = false; - network.isCollapsed = networks.has(network.uuid); + + continue; } - network.channels.forEach(initChannel); + network.isJoinChannelShown = currentNetwork.isJoinChannelShown; + + for (const channel of network.channels) { + const currentChannel = currentNetwork.channels.find((c) => c.id === channel.id); + + if (!currentChannel) { + continue; + } + + channel.scrolledToBottom = currentChannel.scrolledToBottom; + channel.pendingMessage = currentChannel.pendingMessage; + + // Reconnection only sends new messages, so merge it on the client + if (currentChannel.messages) { + channel.messages = currentChannel.messages.concat(channel.messages); + } + + if (currentChannel.moreHistoryAvailable) { + channel.moreHistoryAvailable = true; + } + } } vueApp.networks = data.networks; diff --git a/client/js/socket-events/network.js b/client/js/socket-events/network.js index 278f3b7c..ca584ecf 100644 --- a/client/js/socket-events/network.js +++ b/client/js/socket-events/network.js @@ -14,10 +14,6 @@ socket.on("network", function(data) { network.isCollapsed = false; network.channels.forEach(initChannel); - for (const channel of network.channels) { - initChannel(channel); - } - vueApp.networks.push(network); vueApp.$nextTick(() => {