« get me outta code hell

data: move composite helper functions to top function scope - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-09-06 17:45:56 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-09-06 17:45:56 -0300
commit659620b7522d0e36ca15a54716b46d83f0bfc4f3 (patch)
tree90404e7006c0368b03e5fc05c086972526a16e22 /src/data/things
parent2437ac322a4c44f2fd9f6a77ac7a65bbb3afc2c0 (diff)
data: move composite helper functions to top function scope
Diffstat (limited to 'src/data/things')
-rw-r--r--src/data/things/composite.js390
1 files changed, 195 insertions, 195 deletions
diff --git a/src/data/things/composite.js b/src/data/things/composite.js
index d3f76b11..805331a9 100644
--- a/src/data/things/composite.js
+++ b/src/data/things/composite.js
@@ -496,255 +496,255 @@ function compositeFrom(firstArg, secondArg) {
 
   aggregate.close();
 
-  const constructedDescriptor = {};
-
-  if (annotation) {
-    constructedDescriptor.annotation = annotation;
-  }
+  function _filterDependencies(availableDependencies, {
+    dependencies,
+    mapDependencies,
+    options,
+  }) {
+    const filteredDependencies =
+      (dependencies
+        ? filterProperties(availableDependencies, dependencies)
+        : {});
+
+    if (mapDependencies) {
+      for (const [to, from] of Object.entries(mapDependencies)) {
+        filteredDependencies[to] = availableDependencies[from] ?? null;
+      }
+    }
 
-  constructedDescriptor.flags = {
-    update: baseUpdates,
-    expose: baseExposes,
-    compose: baseComposes,
-  };
+    if (options) {
+      filteredDependencies['#options'] = options;
+    }
 
-  if (baseUpdates) {
-    constructedDescriptor.update = base.update;
+    return filteredDependencies;
   }
 
-  if (baseExposes) {
-    const expose = constructedDescriptor.expose = {};
-    expose.dependencies = Array.from(exposeDependencies);
-
-    const continuationSymbol = Symbol('continuation symbol');
-    const noTransformSymbol = Symbol('no-transform symbol');
-
-    function _filterDependencies(availableDependencies, {
-      dependencies,
-      mapDependencies,
-      options,
-    }) {
-      const filteredDependencies =
-        (dependencies
-          ? filterProperties(availableDependencies, dependencies)
-          : {});
-
-      if (mapDependencies) {
-        for (const [to, from] of Object.entries(mapDependencies)) {
-          filteredDependencies[to] = availableDependencies[from] ?? null;
-        }
-      }
-
-      if (options) {
-        filteredDependencies['#options'] = options;
-      }
-
-      return filteredDependencies;
+  function _assignDependencies(continuationAssignment, {mapContinuation}) {
+    if (!mapContinuation) {
+      return continuationAssignment;
     }
 
-    function _assignDependencies(continuationAssignment, {mapContinuation}) {
-      if (!mapContinuation) {
-        return continuationAssignment;
-      }
+    const assignDependencies = {};
 
-      const assignDependencies = {};
+    for (const [from, to] of Object.entries(mapContinuation)) {
+      assignDependencies[to] = continuationAssignment[from] ?? null;
+    }
 
-      for (const [from, to] of Object.entries(mapContinuation)) {
-        assignDependencies[to] = continuationAssignment[from] ?? null;
-      }
+    return assignDependencies;
+  }
 
-      return assignDependencies;
-    }
+  function _prepareContinuation(callingTransformForThisStep) {
+    const continuationStorage = {
+      returnedWith: null,
+      providedDependencies: undefined,
+      providedValue: undefined,
+    };
 
-    function _prepareContinuation(callingTransformForThisStep) {
-      const continuationStorage = {
-        returnedWith: null,
-        providedDependencies: undefined,
-        providedValue: undefined,
-      };
+    const continuation =
+      (callingTransformForThisStep
+        ? (providedValue, providedDependencies = null) => {
+            continuationStorage.returnedWith = 'continuation';
+            continuationStorage.providedDependencies = providedDependencies;
+            continuationStorage.providedValue = providedValue;
+            return continuationSymbol;
+          }
+        : (providedDependencies = null) => {
+            continuationStorage.returnedWith = 'continuation';
+            continuationStorage.providedDependencies = providedDependencies;
+            return continuationSymbol;
+          });
+
+    continuation.exit = (providedValue) => {
+      continuationStorage.returnedWith = 'exit';
+      continuationStorage.providedValue = providedValue;
+      return continuationSymbol;
+    };
 
-      const continuation =
+    if (baseComposes) {
+      const makeRaiseLike = returnWith =>
         (callingTransformForThisStep
           ? (providedValue, providedDependencies = null) => {
-              continuationStorage.returnedWith = 'continuation';
+              continuationStorage.returnedWith = returnWith;
               continuationStorage.providedDependencies = providedDependencies;
               continuationStorage.providedValue = providedValue;
               return continuationSymbol;
             }
           : (providedDependencies = null) => {
-              continuationStorage.returnedWith = 'continuation';
+              continuationStorage.returnedWith = returnWith;
               continuationStorage.providedDependencies = providedDependencies;
               return continuationSymbol;
             });
 
-      continuation.exit = (providedValue) => {
-        continuationStorage.returnedWith = 'exit';
-        continuationStorage.providedValue = providedValue;
-        return continuationSymbol;
-      };
-
-      if (baseComposes) {
-        const makeRaiseLike = returnWith =>
-          (callingTransformForThisStep
-            ? (providedValue, providedDependencies = null) => {
-                continuationStorage.returnedWith = returnWith;
-                continuationStorage.providedDependencies = providedDependencies;
-                continuationStorage.providedValue = providedValue;
-                return continuationSymbol;
-              }
-            : (providedDependencies = null) => {
-                continuationStorage.returnedWith = returnWith;
-                continuationStorage.providedDependencies = providedDependencies;
-                return continuationSymbol;
-              });
-
-        continuation.raise = makeRaiseLike('raise');
-        continuation.raiseAbove = makeRaiseLike('raiseAbove');
-      }
-
-      return {continuation, continuationStorage};
+      continuation.raise = makeRaiseLike('raise');
+      continuation.raiseAbove = makeRaiseLike('raiseAbove');
     }
 
-    function _computeOrTransform(initialValue, initialDependencies, continuationIfApplicable) {
-      const expectingTransform = initialValue !== noTransformSymbol;
+    return {continuation, continuationStorage};
+  }
 
-      let valueSoFar =
-        (expectingTransform
-          ? initialValue
-          : undefined);
+  const continuationSymbol = Symbol('continuation symbol');
+  const noTransformSymbol = Symbol('no-transform symbol');
 
-      const availableDependencies = {...initialDependencies};
+  function _computeOrTransform(initialValue, initialDependencies, continuationIfApplicable) {
+    const expectingTransform = initialValue !== noTransformSymbol;
 
-      if (expectingTransform) {
-        debug(() => [color.bright(`begin composition - transforming from:`), initialValue]);
-      } else {
-        debug(() => color.bright(`begin composition - not transforming`));
-      }
+    let valueSoFar =
+      (expectingTransform
+        ? initialValue
+        : undefined);
 
-      for (let i = 0; i < steps.length; i++) {
-        const step = steps[i];
-        const isBase = i === steps.length - 1;
+    const availableDependencies = {...initialDependencies};
 
-        debug(() => [
-          `step #${i+1}` +
-          (isBase
-            ? ` (base):`
-            : ` of ${steps.length}:`),
-          step]);
+    if (expectingTransform) {
+      debug(() => [color.bright(`begin composition - transforming from:`), initialValue]);
+    } else {
+      debug(() => color.bright(`begin composition - not transforming`));
+    }
 
-        const expose =
-          (step.flags
-            ? step.expose
-            : step);
+    for (let i = 0; i < steps.length; i++) {
+      const step = steps[i];
+      const isBase = i === steps.length - 1;
 
-        const callingTransformForThisStep =
-          expectingTransform && expose.transform;
+      debug(() => [
+        `step #${i+1}` +
+        (isBase
+          ? ` (base):`
+          : ` of ${steps.length}:`),
+        step]);
 
-        const filteredDependencies = _filterDependencies(availableDependencies, expose);
-        const {continuation, continuationStorage} = _prepareContinuation(callingTransformForThisStep);
+      const expose =
+        (step.flags
+          ? step.expose
+          : step);
 
-        debug(() => [
-          `step #${i+1} - ${callingTransformForThisStep ? 'transform' : 'compute'}`,
-          `with dependencies:`, filteredDependencies]);
+      const callingTransformForThisStep =
+        expectingTransform && expose.transform;
 
-        const result =
-          (callingTransformForThisStep
-            ? expose.transform(valueSoFar, filteredDependencies, continuation)
-            : expose.compute(filteredDependencies, continuation));
+      const filteredDependencies = _filterDependencies(availableDependencies, expose);
+      const {continuation, continuationStorage} = _prepareContinuation(callingTransformForThisStep);
 
-        if (result !== continuationSymbol) {
-          debug(() => [`step #${i+1} - result: exit (inferred) ->`, result]);
+      debug(() => [
+        `step #${i+1} - ${callingTransformForThisStep ? 'transform' : 'compute'}`,
+        `with dependencies:`, filteredDependencies]);
 
-          if (baseComposes) {
-            throw new TypeError(`Inferred early-exit is disallowed in nested compositions`);
-          }
+      const result =
+        (callingTransformForThisStep
+          ? expose.transform(valueSoFar, filteredDependencies, continuation)
+          : expose.compute(filteredDependencies, continuation));
 
-          debug(() => color.bright(`end composition - exit (inferred)`));
+      if (result !== continuationSymbol) {
+        debug(() => [`step #${i+1} - result: exit (inferred) ->`, result]);
 
-          return result;
+        if (baseComposes) {
+          throw new TypeError(`Inferred early-exit is disallowed in nested compositions`);
         }
 
-        const {returnedWith} = continuationStorage;
+        debug(() => color.bright(`end composition - exit (inferred)`));
+
+        return result;
+      }
 
-        if (returnedWith === 'exit') {
-          const {providedValue} = continuationStorage;
+      const {returnedWith} = continuationStorage;
 
-          debug(() => [`step #${i+1} - result: exit (explicit) ->`, providedValue]);
-          debug(() => color.bright(`end composition - exit (explicit)`));
+      if (returnedWith === 'exit') {
+        const {providedValue} = continuationStorage;
 
-          if (baseComposes) {
-            return continuationIfApplicable.exit(providedValue);
-          } else {
-            return providedValue;
-          }
+        debug(() => [`step #${i+1} - result: exit (explicit) ->`, providedValue]);
+        debug(() => color.bright(`end composition - exit (explicit)`));
+
+        if (baseComposes) {
+          return continuationIfApplicable.exit(providedValue);
+        } else {
+          return providedValue;
         }
+      }
 
-        const {providedValue, providedDependencies} = continuationStorage;
-
-        const continuingWithValue =
-          (expectingTransform
-            ? (callingTransformForThisStep
-                ? providedValue ?? null
-                : valueSoFar ?? null)
-            : undefined);
-
-        const continuingWithDependencies =
-          (providedDependencies
-            ? _assignDependencies(providedDependencies, expose)
-            : null);
-
-        const continuationArgs = [];
-        if (continuingWithValue !== undefined) continuationArgs.push(continuingWithValue);
-        if (continuingWithDependencies !== null) continuationArgs.push(continuingWithDependencies);
-
-        debug(() => {
-          const base = `step #${i+1} - result: ` + returnedWith;
-          const parts = [];
-
-          if (callingTransformForThisStep) {
-            if (continuingWithValue === undefined) {
-              parts.push(`(no value)`);
-            } else {
-              parts.push(`value:`, providedValue);
-            }
-          }
+      const {providedValue, providedDependencies} = continuationStorage;
 
-          if (continuingWithDependencies !== null) {
-            parts.push(`deps:`, continuingWithDependencies);
-          } else {
-            parts.push(`(no deps)`);
-          }
+      const continuingWithValue =
+        (expectingTransform
+          ? (callingTransformForThisStep
+              ? providedValue ?? null
+              : valueSoFar ?? null)
+          : undefined);
+
+      const continuingWithDependencies =
+        (providedDependencies
+          ? _assignDependencies(providedDependencies, expose)
+          : null);
 
-          if (empty(parts)) {
-            return base;
+      const continuationArgs = [];
+      if (continuingWithValue !== undefined) continuationArgs.push(continuingWithValue);
+      if (continuingWithDependencies !== null) continuationArgs.push(continuingWithDependencies);
+
+      debug(() => {
+        const base = `step #${i+1} - result: ` + returnedWith;
+        const parts = [];
+
+        if (callingTransformForThisStep) {
+          if (continuingWithValue === undefined) {
+            parts.push(`(no value)`);
           } else {
-            return [base + ' ->', ...parts];
+            parts.push(`value:`, providedValue);
           }
-        });
+        }
 
-        switch (returnedWith) {
-          case 'raise':
-            debug(() =>
-              (isBase
-                ? color.bright(`end composition - raise (base: explicit)`)
-                : color.bright(`end composition - raise`)));
-            return continuationIfApplicable(...continuationArgs);
+        if (continuingWithDependencies !== null) {
+          parts.push(`deps:`, continuingWithDependencies);
+        } else {
+          parts.push(`(no deps)`);
+        }
 
-          case 'raiseAbove':
-            debug(() => color.bright(`end composition - raiseAbove`));
-            return continuationIfApplicable.raise(...continuationArgs);
-
-          case 'continuation':
-            if (isBase) {
-              debug(() => color.bright(`end composition - raise (inferred)`));
-              return continuationIfApplicable(...continuationArgs);
-            } else {
-              Object.assign(availableDependencies, continuingWithDependencies);
-              break;
-            }
+        if (empty(parts)) {
+          return base;
+        } else {
+          return [base + ' ->', ...parts];
         }
+      });
+
+      switch (returnedWith) {
+        case 'raise':
+          debug(() =>
+            (isBase
+              ? color.bright(`end composition - raise (base: explicit)`)
+              : color.bright(`end composition - raise`)));
+          return continuationIfApplicable(...continuationArgs);
+
+        case 'raiseAbove':
+          debug(() => color.bright(`end composition - raiseAbove`));
+          return continuationIfApplicable.raise(...continuationArgs);
+
+        case 'continuation':
+          if (isBase) {
+            debug(() => color.bright(`end composition - raise (inferred)`));
+            return continuationIfApplicable(...continuationArgs);
+          } else {
+            Object.assign(availableDependencies, continuingWithDependencies);
+            break;
+          }
       }
     }
+  }
+
+  const constructedDescriptor = {};
+
+  if (annotation) {
+    constructedDescriptor.annotation = annotation;
+  }
+
+  constructedDescriptor.flags = {
+    update: baseUpdates,
+    expose: baseExposes,
+    compose: baseComposes,
+  };
+
+  if (baseUpdates) {
+    constructedDescriptor.update = base.update;
+  }
+
+  if (baseExposes) {
+    const expose = constructedDescriptor.expose = {};
+    expose.dependencies = Array.from(exposeDependencies);
 
     const transformFn =
       (value, initialDependencies, continuationIfApplicable) =>