From 3e7319464e864111d06468bbe4c8d14b3839172c Mon Sep 17 00:00:00 2001 From: Alejandro Romano Date: Mon, 18 Oct 2021 08:19:34 -0300 Subject: [PATCH] Fix persistent high CPU usage when emoji-sets is enabled (#586) MutationObserver enters into an infinite loop by iterating on itself. By disconnecting the observer and reconnecting it after parsing the DOM for emoji-sets to work, we stop the problem and Notion goes back to sane 0% CPU idle usage. --- mods/emoji-sets/mod.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/mods/emoji-sets/mod.js b/mods/emoji-sets/mod.js index e2d3791..b14b5c6 100644 --- a/mods/emoji-sets/mod.js +++ b/mods/emoji-sets/mod.js @@ -1,6 +1,7 @@ /* * emoji sets * (c) 2020 dragonwocky (https://dragonwocky.me/) + * (c) minor fixes by Arecsu from martyr⁠— (https://martyr.shop/) * under the MIT license */ @@ -47,13 +48,19 @@ module.exports = { if (!queue.length) requestAnimationFrame(handle); queue.push(...list); }); - observer.observe(document.body, { - childList: true, - subtree: true, - characterData: true, - }); + + const observe = ()=> { + observer.observe(document.body, { + childList: true, + subtree: true, + characterData: true, + }); + }; + observe(); // initiate observer + function handle() { queue = []; + observer.disconnect(); // stop observer from getting stuck into an infinite loop from here const isMac = process.platform === 'darwin', native = (store().style === 'microsoft' && process.platform === 'win32') || @@ -139,6 +146,7 @@ module.exports = { } tweaked = true; } + observe(); // re-initiating the observer now that we don't risk an infinite loop } }); },