« get me outta code hell

data: update Album.trackSections - 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-08 12:35:22 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-09-08 12:38:26 -0300
commit4ed5649e83e344615eb0e710c7a942d0dea8fa22 (patch)
tree12ba89874da62d91c0f98f388892d49880fcaebf /src/data/things
parentf4305e5ab0a64a648f39c647b817a4ba09848f11 (diff)
data: update Album.trackSections
Diffstat (limited to 'src/data/things')
-rw-r--r--src/data/things/album.js121
1 files changed, 84 insertions, 37 deletions
diff --git a/src/data/things/album.js b/src/data/things/album.js
index 76e0f638..01f52c2d 100644
--- a/src/data/things/album.js
+++ b/src/data/things/album.js
@@ -1,12 +1,15 @@
 import find from '#find';
-import {empty} from '#sugar';
+import {stitchArrays} from '#sugar';
 import {isDate, isDimensions, isTrackSectionList} from '#validators';
 
 import {
   compositeFrom,
   exitWithoutDependency,
+  exitWithoutUpdateValue,
   exposeDependency,
   exposeUpdateValueOrContinue,
+  withFlattenedArray,
+  withUnflattenedArray,
 } from '#composite';
 
 import Thing, {
@@ -73,40 +76,87 @@ export class Album extends Thing {
       data: 'artTagData',
     }),
 
-    trackSections: {
-      flags: {update: true, expose: true},
+    trackSections: compositeFrom(`Album.trackSections`, [
+      exitWithoutDependency({dependency: 'trackData', value: []}),
+      exitWithoutUpdateValue({value: [], mode: 'empty'}),
 
-      update: {
-        validate: isTrackSectionList,
+      {
+        transform: (trackSections, continuation) =>
+          continuation(trackSections, {
+            '#sectionTrackRefs':
+              trackSections.map(section => section.tracks),
+
+            '#sectionDateOriginallyReleased':
+              trackSections
+                .map(({dateOriginallyReleased}) => dateOriginallyReleased ?? null),
+
+            '#sectionIsDefaultTrackSection':
+              trackSections
+                .map(({isDefaultTrackSection}) => isDefaultTrackSection ?? false),
+          }),
       },
 
-      expose: {
-        dependencies: ['color', 'trackData'],
-        transform(trackSections, {
-          color: albumColor,
-          trackData,
-        }) {
-          let startIndex = 0;
-          return trackSections?.map(section => ({
-            name: section.name ?? null,
-            color: section.color ?? albumColor ?? null,
-            dateOriginallyReleased: section.dateOriginallyReleased ?? null,
-            isDefaultTrackSection: section.isDefaultTrackSection ?? false,
-
-            startIndex: (
-              startIndex += section.tracks.length,
-              startIndex - section.tracks.length
-            ),
-
-            tracks:
-              (trackData && section.tracks
-                ?.map(ref => find.track(ref, trackData, {mode: 'quiet'}))
-                .filter(Boolean)) ??
-              [],
-          }));
+      {
+        dependencies: ['color'],
+        transform: (trackSections, {color: albumColor}, continuation) =>
+          continuation(trackSections, {
+            '#sectionColor':
+              trackSections
+                .map(({color: sectionColor}) => sectionColor ?? albumColor),
+          }),
+      },
+
+      withFlattenedArray({
+        from: '#sectionTrackRefs',
+        into: '#trackRefs',
+        intoIndices: '#sectionStartIndex',
+      }),
+
+      withResolvedReferenceList({
+        list: '#trackRefs',
+        data: 'trackData',
+        mode: 'null',
+        find: find.track,
+        into: '#tracks',
+      }),
+
+      withUnflattenedArray({
+        from: '#tracks',
+        fromIndices: '#sectionStartIndex',
+        into: '#sectionTracks',
+      }),
+
+      {
+        flags: {update: true, expose: true},
+
+        update: {validate: isTrackSectionList},
+
+        expose: {
+          dependencies: [
+            '#sectionTracks',
+            '#sectionColor',
+            '#sectionDateOriginallyReleased',
+            '#sectionIsDefaultTrackSection',
+            '#sectionStartIndex',
+          ],
+
+          transform: (trackSections, {
+            '#sectionTracks': tracks,
+            '#sectionColor': color,
+            '#sectionDateOriginallyReleased': dateOriginallyReleased,
+            '#sectionIsDefaultTrackSection': isDefaultTrackSection,
+            '#sectionStartIndex': startIndex,
+          }) =>
+            stitchArrays({
+              tracks,
+              color,
+              dateOriginallyReleased,
+              isDefaultTrackSection,
+              startIndex,
+            }),
         },
       },
-    },
+    ]),
 
     coverArtFileExtension: compositeFrom(`Album.coverArtFileExtension`, [
       withResolvedContribs({from: 'coverArtistContribs'}),
@@ -149,15 +199,12 @@ export class Album extends Thing {
     hasBannerArt: contribsPresent('bannerArtistContribs'),
 
     tracks: compositeFrom(`Album.tracks`, [
-      exitWithoutDependency({
-        dependency: 'trackSections',
-        mode: 'empty',
-        value: [],
-      }),
+      exitWithoutDependency({dependency: 'trackData', value: []}),
+      exitWithoutDependency({dependency: 'trackSections', mode: 'empty', value: []}),
 
       {
-        dependencies: ['trackSections', 'trackData'],
-        compute: ({trackSections, trackData}, continuation) =>
+        dependencies: ['trackSections'],
+        compute: ({trackSections}, continuation) =>
           continuation({
             '#trackRefs': trackSections
               .flatMap(section => section.tracks ?? []),