raise style restoration limit; don't count own changes

This commit is contained in:
tophf 2017-12-09 18:41:05 +03:00
parent 5f1f9c4d86
commit 57e335abb8

View File

@ -165,9 +165,7 @@
} else { } else {
if (inDoc) { if (inDoc) {
disabledElements.set(id, inDoc); disabledElements.set(id, inDoc);
docRootObserver.stop(); docRootObserver.evade(() => inDoc.remove());
inDoc.remove();
docRootObserver.start();
} }
} }
} }
@ -185,7 +183,7 @@
// in case something went wrong and new style was never applied // in case something went wrong and new style was never applied
retiredStyleTimers.set(deadID, setTimeout(removeStyle, 1000, {id: deadID})); retiredStyleTimers.set(deadID, setTimeout(removeStyle, 1000, {id: deadID}));
} else { } else {
el.remove(); docRootObserver.evade(() => el.remove());
} }
} }
styleElements.delete(ID_PREFIX + id); styleElements.delete(ID_PREFIX + id);
@ -302,12 +300,12 @@
if (next === newElement.nextElementSibling) { if (next === newElement.nextElementSibling) {
return; return;
} }
docRootObserver.stop(); docRootObserver.evade(() => {
ROOT.insertBefore(newElement, next || null); ROOT.insertBefore(newElement, next || null);
if (disableAll) { if (disableAll) {
newElement.disabled = true; newElement.disabled = true;
} }
docRootObserver.start(); });
} }
@ -394,7 +392,7 @@
function init() { function init() {
docRootObserver = new MutationObserver(sortStyleElements); docRootObserver = new MutationObserver(sortStyleElements);
Object.assign(docRootObserver, {start, stop}); Object.assign(docRootObserver, {start, stop, evade});
setTimeout(sortStyleElements); setTimeout(sortStyleElements);
} }
function start({sort = false} = {}) { function start({sort = false} = {}) {
@ -408,10 +406,21 @@
} }
function stop() { function stop() {
if (observing) { if (observing) {
docRootObserver.takeRecords();
docRootObserver.disconnect(); docRootObserver.disconnect();
observing = false; observing = false;
} }
} }
function evade(fn) {
const wasObserving = observing;
if (observing) {
stop();
}
fn();
if (wasObserving) {
start();
}
}
function sortStyleMap() { function sortStyleMap() {
const list = []; const list = [];
let prevStyleId = 0; let prevStyleId = 0;
@ -494,7 +503,7 @@
restorationCounter = 0; restorationCounter = 0;
} }
lastRestorationTime = t; lastRestorationTime = t;
return ++restorationCounter > 2; return ++restorationCounter > 5;
} }
} }
})(); })();