Replace synchronizeNotifiedState with a getter & watcher

This commit is contained in:
Tim Miller-Williams 2019-11-08 00:03:41 +00:00 committed by Pavel Djundik
parent 16f8304c4e
commit cbaf4db339
7 changed files with 23 additions and 39 deletions

View File

@ -162,8 +162,6 @@ export default {
target: this.channel.id, target: this.channel.id,
}); });
} }
this.$root.synchronizeNotifiedState();
}, },
hideUserVisibleError() { hideUserVisibleError() {
this.$store.commit("currentUserVisibleError", null); this.$store.commit("currentUserVisibleError", null);

View File

@ -62,8 +62,6 @@ socket.on("init", function(data) {
} }
} }
vueApp.synchronizeNotifiedState();
if (document.body.classList.contains("public")) { if (document.body.classList.contains("public")) {
window.addEventListener( window.addEventListener(
"beforeunload", "beforeunload",

View File

@ -88,10 +88,6 @@ socket.on("msg", function(data) {
user.lastMessage = new Date(data.msg.time).getTime() || Date.now(); user.lastMessage = new Date(data.msg.time).getTime() || Date.now();
} }
} }
if (data.msg.self || data.msg.highlight) {
vueApp.synchronizeNotifiedState();
}
}); });
function notifyMessage(targetId, channel, activeChannel, msg) { function notifyMessage(targetId, channel, activeChannel, msg) {

View File

@ -1,7 +1,6 @@
"use strict"; "use strict";
const socket = require("../socket"); const socket = require("../socket");
const {vueApp} = require("../vue");
const store = require("../store").default; const store = require("../store").default;
// Sync unread badge and marker when other clients open a channel // Sync unread badge and marker when other clients open a channel
@ -27,6 +26,4 @@ socket.on("open", function(id) {
channel.channel.messages[channel.channel.messages.length - 1].id; channel.channel.messages[channel.channel.messages.length - 1].id;
} }
} }
vueApp.synchronizeNotifiedState();
}); });

View File

@ -18,6 +18,4 @@ socket.on("part", function(data) {
1 1
); );
} }
vueApp.synchronizeNotifiedState();
}); });

View File

@ -140,6 +140,17 @@ const store = new Vuex.Store({
return null; return null;
}, },
highlightCount(state) {
let highlightCount = 0;
for (const network of state.networks) {
for (const channel of network.channels) {
highlightCount += channel.highlight;
}
}
return highlightCount;
},
title(state, getters) { title(state, getters) {
const alertEventCount = getters.highlightCount ? `(${getters.highlightCount}) ` : ""; const alertEventCount = getters.highlightCount ? `(${getters.highlightCount}) ` : "";

View File

@ -11,6 +11,10 @@ const constants = require("./constants");
Vue.filter("localetime", localetime); Vue.filter("localetime", localetime);
const favicon = document.getElementById("favicon");
const faviconNormal = favicon.getAttribute("href");
const faviconAlerted = favicon.dataset.other;
const vueApp = new Vue({ const vueApp = new Vue({
el: "#viewport", el: "#viewport",
router, router,
@ -51,32 +55,6 @@ const vueApp = new Vue({
channel.moreHistoryAvailable = true; channel.moreHistoryAvailable = true;
} }
}, },
synchronizeNotifiedState() {
let hasAnyHighlights = false;
for (const network of this.$store.state.networks) {
for (const chan of network.channels) {
if (chan.highlight > 0) {
hasAnyHighlights = true;
break;
}
}
}
this.toggleNotificationMarkers(hasAnyHighlights);
},
toggleNotificationMarkers(newState) {
if (this.$store.state.isNotified !== newState) {
// Toggles a dot on the menu icon when there are unread notifications
this.$store.commit("isNotified", newState);
// Toggles the favicon to red when there are unread notifications
const favicon = document.getElementById("favicon");
const old = favicon.getAttribute("href");
favicon.setAttribute("href", favicon.dataset.other);
favicon.dataset.other = old;
}
},
}, },
render(createElement) { render(createElement) {
return createElement(App, { return createElement(App, {
@ -113,6 +91,14 @@ store.watch(
} }
); );
// Toggles the favicon to red when there are unread notifications
store.watch(
(_, getters) => getters.highlightCount,
(highlightCount) => {
favicon.setAttribute("href", highlightCount > 0 ? faviconAlerted : faviconNormal);
}
);
Vue.config.errorHandler = function(e) { Vue.config.errorHandler = function(e) {
store.commit("currentUserVisibleError", `Vue error: ${e.message}`); store.commit("currentUserVisibleError", `Vue error: ${e.message}`);
console.error(e); // eslint-disable-line console.error(e); // eslint-disable-line