« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/yaml.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/yaml.js')
-rw-r--r--src/data/yaml.js102
1 files changed, 51 insertions, 51 deletions
diff --git a/src/data/yaml.js b/src/data/yaml.js
index 64223662..a1eb73fb 100644
--- a/src/data/yaml.js
+++ b/src/data/yaml.js
@@ -1225,93 +1225,92 @@ export async function loadAndProcessDataDocuments(dataSteps, {dataPath}) {
 // Data linking! Basically, provide (portions of) wikiData to the Things which
 // require it - they'll expose dynamically computed properties as a result (many
 // of which are required for page HTML generation and other expected behavior).
-export function linkWikiDataArrays(wikiData) {
+export function linkWikiDataArrays(wikiData, {bindFind, bindReverse}) {
   const linkWikiDataSpec = new Map([
+    // entries must be present here even without any properties to explicitly
+    // link if the 'find' or 'reverse' properties will be implicitly linked
+
     [wikiData.albumData, [
       'albumData',
-      'artTagData',
-      'artistData',
-      'groupData',
       'trackData',
       'wikiInfo',
     ]],
 
-    [wikiData.artTagData, [
-      'albumData',
-      'trackData',
-    ]],
+    [wikiData.artTagData, [/* reverse */]],
 
-    [wikiData.artistData, [
-      'albumData',
-      'artistData',
-      'flashData',
-      'groupData',
-      'trackData',
-    ]],
+    [wikiData.artistData, [/* find, reverse */]],
 
     [wikiData.flashData, [
-      'artistData',
-      'flashActData',
-      'trackData',
       'wikiInfo',
     ]],
 
-    [wikiData.flashActData, [
-      'flashData',
-      'flashSideData',
-    ]],
+    [wikiData.flashActData, [/* find, reverse */]],
 
-    [wikiData.flashSideData, [
-      'flashActData',
-    ]],
+    [wikiData.flashSideData, [/* find */]],
 
-    [wikiData.groupData, [
-      'albumData',
-      'artistData',
-      'groupCategoryData',
-    ]],
+    [wikiData.groupData, [/* find, reverse */]],
 
-    [wikiData.groupCategoryData, [
-      'groupData',
-    ]],
+    [wikiData.groupCategoryData, [/* find */]],
 
-    [wikiData.homepageLayout?.rows, [
-      'albumData',
-      'groupData',
-    ]],
+    [wikiData.homepageLayout.rows, [/* find */]],
 
     [wikiData.trackData, [
       'albumData',
-      'artTagData',
-      'artistData',
-      'flashData',
       'trackData',
-      'trackSectionData',
       'wikiInfo',
     ]],
 
-    [wikiData.trackSectionData, [
-      'albumData',
-    ]],
+    [wikiData.trackSectionData, [/* reverse */]],
 
-    [[wikiData.wikiInfo], [
-      'groupData',
-    ]],
+    [[wikiData.wikiInfo], [/* find */]],
   ]);
 
+  const constructorHasFindMap = new Map();
+  const constructorHasReverseMap = new Map();
+
+  const boundFind = bindFind(wikiData);
+  const boundReverse = bindReverse(wikiData);
+
   for (const [things, keys] of linkWikiDataSpec.entries()) {
     if (things === undefined) continue;
+
     for (const thing of things) {
       if (thing === undefined) continue;
+
+      let hasFind;
+      if (constructorHasFindMap.has(thing.constructor)) {
+        hasFind = constructorHasFindMap.get(thing.constructor);
+      } else {
+        hasFind = 'find' in thing;
+        constructorHasFindMap.set(thing.constructor, hasFind);
+      }
+
+      if (hasFind) {
+        thing.find = boundFind;
+      }
+
+      let hasReverse;
+      if (constructorHasReverseMap.has(thing.constructor)) {
+        hasReverse = constructorHasReverseMap.get(thing.constructor);
+      } else {
+        hasReverse = 'reverse' in thing;
+        constructorHasReverseMap.set(thing.constructor, hasReverse);
+      }
+
+      if (hasReverse) {
+        thing.reverse = boundReverse;
+      }
+
       for (const key of keys) {
         if (!(key in wikiData)) continue;
+
         thing[key] = wikiData[key];
       }
     }
   }
 }
 
-export function sortWikiDataArrays(dataSteps, wikiData) {
+export function sortWikiDataArrays(dataSteps, wikiData, {bindFind, bindReverse}) {
   for (const [key, value] of Object.entries(wikiData)) {
     if (!Array.isArray(value)) continue;
     wikiData[key] = value.slice();
@@ -1327,7 +1326,7 @@ export function sortWikiDataArrays(dataSteps, wikiData) {
   // slices instead of the original arrays) - this is so that the object
   // caching system understands that it's working with a new ordering.
   // We still need to actually provide those updated arrays over again!
-  linkWikiDataArrays(wikiData);
+  linkWikiDataArrays(wikiData, {bindFind, bindReverse});
 }
 
 // Utility function for loading all wiki data from the provided YAML data
@@ -1339,6 +1338,7 @@ export function sortWikiDataArrays(dataSteps, wikiData) {
 export async function quickLoadAllFromYAML(dataPath, {
   find,
   bindFind,
+  bindReverse,
   getAllFindSpecs,
 
   showAggregate: customShowAggregate = showAggregate,
@@ -1363,7 +1363,7 @@ export async function quickLoadAllFromYAML(dataPath, {
     }
   }
 
-  linkWikiDataArrays(wikiData);
+  linkWikiDataArrays(wikiData, {bindFind, bindReverse});
 
   try {
     reportDirectoryErrors(wikiData, {getAllFindSpecs});
@@ -1389,7 +1389,7 @@ export async function quickLoadAllFromYAML(dataPath, {
     logWarn`Content text errors found.`;
   }
 
-  sortWikiDataArrays(dataSteps, wikiData);
+  sortWikiDataArrays(dataSteps, wikiData, {bindFind, bindReverse});
 
   return wikiData;
 }