« get me outta code hell

withAllReleases.js « track « things « composite « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite/things/track/withAllReleases.js
blob: bd54384f0b6af5fc250348490b7edc437560cf09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Gets all releases of the current track. All items of the outputs are
// distinct Track objects; one track is the main release; all else are
// secondary releases of that main release; and one item, which may be
// the main release or one of the secondary releases, is the current
// track. The results are sorted by date, and it is possible that the
// main release is not actually the earliest/first.

import {input, templateCompositeFrom} from '#composite';
import {sortByDate} from '#sort';

import {withPropertyFromObject} from '#composite/data';

import withMainReleaseTrack from './withMainReleaseTrack.js';

export default templateCompositeFrom({
  annotation: `withAllReleases`,

  outputs: ['#allReleases'],

  steps: () => [
    withMainReleaseTrack({
      selfIfMain: input.value(true),
      notFoundValue: input.value([]),
    }),

    // We don't talk about bruno no no
    // Yes, this can perform a normal access equivalent to
    // `this.secondaryReleases` from within a data composition.
    // Oooooooooooooooooooooooooooooooooooooooooooooooo
    withPropertyFromObject({
      object: '#mainReleaseTrack',
      property: input.value('secondaryReleases'),
    }),

    {
      dependencies: [
        '#mainReleaseTrack',
        '#mainReleaseTrack.secondaryReleases',
      ],

      compute: (continuation, {
        ['#mainReleaseTrack']: mainReleaseTrack,
        ['#mainReleaseTrack.secondaryReleases']: secondaryReleases,
      }) => continuation({
        ['#allReleases']:
          sortByDate([mainReleaseTrack, ...secondaryReleases]),
      }),
    },
  ],
});