Fix multiple favicon changing request
authorAjay Bura <32841439+ajbura@users.noreply.github.com>
Sun, 4 Sep 2022 08:03:41 +0000 (13:33 +0530)
committerAjay Bura <32841439+ajbura@users.noreply.github.com>
Sun, 4 Sep 2022 08:03:41 +0000 (13:33 +0530)
src/client/state/Notifications.js
src/util/common.js

index c3ec90cf3d1e324800b55f5f1cbc781146a5df36..90f9b4ad865b188e9fd2585feb3257ce43133b58 100644 (file)
@@ -37,6 +37,8 @@ class Notifications extends EventEmitter {
   constructor(roomList) {
     super();
 
+    this.initialized = false;
+    this.favicon = LogoSVG;
     this.matrixClient = roomList.matrixClient;
     this.roomList = roomList;
 
@@ -50,6 +52,7 @@ class Notifications extends EventEmitter {
   }
 
   async _initNoti() {
+    this.initialized = false;
     this.roomIdToNoti = new Map();
 
     const addNoti = (roomId) => {
@@ -63,6 +66,8 @@ class Notifications extends EventEmitter {
     };
     [...this.roomList.rooms].forEach(addNoti);
     [...this.roomList.directs].forEach(addNoti);
+
+    this.initialized = true;
     this._updateFavicon();
   }
 
@@ -136,6 +141,7 @@ class Notifications extends EventEmitter {
   }
 
   async _updateFavicon() {
+    if (!this.initialized) return;
     let unread = false;
     let highlight = false;
     [...this.roomIdToNoti.values()].find((noti) => {
@@ -146,11 +152,16 @@ class Notifications extends EventEmitter {
       if (unread && highlight) return true;
       return false;
     });
-    if (!unread) {
-      setFavicon(LogoSVG);
-      return;
+    let newFavicon = LogoSVG;
+    if (unread && !highlight) {
+      newFavicon = LogoUnreadSVG;
+    }
+    if (unread && highlight) {
+      newFavicon = LogoHighlightSVG;
     }
-    setFavicon(highlight ? LogoHighlightSVG : LogoUnreadSVG);
+    if (newFavicon === this.favicon) return;
+    this.favicon = newFavicon;
+    setFavicon(this.favicon);
   }
 
   _setNoti(roomId, total, highlight) {
index c2a17cbf0dcb773fa69e64ef4de527513d598b12..87b0b7eb916ef138eb1a13ccae8b5dcbc46a9538 100644 (file)
@@ -120,12 +120,7 @@ export function cssVar(name) {
 }
 
 export function setFavicon(url) {
-  const oldFav = document.querySelector('[rel=icon]');
-  oldFav.parentElement.removeChild(oldFav);
-  const fav = document.createElement('link');
-  fav.rel = 'icon';
-  fav.href = url;
-  document.head.appendChild(fav);
+  document.querySelector('[rel=icon]').href = url;
 }
 
 export function copyToClipboard(text) {