Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let refCount = 0;
let resolvers = [];
function resolveAll() {
for (let resolve of resolvers)
resolve();
resolvers = [];
}
function checkRefCount() {
if (refCount > 0)
return ;
resolveAll();
}
function checkBrowserIdle() {
if (window.requestIdleCallback) {
window.requestIdleCallback(checkRefCount);
} else {
window.setTimeout(checkRefCount, 0);
}
}
export async function requestIdle() {
return new Promise(resolve => {
resolvers.push(resolve);
checkBrowserIdle();
});
}
requestIdle.increaseRefCount = () => {
refCount += 1;
};
requestIdle.decreaseRefCount = () => {
refCount -= 1;
checkBrowserIdle();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.