« get me outta code hell

data steps: contentFunction fixes & polish - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-03-23 15:00:19 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-03-23 15:00:19 -0300
commit95465bae3aaa92cb617c873bcbbfe8906bea7506 (patch)
tree13d2e3075003e2b7f8864bd7ae86f6ab4a44c5c7 /src
parentf1b426d5e9c382bad3fb9a158260c4ef7b78b268 (diff)
data steps: contentFunction fixes & polish
Diffstat (limited to 'src')
-rw-r--r--src/content-function.js53
1 files changed, 29 insertions, 24 deletions
diff --git a/src/content-function.js b/src/content-function.js
index 5cd5392c..891a348f 100644
--- a/src/content-function.js
+++ b/src/content-function.js
@@ -40,7 +40,7 @@ export function expectDependencies({
   const fulfilledDependencyKeys = Object.keys(fulfilledDependencies);
 
   const invalidatingDependencyKeys = Object.entries(fulfilledDependencies)
-    .filter(([key, value]) => value.fulfilled === false)
+    .filter(([key, value]) => value?.fulfilled === false)
     .map(([key]) => key);
 
   const missingContentDependencyKeys = expectedContentDependencyKeys
@@ -57,39 +57,37 @@ export function expectDependencies({
     };
 
     annotateFunction(wrappedGenerate, {name: generate, trait: 'invalidated'});
-    wrappedGenerate.fulfilled ??= false;
-  }
-
-  if (empty(missingContentDependencyKeys) && empty(missingExtraDependencyKeys)) {
-    wrappedGenerate ??= function(data, relations) {
-      if (hasDataFunction && !data) {
+    wrappedGenerate.fulfilled = false;
+  } else if (empty(missingContentDependencyKeys) && empty(missingExtraDependencyKeys)) {
+    wrappedGenerate = function(arg1, arg2) {
+      if (hasDataFunction && !arg1) {
         throw new Error(`Expected data`);
       }
 
-      if (hasRelationsFunction && !relations) {
+      if (hasDataFunction && hasRelationsFunction && !arg2) {
+        throw new Error(`Expected relations`);
+      }
+
+      if (hasRelationsFunction && !arg1) {
         throw new Error(`Expected relations`);
       }
 
       if (hasDataFunction && hasRelationsFunction) {
-        return generate(data, relations, fulfilledDependencies);
-      } else if (hasDataFunction) {
-        return generate(data, fulfilledDependencies);
-      } else if (hasRelationsFunction) {
-        return generate(relations, fulfilledDependencies);
+        return generate(arg1, arg2, fulfilledDependencies);
+      } else if (hasDataFunction || hasRelationsFunction) {
+        return generate(arg1, fulfilledDependencies);
       } else {
         return generate(fulfilledDependencies);
       }
     };
 
     annotateFunction(wrappedGenerate, {name: generate, trait: 'fulfilled'});
-    wrappedGenerate.fulfilled ??= true;
+    wrappedGenerate.fulfilled = true;
 
     wrappedGenerate.fulfill = function() {
       throw new Error(`All dependencies already fulfilled`);
     };
-  }
-
-  if (!wrappedGenerate) {
+  } else {
     wrappedGenerate = function() {
       throw new Error(`Dependencies still needed: ${missingContentDependencyKeys.concat(missingExtraDependencyKeys).join(', ')}`);
     };
@@ -136,6 +134,11 @@ export function expectDependencies({
     });
   };
 
+  Object.assign(wrappedGenerate, {
+    contentDependencies: expectedContentDependencyKeys,
+    extraDependencies: expectedExtraDependencyKeys,
+  });
+
   return wrappedGenerate;
 }
 
@@ -153,10 +156,6 @@ export function fulfillDependencies({
   let bail = false;
 
   for (let [key, value] of Object.entries(dependencies)) {
-    if (key.startsWith('u_')) {
-      key = key.slice(2);
-    }
-
     if (fulfilledDependencyKeys.includes(key)) {
       errors.push(new Error(`Dependency ${key} is already fulfilled`));
       bail = true;
@@ -172,13 +171,19 @@ export function fulfillDependencies({
       continue;
     }
 
-    if (isContentKey && !value[contentFunction.identifyingSymbol]) {
-      errors.push(new Error(`Content dependency ${key} is not a content function`));
+    if (value === undefined) {
+      errors.push(new Error(`Dependency ${key} was provided undefined`));
+      bail = true;
+      continue;
+    }
+
+    if (isContentKey && !value?.[contentFunction.identifyingSymbol]) {
+      errors.push(new Error(`Content dependency ${key} is not a content function (got ${value})`));
       bail = true;
       continue;
     }
 
-    if (isExtraKey && value[contentFunction.identifyingSymbol]) {
+    if (isExtraKey && value?.[contentFunction.identifyingSymbol]) {
       errors.push(new Error(`Extra dependency ${key} is a content function`));
       bail = true;
       continue;