« get me outta code hell

withOriginalRelease.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/withOriginalRelease.js
blob: c7f49657085c2b983ad876d8178a610ea3e7fd8a (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
// 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. This will early exit (with notFoundValue) if the original release
// is specified by reference and that reference doesn't resolve to anything.

import {input, templateCompositeFrom} from '#composite';
import find from '#find';
import {validateWikiData} from '#validators';

import {exitWithoutDependency, withResultOfAvailabilityCheck}
  from '#composite/control-flow';
import {withResolvedReference} from '#composite/wiki-data';

export default templateCompositeFrom({
  annotation: `withOriginalRelease`,

  inputs: {
    selfIfOriginal: input({type: 'boolean', defaultValue: false}),

    data: input({
      validate: validateWikiData({referenceType: 'track'}),
      defaultDependency: 'trackData',
    }),

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

  outputs: ['#originalRelease'],

  steps: () => [
    withResultOfAvailabilityCheck({
      from: 'originalReleaseTrack',
    }),

    {
      dependencies: [
        input.myself(),
        input('selfIfOriginal'),
        '#availability',
      ],

      compute: (continuation, {
        [input.myself()]: track,
        [input('selfIfOriginal')]: selfIfOriginal,
        '#availability': availability,
      }) =>
        (availability
          ? continuation()
          : continuation.raiseOutput({
              ['#originalRelease']:
                (selfIfOriginal ? track : null),
            })),
    },

    withResolvedReference({
      ref: 'originalReleaseTrack',
      data: input('data'),
      find: input.value(find.track),
    }),

    exitWithoutDependency({
      dependency: '#resolvedReference',
      value: input('notFoundValue'),
    }),

    {
      dependencies: ['#resolvedReference'],

      compute: (continuation, {
        ['#resolvedReference']: resolvedReference,
      }) =>
        continuation({
          ['#originalRelease']: resolvedReference,
        }),
    },
  ],
});