« get me outta code hell

infra: factor out prepareWorkingGenerateFunction - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-10-07 12:30:29 -0300
committer(quasar) nebula <qznebula@protonmail.com>2025-10-07 12:31:06 -0300
commit49f78d3c057c6d6a68924c0e64308a4b3e85b559 (patch)
treed1c5f5d7aecc295a55c05f93b2f6a360d328a1a4
parent05e835e6cdaee2581dca45f4ba618ff278b9a280 (diff)
infra: factor out prepareWorkingGenerateFunction
-rw-r--r--src/content-function.js73
1 files changed, 34 insertions, 39 deletions
diff --git a/src/content-function.js b/src/content-function.js
index 0f49936e..f3aa7c42 100644
--- a/src/content-function.js
+++ b/src/content-function.js
@@ -13,6 +13,14 @@ const DECORATE_TIME = process.env.HSMUSIC_DEBUG_CONTENT_PERF === '1';
 
 export class ContentFunctionSpecError extends Error {}
 
+function optionalDecorateTime(prefix, fn) {
+  if (DECORATE_TIME) {
+    return decorateTime(`${prefix}/${generate.name}`, fn);
+  } else {
+    return fn;
+  }
+}
+
 export default function contentFunction(spec) {
   if (!spec.generate) {
     throw new ContentFunctionSpecError(`Expected generate function`);
@@ -22,19 +30,34 @@ export default function contentFunction(spec) {
     Template.validateSlotsDescription(spec.slots);
   }
 
-  return expectDependencies(spec);
+  return expectExtraDependencies(spec, null);
 }
 
 contentFunction.identifyingSymbol = Symbol(`Is a content function?`);
 
-export function expectDependencies(spec, {
-  boundExtraDependencies = null,
-} = {}) {
-  const optionalDecorateTime = (prefix, fn) =>
-    (DECORATE_TIME
-      ? decorateTime(`${prefix}/${generate.name}`, fn)
-      : fn);
+export function expectExtraDependencies(spec, boundExtraDependencies) {
+  const generate =
+    (boundExtraDependencies
+      ? prepareWorkingGenerateFunction(spec, boundExtraDependencies)
+      : () => {
+          throw new Error(`Not bound with extraDependencies yet`);
+        });
+
+  generate[contentFunction.identifyingSymbol] = true;
+
+  for (const key of ['sprawl', 'query', 'relations', 'data']) {
+    if (spec[key]) {
+      generate[key] = optionalDecorateTime(`sprawl`, spec[key]);
+    }
+  }
+
+  generate.bindExtraDependencies = (extraDependencies) =>
+    expectExtraDependencies(spec, extraDependencies);
 
+  return generate;
+}
+
+function prepareWorkingGenerateFunction(spec, boundExtraDependencies) {
   let generate = ([arg1, arg2], ...extraArgs) => {
     if (spec.data && !arg1) {
       throw new Error(`Expected data`);
@@ -80,10 +103,8 @@ export function expectDependencies(spec, {
   generate = optionalDecorateTime(`generate`, generate);
 
   if (spec.slots) {
-    const normalGenerate = generate;
-
     let stationery = null;
-    generate = function(...args) {
+    return (...args) => {
       stationery ??= boundExtraDependencies.html.stationery({
         annotation: generate.name,
 
@@ -99,7 +120,7 @@ export function expectDependencies(spec, {
 
         content(slots) {
           const args = [slots._cfArg1, slots._cfArg2];
-          return normalGenerate(args, slots);
+          return generate(args, slots);
         },
       });
 
@@ -108,35 +129,9 @@ export function expectDependencies(spec, {
         _cfArg2: args[1] ?? null,
       });
     };
-  } else {
-    const normalGenerate = generate;
-    generate = (...args) => normalGenerate(args);
   }
 
-  generate.fulfill = function() {
-    throw new Error(`not part of the flow`);
-  };
-
-  Object.defineProperty(generate, 'fulfilled', {
-    get() {
-      throw new Error(`unknowable`);
-    }
-  });
-
-  generate[contentFunction.identifyingSymbol] = true;
-
-  for (const key of ['sprawl', 'query', 'relations', 'data']) {
-    if (spec[key]) {
-      generate[key] = optionalDecorateTime(`sprawl`, spec[key]);
-    }
-  }
-
-  generate.bindExtraDependencies = (extraDependencies) =>
-    expectDependencies(spec, {
-      boundExtraDependencies: extraDependencies,
-    });
-
-  return generate;
+  return (...args) => generate(args);
 }
 
 export function getArgsForRelationsAndData(contentFunction, wikiData, ...args) {