« get me outta code hell

infra: don't perform redundant checks in expectDependencies - 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-06-11 11:25:34 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-06-11 11:25:34 -0300
commit0a3b163772ab56ec0c0e775deec0ec3bed2b2825 (patch)
tree2646bfa9a986f7b0efcc67a03198ae6e43247b45 /src
parent957f9b83171011f2da63f2e0880be30a4dc0d978 (diff)
infra: don't perform redundant checks in expectDependencies
Diffstat (limited to 'src')
-rw-r--r--src/content-function.js24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/content-function.js b/src/content-function.js
index 73e4629e..2eb12f0e 100644
--- a/src/content-function.js
+++ b/src/content-function.js
@@ -9,6 +9,22 @@ export default function contentFunction({
   data,
   generate,
 }) {
+  // Initial checks. These only need to be run once per description of a
+  // content function, and don't depend on any mutable context (e.g. which
+  // dependencies have been fulfilled so far).
+
+  if (!generate) {
+    throw new Error(`Expected generate function`);
+  }
+
+  if (sprawl && !extraDependencies.includes('wikiData')) {
+    throw new Error(`Content functions which sprawl must specify wikiData in extraDependencies`);
+  }
+
+  // Pass all the details to expectDependencies, which will recursively build
+  // up a set of fulfilled dependencies and make functions like `relations`
+  // and `generate` callable only with sufficient fulfilled dependencies.
+
   return expectDependencies({
     sprawl,
     relations,
@@ -33,18 +49,10 @@ export function expectDependencies({
   expectedExtraDependencyKeys,
   fulfilledDependencies,
 }) {
-  if (!generate) {
-    throw new Error(`Expected generate function`);
-  }
-
   const hasSprawlFunction = !!sprawl;
   const hasRelationsFunction = !!relations;
   const hasDataFunction = !!data;
 
-  if (hasSprawlFunction && !expectedExtraDependencyKeys.includes('wikiData')) {
-    throw new Error(`Content functions which sprawl must specify wikiData in extraDependencies`);
-  }
-
   const fulfilledDependencyKeys = Object.keys(fulfilledDependencies);
 
   const invalidatingDependencyKeys = Object.entries(fulfilledDependencies)