« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/file-size-preloader.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/file-size-preloader.js')
-rw-r--r--src/file-size-preloader.js23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/file-size-preloader.js b/src/file-size-preloader.js
index 4eadde7b..a5db1cb0 100644
--- a/src/file-size-preloader.js
+++ b/src/file-size-preloader.js
@@ -20,6 +20,7 @@
 import {stat} from 'node:fs/promises';
 
 import {logWarn} from '#cli';
+import {transposeArrays} from '#sugar';
 
 export default class FileSizePreloader {
   #paths = [];
@@ -101,4 +102,26 @@ export default class FileSizePreloader {
     if (index > this.#loadedPathIndex) return null;
     return this.#sizes[index];
   }
+
+  saveAsCache() {
+    const entries =
+      transposeArrays([
+        this.#paths.slice(0, this.#loadedPathIndex),
+        this.#sizes.slice(0, this.#loadedPathIndex),
+      ]);
+
+    return Object.fromEntries(entries);
+  }
+
+  loadFromCache(cache) {
+    const entries =
+      Object.entries(cache)
+        .filter(([p]) => !this.#paths.includes(p));
+
+    const [newPaths, newSizes] = transposeArrays(entries);
+
+    this.#paths.splice(this.#loadedPathIndex + 1, 0, ...newPaths);
+    this.#sizes.splice(this.#loadedPathIndex + 1, 0, ...newSizes);
+    this.#loadedPathIndex += entries.length;
+  }
 }