Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/iripo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const iripo = (window.iripo = {
inWatchers: new Map(),
outWatchers: new Map(),
processedElems: new WeakMap(),
processingQueued: false,
ignoreMutationsInHead: true,

getSymbol: function getSymbol(fn) {
Expand Down Expand Up @@ -154,17 +153,21 @@ window.addEventListener('DOMContentLoaded', function handleDOMContentLoaded(

// Watch the page for any mutations. If they occur, requset that the browser run mutations during the next idle period.
// If the idle period has not yet happened, do nothing, as all mutation functions run once the browser is idle.
let queuedMutations = [];
function watchMutations(mutations) {
if (iripo.paused || iripo.processingQueued) return;
iripo.processingQueued = true;
const hasQueuedMutations = queuedMutations.length > 0;
queuedMutations = [...queuedMutations, ...mutations];
if (iripo.paused || hasQueuedMutations) {
return;
}

// Have to use polyfill for Safari since it does not support `requestIdleCallback` natively.
// FIXME: Once it does, remove the second option specifying the timeout.
requestIdleCallback(
function handleRequestIdleCallback() {
iripo.processInFns(mutations);
iripo.processOutFns(mutations);
iripo.processingQueued = false;
iripo.processInFns(queuedMutations);
iripo.processOutFns(queuedMutations);
queuedMutations = [];
},
{ timeout: 1000 }
);
Expand Down