« get me outta code hell

withMainRelease.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/withMainRelease.js
blob: b1f427eb718474057046ba16df91bdbf2cb1bccc (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Resolves this track's `mainRelease` reference, using weird-ass atypical
// machinery that operates on soupyFind and does not operate on findMixed,
// let alone a prim and proper standalone find spec.
//
// Raises null only if there is no `mainRelease` reference provided at all.
// This will early exit (with notFoundValue) if the reference doesn't resolve.
//

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

import {raiseOutputWithoutDependency} from '#composite/control-flow';
import {withPropertyFromObject} from '#composite/data';
import {withResolvedReference} from '#composite/wiki-data';
import {soupyFind} from '#composite/wiki-properties';

export default templateCompositeFrom({
  annotation: `withMainRelease`,

  inputs: {
    from: input({
      defaultDependency: 'mainRelease',
      acceptsNull: true,
    }),

    notFoundValue: input({defaultValue: null}),
  },

  outputs: ['#mainRelease'],

  steps: () => [
    raiseOutputWithoutDependency({
      dependency: input('from'),
      output: input.value({'#mainRelease': null}),
    }),

    withResolvedReference({
      ref: input('from'),
      find: soupyFind.input('trackMainReleasesOnly'),
    }).outputs({
      '#resolvedReference': '#matchingTrack',
    }),

    withResolvedReference({
      ref: input('from'),
      find: soupyFind.input('album'),
    }).outputs({
      '#resolvedReference': '#matchingAlbum',
    }),

    {
      dependencies: [
        '#matchingTrack',
        '#matchingAlbum',
        input('notFoundValue'),
      ],

      compute: (continuation, {
        ['#matchingTrack']: matchingTrack,
        ['#matchingAlbum']: matchingAlbum,
        [input('notFoundValue')]: notFoundValue,
      }) =>
        (matchingTrack && matchingAlbum
          ? continuation()
       : matchingTrack ?? matchingAlbum
          ? continuation.raiseOutput({
              ['#mainRelease']:
                matchingTrack ?? matchingAlbum,
            })
          : continuation.exit(notFoundValue)),
    },

    withPropertyFromObject({
      object: '#matchingAlbum',
      property: input.value('tracks'),
    }),

    {
      dependencies: [
        '#matchingAlbum.tracks',
        '#matchingTrack',
        input('notFoundValue'),
      ],

      compute: (continuation, {
        ['#matchingAlbum.tracks']: matchingAlbumTracks,
        ['#matchingTrack']: matchingTrack,
        [input('notFoundValue')]: notFoundValue,
      }) =>
        (matchingAlbumTracks.includes(matchingTrack)
          ? continuation.raiseOutput({'#mainRelease': matchingTrack})
          : continuation.exit(notFoundValue)),
    },
  ],
});