« get me outta code hell

data: update Track.otherReleases implementation - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/track.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-08-30 16:31:54 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-09-05 21:02:54 -0300
commitb38a3c787dc4fbec5e2dc0c297bbcd3ceae83349 (patch)
tree5dbb0a349f0a877ba0c7b52b44e3e53d51b6a10a /src/data/things/track.js
parent011c197aeedab56d501b03b800433dd0cd9bc4f7 (diff)
data: update Track.otherReleases implementation
Also adds {selfIfOriginal} option to withOriginalRelease().
Diffstat (limited to 'src/data/things/track.js')
-rw-r--r--src/data/things/track.js81
1 files changed, 49 insertions, 32 deletions
diff --git a/src/data/things/track.js b/src/data/things/track.js
index cdc9cec3..3b2029e8 100644
--- a/src/data/things/track.js
+++ b/src/data/things/track.js
@@ -198,33 +198,30 @@ export class Track extends Thing {
       find.track
     ),
 
-    otherReleases: {
-      flags: {expose: true},
+    otherReleases:
+      Thing.composite.from(`Track.otherReleases`, [
+        Thing.composite.earlyExitWithoutDependency('trackData'),
+        Track.composite.withOriginalRelease({selfIfOriginal: true}),
 
-      expose: {
-        dependencies: ['this', 'originalReleaseTrackByRef', 'trackData'],
-
-        compute: ({
-          this: t1,
-          originalReleaseTrackByRef: t1origRef,
-          trackData,
-        }) => {
-          if (!trackData) {
-            return [];
-          }
-
-          const t1orig = find.track(t1origRef, trackData);
-
-          return [
-            t1orig,
-            ...trackData.filter((t2) => {
-              const {originalReleaseTrack: t2orig} = t2;
-              return t2 !== t1 && t2orig && (t2orig === t1orig || t2orig === t1);
-            }),
-          ].filter(Boolean);
+        {
+          flags: {expose: true},
+          expose: {
+            dependencies: ['this', 'trackData', '#originalRelease'],
+            compute: ({
+              this: thisTrack,
+              trackData,
+              '#originalRelease': originalRelease,
+            }) =>
+              (originalRelease === thisTrack
+                ? []
+                : [originalRelease])
+                .concat(trackData.filter(track =>
+                  track !== originalRelease &&
+                  track !== thisTrack &&
+                  track.originalReleaseTrack === originalRelease)),
+          },
         },
-      },
-    },
+      ]),
 
     artistContribs: Thing.composite.from(`Track.artistContribs`, [
       Track.composite.inheritFromOriginalRelease({property: 'artistContribs'}),
@@ -545,12 +542,15 @@ export class Track extends Thing {
       ]);
     },
 
-    // Just includes the original release of this track as a dependency, or
-    // null, if it's not a rerelease. Note that this will early exit if the
-    // original release is specified by reference and that reference doesn't
-    // resolve to anything. Outputs to '#originalRelease' by default.
+    // Just includes the original release of this track as a dependency.
+    // If this track isn't a rerelease, then it'll provide null, unless the
+    // {selfIfOriginal} option is set, in which case it'll provide this track
+    // itself. Note that this will early exit if the original release is
+    // specified by reference and that reference doesn't resolve to anything.
+    // Outputs to '#originalRelease' by default.
     withOriginalRelease({
       to = '#originalRelease',
+      selfIfOriginal = false,
     } = {}) {
       return Thing.composite.from(`Track.composite.withOriginalRelease`, [
         Thing.composite.withResolvedReference({
@@ -561,9 +561,26 @@ export class Track extends Thing {
           earlyExitIfNotFound: true,
         }),
 
-        Thing.composite.export({
-          [outputDependency]: '#originalRelease',
-        }),
+        {
+          flags: {expose: true, compose: true},
+          expose: {
+            dependencies: ['this', '#originalRelease'],
+            options: {selfIfOriginal},
+            mapContinuation: {to},
+            compute: ({
+              this: track,
+              '#originalRelease': originalRelease,
+              '#options': {selfIfOriginal},
+            }, continuation) =>
+              continuation.raise({
+                to:
+                  (originalRelease ??
+                    (selfIfOriginal
+                      ? track
+                      : null)),
+              }),
+          },
+        },
       ]);
     },