« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/search.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/search.js')
-rw-r--r--src/search.js61
1 files changed, 53 insertions, 8 deletions
diff --git a/src/search.js b/src/search.js
index a2dae9e1..138a2d2c 100644
--- a/src/search.js
+++ b/src/search.js
@@ -9,11 +9,53 @@ import FlexSearch from 'flexsearch';
 import {pack} from 'msgpackr';
 
 import {logWarn} from '#cli';
-import {makeSearchIndex, populateSearchIndex, searchSpec} from '#search-spec';
+import {makeSearchIndex} from '#search-shape';
+import searchSpec from '#search-select';
 import {stitchArrays} from '#sugar';
 import {checkIfImagePathHasCachedThumbnails, getThumbnailEqualOrSmaller}
   from '#thumbs';
 
+// TODO: This function basically mirrors bind-utilities.js, which isn't
+// exactly robust, but... binding might need some more thought across the
+// codebase in *general.*
+function bindSearchUtilities({
+  checkIfImagePathHasCachedThumbnails,
+  getThumbnailEqualOrSmaller,
+  thumbsCache,
+  urls,
+}) {
+  const bound = {
+    urls,
+  };
+
+  bound.checkIfImagePathHasCachedThumbnails =
+    (imagePath) =>
+      checkIfImagePathHasCachedThumbnails(imagePath, thumbsCache);
+
+  bound.getThumbnailEqualOrSmaller =
+    (preferred, imagePath) =>
+      getThumbnailEqualOrSmaller(preferred, imagePath, thumbsCache);
+
+  return bound;
+}
+
+function populateSearchIndex(index, descriptor, wikiData, utilities) {
+  for (const thing of descriptor.select(wikiData)) {
+    const reference = thing.constructor.getReference(thing);
+
+    let processed;
+    try {
+      processed = descriptor.process(thing, utilities);
+    } catch (caughtError) {
+      throw new Error(
+        `Failed to process searchable thing ${reference}`,
+        {cause: caughtError});
+    }
+
+    index.add({reference, ...processed});
+  }
+}
+
 async function serializeIndex(index) {
   const results = {};
 
@@ -60,17 +102,20 @@ export async function writeSearchData({
       .map(descriptor =>
         makeSearchIndex(descriptor, {FlexSearch}));
 
+  const utilities =
+    bindSearchUtilities({
+      checkIfImagePathHasCachedThumbnails,
+      getThumbnailEqualOrSmaller,
+      thumbsCache,
+      urls,
+      wikiData,
+    });
+
   stitchArrays({
     index: indexes,
     descriptor: descriptors,
   }).forEach(({index, descriptor}) =>
-      populateSearchIndex(index, descriptor, {
-        checkIfImagePathHasCachedThumbnails,
-        getThumbnailEqualOrSmaller,
-        thumbsCache,
-        urls,
-        wikiData,
-      }));
+      populateSearchIndex(index, descriptor, wikiData, utilities));
 
   const serializedIndexes =
     await Promise.all(indexes.map(serializeIndex));