Add convenience logging.

This commit is contained in:
Timothy Farrell 2017-05-18 14:01:38 -05:00
parent 236a3c7f72
commit b2f3615754
2 changed files with 23 additions and 10 deletions

View File

@ -165,6 +165,3 @@ const processImportables = backgroundTask(async function _processImportables() {
remove(_id, _rev); remove(_id, _rev);
processImportables(); processImportables();
}); });
// Check if we have any unimported images.
processImportables();

View File

@ -57,18 +57,27 @@ if (!global.requestIdleCallback) {
}; };
} }
export function backgroundTask(fn) { export function backgroundTask(fn, initialDelay = 500) {
let id = null; let id = null;
let reRunCount = 0; let reRunCount = 0;
let params = [];
function runTask({ didTimeout, timeRemaining }) { async function runTask({ didTimeout }) {
if (didTimeout) { if (didTimeout) {
id = requestIdleCallback(runTask); id = requestIdleCallback(runTask);
return; return;
} }
const start = Date.now(); const start = Date.now();
fn(); group(fn.name);
if (reRunCount && Date.now() - start < timeRemaining()) { if (params.length) {
log(`${fn.name} params: `, ...params);
}
await fn(...params);
const executionTime = Date.now() - start;
log(`${fn.name} execution time: ${executionTime}ms`);
groupEnd(fn.name);
params = [];
if (reRunCount) {
reRunCount -= 1; reRunCount -= 1;
id = requestIdleCallback(runTask); id = requestIdleCallback(runTask);
} else { } else {
@ -76,12 +85,19 @@ export function backgroundTask(fn) {
} }
} }
return () => { const wrapper = (...args) => {
if (id !== null) { if (id !== null) {
reRunCount += 1; reRunCount += 1;
return; return false;
} }
params = args;
id = requestIdleCallback(runTask); id = requestIdleCallback(runTask);
return; return true;
}; };
if (initialDelay) {
setTimeout(wrapper, initialDelay);
}
return wrapper;
} }