fuzzyworld

hawk / commit

Changed a bit how API progress is reported.

e39bdb17de964e384ee1d960ce0f62f055dfbf02 · Pavel Safronov · 2023-10-14 11:38:03 -0700 · parents 09b64e4c5f3e

Changed a bit how API progress is reported.
 public/api.js  | 15 +++++++++++----
 public/main.js | 19 +++++++++++++++----
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/public/api.js b/public/api.js
index 859eee2..16431ab 100644
--- a/public/api.js
+++ b/public/api.js
@@ -1,12 +1,19 @@
 const apiExpiry = 3600 * 1000; // 1 hour
 
+function createCallbackState(processedCount, totalCount) {
+    return {
+        processedCount: processedCount,
+        totalCount: totalCount
+    };
+}
+
 async function getAlbums(statusCallback) {
     const db = await openDb();
     const cacheKey = 'albums';
     const cachedAlbums = await getWithExpiry(db, cacheKey);
     if (cachedAlbums) {
         if (statusCallback) {
-            statusCallback(`Retrieved ${cachedAlbums.length} albums`)
+            statusCallback(createCallbackState(cachedAlbums.length, cachedAlbums.length))
         }
         return cachedAlbums;
     }
@@ -23,7 +30,7 @@ async function getAlbums(statusCallback) {
         const data = await response.json();
         albums.push(...data.items);
         if (statusCallback) {
-            statusCallback(`Retrieved ${albums.length} albums`);
+            statusCallback(createCallbackState(albums.length, data.total))
         }
         url = data.next;
     }
@@ -38,7 +45,7 @@ async function getTracks(albumId, statusCallback) {
     const cachedTracks = await getWithExpiry(db, cacheKey);
     if (cachedTracks) {
         if (statusCallback) {
-            statusCallback(`Retrieved ${cachedTracks.length} tracks`)
+            statusCallback(createCallbackState(cachedTracks.length, cachedTracks.length))
         }
         return cachedTracks;
     }
@@ -55,7 +62,7 @@ async function getTracks(albumId, statusCallback) {
         const data = await response.json();
         tracks.push(...data.items);
         if (statusCallback) {
-            statusCallback(`Retrieved ${tracks.length} tracks`)
+            statusCallback(createCallbackState(tracks.length, data.total))
         }
         url = data.next;
     }
diff --git a/public/main.js b/public/main.js
index a34d969..3d8c49c 100644
--- a/public/main.js
+++ b/public/main.js
@@ -96,13 +96,24 @@ async function searchForMatch() {
 
 async function search(minutes, seconds) {
     reportStatus(`Retrieving albums`);
-    const albums = await getAlbums(null);
+    const albums = await getAlbums((state) => {
+        const { processedCount, totalCount } = state;
+        if (totalCount <= 0) {
+            totalCount = "???"
+        }
+        reportStatus(`Retrieving albums: ${processedCount} of ${totalCount}`);
+    });
 
     const foundTracks = [];
     for (let i = 0; i < albums.length; i++) {
         const album = albums[i];
-        const tracks = await getTracks(album.album.id, null);
-        reportStatus(`Retrieving tracks for album ${i + 1} of ${albums.length}`)
+        const tracks = await getTracks(album.album.id, (state) => {
+            const { processedCount, totalCount } = state;
+            if (totalCount <= 0) {
+                totalCount = "???"
+            }
+            reportStatus(`Retrieving tracks for album ${i + 1} of ${albums.length}: ${processedCount} of ${totalCount}`);
+        });
 
         for (let j = 0; j < tracks.length; j++) {
             const track = tracks[j];
@@ -140,7 +151,7 @@ let statusText = '';
 setInterval(() => {
     const statusDiv = document.getElementById('status');
     statusDiv.innerText = statusText;
-}, 1000);
+}, 100);
 
 function reportStatus(text) {
     statusText = text;