From 94b9895a5a0d836f9e91ef87779ac29a1b2c525f Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 3 May 2024 16:18:04 -0300 Subject: client: wrap search-worker main code in function Avoids top-level await in client code and organizes control flow a little nicer. --- src/static/js/search-worker.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'src/static') diff --git a/src/static/js/search-worker.js b/src/static/js/search-worker.js index 7932655b..24412b22 100644 --- a/src/static/js/search-worker.js +++ b/src/static/js/search-worker.js @@ -4,28 +4,34 @@ import {withEntries} from '../shared-util/sugar.js'; import FlexSearch from '../lib/flexsearch/flexsearch.bundle.module.min.js'; let status = null; +let indexes = null; +let searchData = null; onmessage = handleWindowMessage; - postStatus('alive'); -const indexes = - makeSearchIndexes(FlexSearch); +main().then( + () => { + postStatus('ready'); + }); + +async function main() { + indexes = + makeSearchIndexes(FlexSearch); -const searchData = - await fetch('/search-data/index.json') - .then(resp => resp.json()); + searchData = + await fetch('/search-data/index.json') + .then(resp => resp.json()); -// If this fails, it's because an outdated index was cached. -// TODO: If this fails, try again once with a cache busting url. -for (const [indexName, indexData] of Object.entries(searchData)) { - for (const [key, value] of Object.entries(indexData)) { - indexes[indexName].import(key, value); + // If this fails, it's because an outdated index was cached. + // TODO: If this fails, try again once with a cache busting url. + for (const [indexName, indexData] of Object.entries(searchData)) { + for (const [key, value] of Object.entries(indexData)) { + indexes[indexName].import(key, value); + } } } -postStatus('ready'); - function handleWindowMessage(message) { switch (message.data.kind) { case 'action': -- cgit 1.3.0-6-gf8a5