« get me outta code hell

data: withReverseContributionList: mode: contributions - 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>2024-03-06 14:28:07 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-06-12 17:26:36 -0300
commit4dd54f8b38a38a47566a0006b1dba3065158fc7e (patch)
tree6c5e3aab917fa80d382a079a5c642f806a4f9c0a
parent0630befa57356b8da8b54eda97ab7c6bb68ee8a1 (diff)
data: withReverseContributionList: mode: contributions
-rw-r--r--src/data/composite/wiki-data/withReverseContributionList.js47
1 files changed, 35 insertions, 12 deletions
diff --git a/src/data/composite/wiki-data/withReverseContributionList.js b/src/data/composite/wiki-data/withReverseContributionList.js
index 91e125e4..99d0c73b 100644
--- a/src/data/composite/wiki-data/withReverseContributionList.js
+++ b/src/data/composite/wiki-data/withReverseContributionList.js
@@ -1,6 +1,9 @@
 // Analogous implementation for withReverseReferenceList, for contributions.
-// This is all duplicate code and both should be ported to the same underlying
-// data form later on.
+// This is mostly duplicate code and both should be ported to the same
+// underlying data form later on. Unique to contributions, the 'mode' option
+// controls whether the things themselves, for which the artist is credited,
+// are exposed (the default), or the actual contribution objects representing
+// the relationship itself, instead.
 //
 // This implementation uses a global cache (via WeakMap) to attempt to speed
 // up subsequent similar accesses.
@@ -10,6 +13,7 @@
 // is used, a fresh cache will always be created.
 
 import {input, templateCompositeFrom} from '#composite';
+import {is} from '#validators';
 
 import {exitWithoutDependency, raiseOutputWithoutDependency}
   from '#composite/control-flow';
@@ -28,6 +32,11 @@ export default templateCompositeFrom({
   inputs: {
     data: inputWikiData({allowMixedTypes: false}),
     list: input({type: 'string'}),
+
+    mode: input({
+      validate: is('things', 'contributions'),
+      defaultValue: 'things',
+    }),
   },
 
   outputs: ['#reverseContributionList'],
@@ -64,16 +73,15 @@ export default templateCompositeFrom({
           const cacheRecord = new WeakMap();
 
           for (const referencingThing of data) {
-            const referenceList = referencingThing[list];
-
-            // Destructuring {artist} is the only unique part of the
-            // withReverseContributionList implementation, compared to
-            // withReverseReferneceList.
-            for (const {artist: referencedThing} of referenceList) {
-              if (cacheRecord.has(referencedThing)) {
-                cacheRecord.get(referencedThing).push(referencingThing);
+            const contributionList = referencingThing[list];
+
+            for (const contribution of contributionList) {
+              const {artist} = contribution;
+
+              if (cacheRecord.has(artist)) {
+                cacheRecord.get(artist).push(contribution);
               } else {
-                cacheRecord.set(referencedThing, [referencingThing]);
+                cacheRecord.set(artist, [contribution]);
               }
             }
           }
@@ -82,10 +90,25 @@ export default templateCompositeFrom({
         }
 
         return continuation({
-          ['#reverseContributionList']:
+          ['#contributions']:
             cache.get(data).get(myself) ?? [],
         });
       },
     },
+
+    {
+      dependencies: ['#contributions', input('mode')],
+      compute: (continuation, {
+        ['#contributions']: contributions,
+        [input('mode')]: mode,
+      }) => continuation({
+        ['#reverseContributionList']:
+          (mode === 'contributions'
+            ? contributions
+         : mode === 'things'
+            ? contributions.map(contrib => contrib.thing)
+            : []),
+      }),
+    },
   ],
 });