« get me outta code hell

client: wrap search-worker main code in function - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-03 16:18:04 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:48 -0300
commit94b9895a5a0d836f9e91ef87779ac29a1b2c525f (patch)
tree6a3fac2e9d99119a99436fe6b563ab33d8e1bca9 /src
parentf415551fb45174f2618f916fd323fb076adfd7a6 (diff)
client: wrap search-worker main code in function
Avoids top-level await in client code and organizes control flow
a little nicer.
Diffstat (limited to 'src')
-rw-r--r--src/static/js/search-worker.js32
1 files changed, 19 insertions, 13 deletions
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':