« get me outta code hell

client: stub search index download progress - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/static/js/search-worker.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-15 21:32:50 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:54 -0300
commitfec9c6556e181fc68068eae3db9e8ec81208221d (patch)
treea0c9f26b6bd89a1841839382b6f35cd60eb9a7d9 /src/static/js/search-worker.js
parent5d1151d352a092bd27c982ffde3ce2dfd093ddbb (diff)
client: stub search index download progress
Diffstat (limited to 'src/static/js/search-worker.js')
-rw-r--r--src/static/js/search-worker.js39
1 files changed, 33 insertions, 6 deletions
diff --git a/src/static/js/search-worker.js b/src/static/js/search-worker.js
index c2bdcaee..9a7827da 100644
--- a/src/static/js/search-worker.js
+++ b/src/static/js/search-worker.js
@@ -14,6 +14,7 @@ import {
 } from '../shared-util/sugar.js';
 
 import {loadDependency} from './module-import-shims.js';
+import {fetchWithProgress} from './xhr-util.js';
 
 // Will be loaded from dependencies.
 let decompress;
@@ -24,7 +25,7 @@ let idb;
 let status = null;
 let indexes = null;
 
-globalThis.onmessage = handleWindowMessage;
+onmessage = handleWindowMessage;
 postStatus('alive');
 
 Promise.all([
@@ -158,6 +159,8 @@ async function main() {
 
   const [indexData, idbIndexData] = await background;
 
+  delete idbIndexData.generic;
+
   const keysNeedingFetch =
     (idbIndexData
       ? Object.keys(indexData)
@@ -170,10 +173,34 @@ async function main() {
     Object.keys(indexData)
       .filter(key => !keysNeedingFetch.includes(key))
 
+  if (!empty(keysNeedingFetch)) {
+    postMessage({
+      kind: 'download-begun',
+      context: 'search-indexes',
+      keys: keysNeedingFetch,
+    });
+  }
+
   const fetchPromises =
-    keysNeedingFetch
-      .map(key => rebase(key + '.json.msgpack'))
-      .map(url => fetch(url));
+    keysNeedingFetch.map(key =>
+      fetchWithProgress(
+        rebase(key + '.json.msgpack'),
+        progress => {
+          postMessage({
+            kind: 'download-progress',
+            context: 'search-indexes',
+            progress: progress / 1.00,
+            key,
+          });
+        }).then(response => {
+            postMessage({
+              kind: 'download-complete',
+              context: 'search-indexes',
+              key,
+            });
+
+            return response;
+          }));
 
   const fetchBlobPromises =
     fetchPromises
@@ -316,14 +343,14 @@ async function handleWindowActionMessage(message) {
 
 function postStatus(newStatus) {
   status = newStatus;
-  globalThis.postMessage({
+  postMessage({
     kind: 'status',
     status: newStatus,
   });
 }
 
 function postActionResult(id, status, value) {
-  globalThis.postMessage({
+  postMessage({
     kind: 'result',
     id,
     status,