diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2024-06-11 11:01:03 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2024-06-11 11:01:03 -0300 |
commit | ebe57e4810b003628ac947d6381dd03b70f58cd8 (patch) | |
tree | 77aa601353585f849b36f377e0d05d313249f194 | |
parent | cc053537f64c45b1ff3eb2f8952bab0a167786f4 (diff) |
client: factor out some search logic
Was trying to address a bug that is already handled just by making sure the generated search is up to date w/ search-spec lol
-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); |