« get me outta code hell

data: withUniqueReferencingThing - 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>2024-04-03 13:16:16 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-04-03 13:44:00 -0300
commit0792908781a1956cd44038498bbd68f2bfdb8396 (patch)
treed9a9a62a4e3f81f8b6436eb4ef31b873a9df88e5 /src
parent01fca1864f58067ec95590700b5dead24fd2dc73 (diff)
data: withUniqueReferencingThing
Diffstat (limited to 'src')
-rw-r--r--src/data/composite/things/flash/withFlashAct.js48
-rw-r--r--src/data/composite/things/track/withAlbum.js48
-rw-r--r--src/data/composite/wiki-data/index.js1
-rw-r--r--src/data/composite/wiki-data/withUniqueReferencingThing.js52
4 files changed, 65 insertions, 84 deletions
diff --git a/src/data/composite/things/flash/withFlashAct.js b/src/data/composite/things/flash/withFlashAct.js
index 2c985fe..652b8bf 100644
--- a/src/data/composite/things/flash/withFlashAct.js
+++ b/src/data/composite/things/flash/withFlashAct.js
@@ -1,15 +1,10 @@
 // Gets the flash's act. This will early exit if flashActData is missing.
 // If there's no flash whose list of flashes includes this flash, the output
 // dependency will be null.
-//
-// This step models with Flash.withAlbum.
 
 import {input, templateCompositeFrom} from '#composite';
-import {is} from '#validators';
 
-import {exitWithoutDependency, raiseOutputWithoutDependency}
-  from '#composite/control-flow';
-import {withPropertyFromList} from '#composite/data';
+import {withUniqueReferencingThing} from '#composite/wiki-data';
 
 export default templateCompositeFrom({
   annotation: `withFlashAct`,
@@ -17,42 +12,11 @@ export default templateCompositeFrom({
   outputs: ['#flashAct'],
 
   steps: () => [
-    exitWithoutDependency({
-      dependency: 'flashActData',
-      mode: input.value('null'),
+    withUniqueReferencingThing({
+      data: 'flashActData',
+      list: input.value('flashes'),
+    }).outputs({
+      ['#uniqueReferencingThing']: '#flashAct',
     }),
-
-    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)),
-      }),
-    },
-
-    raiseOutputWithoutDependency({
-      dependency: '#flashActIndex',
-      mode: input.value('index'),
-      output: input.value({'#album': 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 143e097..03b840d 100644
--- a/src/data/composite/things/track/withAlbum.js
+++ b/src/data/composite/things/track/withAlbum.js
@@ -1,15 +1,10 @@
 // Gets the track's album. This will early exit if albumData is missing.
 // If there's no album whose list of tracks includes this track, the output
 // dependency will be null.
-//
-// This step models with Flash.withFlashAct.
 
 import {input, templateCompositeFrom} from '#composite';
-import {is} from '#validators';
 
-import {exitWithoutDependency, raiseOutputWithoutDependency}
-  from '#composite/control-flow';
-import {withPropertyFromList} from '#composite/data';
+import {withUniqueReferencingThing} from '#composite/wiki-data';
 
 export default templateCompositeFrom({
   annotation: `withAlbum`,
@@ -17,42 +12,11 @@ export default templateCompositeFrom({
   outputs: ['#album'],
 
   steps: () => [
-    exitWithoutDependency({
-      dependency: 'albumData',
-      mode: input.value('null'),
+    withUniqueReferencingThing({
+      data: 'albumData',
+      list: input.value('tracks'),
+    }).outputs({
+      ['#uniqueReferencingThing']: '#album',
     }),
-
-    withPropertyFromList({
-      list: 'albumData',
-      property: input.value('tracks'),
-    }),
-
-    {
-      dependencies: [input.myself(), '#albumData.tracks'],
-      compute: (continuation, {
-        [input.myself()]: track,
-        ['#albumData.tracks']: trackLists,
-      }) => continuation({
-        ['#albumIndex']:
-          trackLists.findIndex(tracks => tracks.includes(track)),
-      }),
-    },
-
-    raiseOutputWithoutDependency({
-      dependency: '#albumIndex',
-      mode: input.value('index'),
-      output: input.value({'#album': null}),
-    }),
-
-    {
-      dependencies: ['albumData', '#albumIndex'],
-      compute: (continuation, {
-        ['albumData']: albumData,
-        ['#albumIndex']: albumIndex,
-      }) => continuation.raiseOutput({
-        ['#album']:
-          albumData[albumIndex],
-      }),
-    },
   ],
 });
diff --git a/src/data/composite/wiki-data/index.js b/src/data/composite/wiki-data/index.js
index 3ccfa75..b4cf6d1 100644
--- a/src/data/composite/wiki-data/index.js
+++ b/src/data/composite/wiki-data/index.js
@@ -13,3 +13,4 @@ export {default as withResolvedReferenceList} from './withResolvedReferenceList.
 export {default as withReverseContributionList} from './withReverseContributionList.js';
 export {default as withReverseReferenceList} from './withReverseReferenceList.js';
 export {default as withThingsSortedAlphabetically} from './withThingsSortedAlphabetically.js';
+export {default as withUniqueReferencingThing} from './withUniqueReferencingThing.js';
diff --git a/src/data/composite/wiki-data/withUniqueReferencingThing.js b/src/data/composite/wiki-data/withUniqueReferencingThing.js
new file mode 100644
index 0000000..ce04f83
--- /dev/null
+++ b/src/data/composite/wiki-data/withUniqueReferencingThing.js
@@ -0,0 +1,52 @@
+// Like withReverseReferenceList, but this is specifically for special "unique"
+// references, meaning this thing is referenced by exactly one or zero things
+// in the data list.
+
+import {input, templateCompositeFrom} from '#composite';
+
+import {exitWithoutDependency, raiseOutputWithoutDependency}
+  from '#composite/control-flow';
+
+import inputWikiData from './inputWikiData.js';
+import withReverseReferenceList from './withReverseReferenceList.js';
+
+export default templateCompositeFrom({
+  annotation: `withUniqueReferencingThing`,
+
+  inputs: {
+    data: inputWikiData({allowMixedTypes: false}),
+    list: input({type: 'string'}),
+  },
+
+  outputs: ['#uniqueReferencingThing'],
+
+  steps: () => [
+    // withReverseRefernceList does this check too, but it early exits with
+    // an empty array. That's no good here!
+    exitWithoutDependency({
+      dependency: input('data'),
+      mode: input.value('empty'),
+    }),
+
+    withReverseReferenceList({
+      data: input('data'),
+      list: input('list'),
+    }),
+
+    raiseOutputWithoutDependency({
+      dependency: '#reverseReferenceList',
+      mode: input.value('empty'),
+      output: input.value({'#uniqueReferencingThing': null}),
+    }),
+
+    {
+      dependencies: ['#reverseReferenceList'],
+      compute: (continuation, {
+        ['#reverseReferenceList']: reverseReferenceList,
+      }) => continuation({
+        ['#uniqueReferencingThing']:
+          reverseReferenceList[0],
+      }),
+    },
+  ],
+});