diff options
-rw-r--r-- | src/static/js/search-worker.js | 86 |
1 files changed, 47 insertions, 39 deletions
diff --git a/src/static/js/search-worker.js b/src/static/js/search-worker.js index 5c7f43f6..8e83fe02 100644 --- a/src/static/js/search-worker.js +++ b/src/static/js/search-worker.js @@ -141,38 +141,18 @@ function rebase(path) { return `/search-data/` + path; } -async function main() { - let background; - - background = - Promise.all([ - fetch(rebase('index.json')) - .then(resp => resp.json()), - - loadCachedIndexFromIDB(), - ]); - - indexes = - withEntries(searchSpec, entries => entries - .map(([key, descriptor]) => [ - key, - makeSearchIndex(descriptor, {FlexSearch}), - ])); - - const [indexData, idbIndexData] = await background; - - const keysNeedingFetch = - (idbIndexData - ? Object.keys(indexData) - .filter(key => - indexData[key].md5 !== - idbIndexData[key]?.md5) - : Object.keys(indexData)); - - const keysFromCache = - Object.keys(indexData) - .filter(key => !keysNeedingFetch.includes(key)) +async function prepareIndexData() { + return Promise.all([ + fetch(rebase('index.json')) + .then(resp => resp.json()), + + loadCachedIndexFromIDB(), + ]).then( + ([indexData, idbIndexData]) => + ({indexData, idbIndexData})); +} +function fetchIndexes(keysNeedingFetch) { if (!empty(keysNeedingFetch)) { postMessage({ kind: 'download-begun', @@ -181,7 +161,7 @@ async function main() { }); } - const fetchPromises = + return ( keysNeedingFetch.map(key => fetchWithProgress( rebase(key + '.json.msgpack'), @@ -200,7 +180,41 @@ async function main() { }); return response; - })); + }))); +} + +async function main() { + const prepareIndexDataPromise = prepareIndexData(); + + indexes = + withEntries(searchSpec, entries => entries + .map(([key, descriptor]) => [ + key, + makeSearchIndex(descriptor, {FlexSearch}), + ])); + + const {indexData, idbIndexData} = await prepareIndexDataPromise; + + const keysNeedingFetch = + (idbIndexData + ? Object.keys(indexData) + .filter(key => + indexData[key].md5 !== + idbIndexData[key]?.md5) + : Object.keys(indexData)); + + const keysFromCache = + Object.keys(indexData) + .filter(key => !keysNeedingFetch.includes(key)) + + const cacheArrayBufferPromises = + keysFromCache + .map(key => idbIndexData[key]) + .map(({cachedBinarySource}) => + cachedBinarySource.arrayBuffer()); + + const fetchPromises = + fetchIndexes(keysNeedingFetch); const fetchBlobPromises = fetchPromises @@ -212,12 +226,6 @@ async function main() { .map(promise => promise .then(blob => blob.arrayBuffer())); - const cacheArrayBufferPromises = - keysFromCache - .map(key => idbIndexData[key]) - .map(({cachedBinarySource}) => - cachedBinarySource.arrayBuffer()); - function arrayBufferToJSON(data) { data = new Uint8Array(data); data = unpack(data); |