diff --git a/packages/gallery/src/data/image.js b/packages/gallery/src/data/image.js index a37eac0..04328e9 100644 --- a/packages/gallery/src/data/image.js +++ b/packages/gallery/src/data/image.js @@ -165,6 +165,3 @@ const processImportables = backgroundTask(async function _processImportables() { remove(_id, _rev); processImportables(); }); - -// Check if we have any unimported images. -processImportables(); diff --git a/packages/gallery/src/utils/event.js b/packages/gallery/src/utils/event.js index 592ff4d..42b3436 100644 --- a/packages/gallery/src/utils/event.js +++ b/packages/gallery/src/utils/event.js @@ -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; }