Add convenience logging.
This commit is contained in:
parent
0268f01bf3
commit
ade9b64a13
@ -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();
|
|
||||||
|
|||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user