Add convenience logging.

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

View File

@ -165,6 +165,3 @@ const processImportables = backgroundTask(async function _processImportables() {
remove(_id, _rev);
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 reRunCount = 0;
let params = [];
function runTask({ didTimeout, timeRemaining }) {
async function runTask({ didTimeout }) {
if (didTimeout) {
id = requestIdleCallback(runTask);
return;
}
const start = Date.now();
fn();
if (reRunCount && Date.now() - start < timeRemaining()) {
group(fn.name);
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;
id = requestIdleCallback(runTask);
} else {
@ -76,12 +85,19 @@ export function backgroundTask(fn) {
}
}
return () => {
const wrapper = (...args) => {
if (id !== null) {
reRunCount += 1;
return;
return false;
}
params = args;
id = requestIdleCallback(runTask);
return;
return true;
};
if (initialDelay) {
setTimeout(wrapper, initialDelay);
}
return wrapper;
}