From a80e77ab3f2fc1d7145e438b66decf95b8e7a13b Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 24 Aug 2020 23:00:32 +0300 Subject: [PATCH] Fix handling chat list mutations --- puppet/src/contentscript.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/puppet/src/contentscript.js b/puppet/src/contentscript.js index 22a682a..255feaf 100644 --- a/puppet/src/contentscript.js +++ b/puppet/src/contentscript.js @@ -23,7 +23,7 @@ */ window.__chronoParseDate = function (text, ref, option) {} /** - * @param {Set} changes - The hrefs of the chats that changed. + * @param {string[]} changes - The hrefs of the chats that changed. * @return {Promise} */ window.__mautrixReceiveChanges = function (changes) {} @@ -171,7 +171,8 @@ class MautrixController { * @property {string} name - The name of the chat. * @property {string} lastMsg - The most recent message in the chat. * May be prefixed by sender name. - * @property {string} lastMsgDate - An imprecise date for the most recent message (e.g. "7:16 PM", "Thu" or "Aug 4") + * @property {string} lastMsgDate - An imprecise date for the most recent message + * (e.g. "7:16 PM", "Thu" or "Aug 4") */ /** @@ -251,14 +252,18 @@ class MautrixController { console.debug("Chat list mutation:", change) if (!(change.target instanceof Element) || change.target.tagName.toLowerCase() === "mws-conversation-list-item-menu") { + console.debug("Ignoring chat list mutation:", change.target instanceof Element) continue } const chat = this.parseChatListItem(change.target.closest("mws-conversation-list-item")) + console.debug("Changed chat list item:", chat) changedChatIDs.add(chat.id) } if (changedChatIDs.size > 0) { console.debug("Dispatching chat list mutations:", changedChatIDs) - window.__mautrixReceiveChanges(Array.from(changedChatIDs)) + window.__mautrixReceiveChanges(Array.from(changedChatIDs)).then( + () => console.debug("Chat list mutations dispatched"), + err => console.error("Error dispatching chat list mutations:", err)) } } @@ -271,7 +276,13 @@ class MautrixController { if (this.chatListObserver !== null) { this.removeChatListObserver() } - this.chatListObserver = new MutationObserver(this._observeChatListMutations) + this.chatListObserver = new MutationObserver(mutations => { + try { + this._observeChatListMutations(mutations) + } catch (err) { + console.error("Error observing chat list mutations:", err) + } + }) this.chatListObserver.observe(element, { childList: true, subtree: true }) console.debug("Started chat list observer") }