Fix reconnection state

This commit is contained in:
Pavel Djundik 2018-07-15 23:23:49 +03:00 committed by Pavel Djundik
parent cd94b5d655
commit c42fc55c6f
5 changed files with 41 additions and 16 deletions

View file

@ -5,6 +5,7 @@ const socket = require("../socket");
const storage = require("../localStorage");
const utils = require("../utils");
const templates = require("../../views");
const {vueApp} = require("../vue");
socket.on("auth", function(data) {
// If we reconnected and serverHash differs, that means the server restarted
@ -67,7 +68,19 @@ socket.on("auth", function(data) {
if (token) {
$("#loading-page-message, #connection-error").text("Authorizing…");
const lastMessage = utils.lastMessageId;
let lastMessage = -1;
for (const network of vueApp.networks) {
for (const chan of network.channels) {
for (const msg of chan.messages) {
if (msg.id > lastMessage) {
lastMessage = msg.id;
}
}
}
}
socket.emit("auth", {user, token, lastMessage});
}
}

View file

@ -13,18 +13,33 @@ const {vueApp} = require("../vue");
socket.on("init", function(data) {
$("#loading-page-message, #connection-error").text("Rendering…");
const lastMessageId = utils.lastMessageId;
let previousActive = 0;
if (lastMessageId > -1) {
previousActive = sidebar.find(".active").data("id");
}
let previousActive = vueApp.activeChannel && vueApp.activeChannel.channel.id;
const networks = new Set(JSON.parse(storage.get("thelounge.networks.collapsed")));
for (const network of data.networks) {
network.isJoinChannelShown = false;
network.isCollapsed = networks.has(network.uuid);
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 && currentChannel.messages) {
channel.messages = currentChannel.messages.concat(channel.messages);
if (currentChannel.moreHistoryAvailable) {
channel.moreHistoryAvailable = true;
}
}
}
} else {
network.isJoinChannelShown = false;
network.isCollapsed = networks.has(network.uuid);
}
for (const channel of network.channels) {
if (channel.type === "channel") {
@ -38,7 +53,9 @@ socket.on("init", function(data) {
$("#connection-error").removeClass("shown");
if (lastMessageId < 0) {
if (!vueApp.initialized) {
vueApp.initialized = true;
if (data.token) {
storage.set("token", data.token);
}

View file

@ -20,10 +20,6 @@ try {
}
socket.on("msg", function(data) {
if (utils.lastMessageId < data.msg.id) {
utils.lastMessageId = data.msg.id;
}
let targetId = data.chan;
let {channel} = findChannel(data.chan);

View file

@ -6,14 +6,12 @@ const viewport = $("#viewport");
const {vueApp} = require("./vue");
var serverHash = -1; // eslint-disable-line no-var
var lastMessageId = -1; // eslint-disable-line no-var
module.exports = {
// Same value as media query in CSS that forces sidebars to become overlays
mobileViewportPixels: 768,
findCurrentNetworkChan,
serverHash,
lastMessageId,
confirmExit,
scrollIntoViewNicely,
hasRoleInChannel,

View file

@ -21,6 +21,7 @@ Vue.filter("roundBadgeNumber", roundBadgeNumber);
const vueApp = new Vue({
el: "#viewport",
data: {
initialized: false,
connected: false,
appName: document.title,
activeChannel: null,