Fix multiple favicon changing request

This commit is contained in:
Ajay Bura 2022-09-04 13:33:41 +05:30
parent 584fa87bbd
commit bdc10fb729
2 changed files with 16 additions and 10 deletions

View file

@ -37,6 +37,8 @@ class Notifications extends EventEmitter {
constructor(roomList) { constructor(roomList) {
super(); super();
this.initialized = false;
this.favicon = LogoSVG;
this.matrixClient = roomList.matrixClient; this.matrixClient = roomList.matrixClient;
this.roomList = roomList; this.roomList = roomList;
@ -50,6 +52,7 @@ class Notifications extends EventEmitter {
} }
async _initNoti() { async _initNoti() {
this.initialized = false;
this.roomIdToNoti = new Map(); this.roomIdToNoti = new Map();
const addNoti = (roomId) => { const addNoti = (roomId) => {
@ -63,6 +66,8 @@ class Notifications extends EventEmitter {
}; };
[...this.roomList.rooms].forEach(addNoti); [...this.roomList.rooms].forEach(addNoti);
[...this.roomList.directs].forEach(addNoti); [...this.roomList.directs].forEach(addNoti);
this.initialized = true;
this._updateFavicon(); this._updateFavicon();
} }
@ -136,6 +141,7 @@ class Notifications extends EventEmitter {
} }
async _updateFavicon() { async _updateFavicon() {
if (!this.initialized) return;
let unread = false; let unread = false;
let highlight = false; let highlight = false;
[...this.roomIdToNoti.values()].find((noti) => { [...this.roomIdToNoti.values()].find((noti) => {
@ -146,11 +152,16 @@ class Notifications extends EventEmitter {
if (unread && highlight) return true; if (unread && highlight) return true;
return false; return false;
}); });
if (!unread) { let newFavicon = LogoSVG;
setFavicon(LogoSVG); if (unread && !highlight) {
return; newFavicon = LogoUnreadSVG;
} }
setFavicon(highlight ? LogoHighlightSVG : LogoUnreadSVG); if (unread && highlight) {
newFavicon = LogoHighlightSVG;
}
if (newFavicon === this.favicon) return;
this.favicon = newFavicon;
setFavicon(this.favicon);
} }
_setNoti(roomId, total, highlight) { _setNoti(roomId, total, highlight) {

View file

@ -120,12 +120,7 @@ export function cssVar(name) {
} }
export function setFavicon(url) { export function setFavicon(url) {
const oldFav = document.querySelector('[rel=icon]'); document.querySelector('[rel=icon]').href = url;
oldFav.parentElement.removeChild(oldFav);
const fav = document.createElement('link');
fav.rel = 'icon';
fav.href = url;
document.head.appendChild(fav);
} }
export function copyToClipboard(text) { export function copyToClipboard(text) {