« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--package.json1
-rw-r--r--src/data/composite/things/flash/index.js1
-rw-r--r--src/data/composite/things/flash/withFlashAct.js108
-rw-r--r--src/data/composite/things/track/withAlbum.js2
4 files changed, 112 insertions, 0 deletions
diff --git a/package.json b/package.json
index 16261f9..194c406 100644
--- a/package.json
+++ b/package.json
@@ -19,6 +19,7 @@
         "#composite/wiki-data": "./src/data/composite/wiki-data/index.js",
         "#composite/wiki-properties": "./src/data/composite/wiki-properties/index.js",
         "#composite/things/album": "./src/data/composite/things/album/index.js",
+        "#composite/things/flash": "./src/data/composite/things/flash/index.js",
         "#composite/things/track": "./src/data/composite/things/track/index.js",
         "#content-dependencies": "./src/content/dependencies/index.js",
         "#content-function": "./src/content-function.js",
diff --git a/src/data/composite/things/flash/index.js b/src/data/composite/things/flash/index.js
new file mode 100644
index 0000000..63ac13d
--- /dev/null
+++ b/src/data/composite/things/flash/index.js
@@ -0,0 +1 @@
+export {default as withFlashAct} from './withFlashAct.js';
diff --git a/src/data/composite/things/flash/withFlashAct.js b/src/data/composite/things/flash/withFlashAct.js
new file mode 100644
index 0000000..ada2dcf
--- /dev/null
+++ b/src/data/composite/things/flash/withFlashAct.js
@@ -0,0 +1,108 @@
+// Gets the flash's act. This will early exit if flashActData is missing.
+// By default, if there's no flash whose list of flashes includes this flash,
+// the output dependency will be null; set {notFoundMode: 'exit'} to early
+// exit instead.
+//
+// This step models with Flash.withAlbum.
+
+import {input, templateCompositeFrom} from '#composite';
+import {is} from '#validators';
+
+import {exitWithoutDependency, withResultOfAvailabilityCheck}
+  from '#composite/control-flow';
+import {withPropertyFromList} from '#composite/data';
+
+export default templateCompositeFrom({
+  annotation: `withFlashAct`,
+
+  inputs: {
+    notFoundMode: input({
+      validate: is('exit', 'null'),
+      defaultValue: 'null',
+    }),
+  },
+
+  outputs: ['#flashAct'],
+
+  steps: () => [
+    // null flashActData is always an early exit.
+
+    exitWithoutDependency({
+      dependency: 'flashActData',
+      mode: input.value('null'),
+    }),
+
+    // empty flashActData conditionally exits early or outputs null.
+
+    withResultOfAvailabilityCheck({
+      from: 'flashActData',
+      mode: input.value('empty'),
+    }).outputs({
+      '#availability': '#flashActDataAvailability',
+    }),
+
+    {
+      dependencies: [input('notFoundMode'), '#flashActDataAvailability'],
+      compute(continuation, {
+        [input('notFoundMode')]: notFoundMode,
+        ['#flashActDataAvailability']: flashActDataIsAvailable,
+      }) {
+        if (flashActDataIsAvailable) return continuation();
+        switch (notFoundMode) {
+          case 'exit': return continuation.exit(null);
+          case 'null': return continuation.raiseOutput({'#flashAct': null});
+        }
+      },
+    },
+
+    withPropertyFromList({
+      list: 'flashActData',
+      property: input.value('flashes'),
+    }),
+
+    {
+      dependencies: [input.myself(), '#flashActData.flashes'],
+      compute: (continuation, {
+        [input.myself()]: track,
+        ['#flashActData.flashes']: flashLists,
+      }) => continuation({
+        ['#flashActIndex']:
+          flashLists.findIndex(flashes => flashes.includes(track)),
+      }),
+    },
+
+    // album not found conditionally exits or outputs null.
+
+    withResultOfAvailabilityCheck({
+      from: '#flashActIndex',
+      mode: input.value('index'),
+    }).outputs({
+      '#availability': '#flashActAvailability',
+    }),
+
+    {
+      dependencies: [input('notFoundMode'), '#flashActAvailability'],
+      compute(continuation, {
+        [input('notFoundMode')]: notFoundMode,
+        ['#flashActAvailability']: flashActIsAvailable,
+      }) {
+        if (flashActIsAvailable) return continuation();
+        switch (notFoundMode) {
+          case 'exit': return continuation.exit(null);
+          case 'null': return continuation.raiseOutput({'#flashAct': null});
+        }
+      },
+    },
+
+    {
+      dependencies: ['flashActData', '#flashActIndex'],
+      compute: (continuation, {
+        ['flashActData']: flashActData,
+        ['#flashActIndex']: flashActIndex,
+      }) => continuation.raiseOutput({
+        ['#flashAct']:
+          flashActData[flashActIndex],
+      }),
+    },
+  ],
+});
diff --git a/src/data/composite/things/track/withAlbum.js b/src/data/composite/things/track/withAlbum.js
index 9c974cd..cbd16dc 100644
--- a/src/data/composite/things/track/withAlbum.js
+++ b/src/data/composite/things/track/withAlbum.js
@@ -2,6 +2,8 @@
 // By default, if there's no album whose list of tracks includes this track,
 // the output dependency will be null; set {notFoundMode: 'exit'} to early
 // exit instead.
+//
+// This step models with Flash.withFlashAct.
 
 import {input, templateCompositeFrom} from '#composite';
 import {is} from '#validators';