BackgroundTask no longer debounces

This commit is contained in:
Timothy Farrell 2017-10-27 09:19:03 -05:00
parent 941e1f6b1b
commit 6f103b92bc

View File

@ -59,8 +59,7 @@ if (!global.requestIdleCallback) {
export function backgroundTask(fn, initialDelay = 500) {
let id = null;
let reRunCount = 0;
let params = [];
const params = [];
async function runTask({ didTimeout }) {
if (didTimeout) {
@ -69,16 +68,15 @@ export function backgroundTask(fn, initialDelay = 500) {
}
const start = Date.now();
group(fn.name);
if (params.length) {
log(`${fn.name} params: `, ...params);
const p = params.shift();
if (p.length) {
log(`${fn.name} params: `, ...p);
}
await fn(...params);
await fn(...p);
const executionTime = Date.now() - start;
log(`${fn.name} execution time: ${executionTime}ms`);
groupEnd(fn.name);
params = [];
if (reRunCount) {
reRunCount -= 1;
if (params.length) {
id = requestIdleCallback(runTask);
} else {
id = null;
@ -86,11 +84,10 @@ export function backgroundTask(fn, initialDelay = 500) {
}
const wrapper = (...args) => {
params.push(args);
if (id !== null) {
reRunCount += 1;
return false;
}
params = args;
id = requestIdleCallback(runTask);
return true;
};