« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/file-size-preloader.js26
-rwxr-xr-xsrc/upd8.js5
2 files changed, 25 insertions, 6 deletions
diff --git a/src/file-size-preloader.js b/src/file-size-preloader.js
index a5db1cb0..5eff243a 100644
--- a/src/file-size-preloader.js
+++ b/src/file-size-preloader.js
@@ -18,6 +18,7 @@
 // are very, very fast.
 
 import {stat} from 'node:fs/promises';
+import {relative, resolve} from 'node:path';
 
 import {logWarn} from '#cli';
 import {transposeArrays} from '#sugar';
@@ -32,6 +33,10 @@ export default class FileSizePreloader {
 
   hadErrored = false;
 
+  constructor({prefix = ''} = {}) {
+    this.prefix = prefix;
+  }
+
   loadPaths(...paths) {
     this.#paths.push(...paths.filter((p) => !this.#paths.includes(p)));
     return this.#startLoadingPaths();
@@ -46,9 +51,9 @@ export default class FileSizePreloader {
       return this.#loadingPromise;
     }
 
-    this.#loadingPromise = new Promise((resolve) => {
-      this.#resolveLoadingPromise = resolve;
-    });
+    ({promise: this.#loadingPromise,
+      resolve: this.#resolveLoadingPromise} =
+        Promise.withResolvers());
 
     this.#loadNextPath();
 
@@ -97,6 +102,14 @@ export default class FileSizePreloader {
   }
 
   getSizeOfPath(path) {
+    let size = this.#getSizeOfPath(path);
+    if (size || !this.prefix) return size;
+    const path2 = resolve(this.prefix, path);
+    if (path2 === path) return null;
+    return this.#getSizeOfPath(path2);
+  }
+
+  #getSizeOfPath(path) {
     const index = this.#paths.indexOf(path);
     if (index === -1) return null;
     if (index > this.#loadedPathIndex) return null;
@@ -106,7 +119,9 @@ export default class FileSizePreloader {
   saveAsCache() {
     const entries =
       transposeArrays([
-        this.#paths.slice(0, this.#loadedPathIndex),
+        this.#paths.slice(0, this.#loadedPathIndex)
+          .map(path => relative(this.prefix, path)),
+
         this.#sizes.slice(0, this.#loadedPathIndex),
       ]);
 
@@ -118,7 +133,8 @@ export default class FileSizePreloader {
       Object.entries(cache)
         .filter(([p]) => !this.#paths.includes(p));
 
-    const [newPaths, newSizes] = transposeArrays(entries);
+    let [newPaths, newSizes] = transposeArrays(entries);
+    newPaths = newPaths.map(p => resolve(this.prefix, p));
 
     this.#paths.splice(this.#loadedPathIndex + 1, 0, ...newPaths);
     this.#sizes.splice(this.#loadedPathIndex + 1, 0, ...newSizes);
diff --git a/src/upd8.js b/src/upd8.js
index b9e71784..b1ca3577 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -2566,7 +2566,10 @@ async function main() {
       timeStart: Date.now(),
     });
 
-    const fileSizePreloader = new FileSizePreloader();
+    const fileSizePreloader =
+      new FileSizePreloader({
+        prefix: mediaPath,
+      });
 
     const mediaFilePaths =
       await traverse(mediaPath, {