BackgroundTask no longer debounces

This commit is contained in:
Timothy Farrell 2017-10-27 09:19:03 -05:00
parent 36773bf5c0
commit a6b6d00f49

View File

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