« get me outta code hell

withAlbum.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/withAlbum.js
blob: 0e85cee9720b3868f802fab713b96a0c555a943d (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
// Gets the track's album. This will early exit if albumData is missing.
// By default, if there's no album whose list of tracks includes this track,
// the output dependency will be null; set {notFoundMode: 'exit'} to early
// exit instead.

import {input, templateCompositeFrom} from '#composite';
import {empty} from '#sugar';
import {is} from '#validators';

export default templateCompositeFrom({
  annotation: `withAlbum`,

  inputs: {
    notFoundMode: input({
      validate: is('exit', 'null'),
      defaultValue: 'null',
    }),
  },

  outputs: ['#album'],

  steps: () => [
    {
      dependencies: [input('notFoundMode'), 'albumData'],
      compute: (continuation, {
        [input('notFoundMode')]: notFoundMode,
        ['albumData']: albumData,
      }) =>
        (albumData === null
          ? continuation.exit(null)
       : empty(albumData)
          ? (notFoundMode === 'exit'
              ? continuation.exit(null)
              : continuation.raiseOutput({'#album': null}))
          : continuation()),
    },

    {
      dependencies: [input.myself(), 'albumData'],
      compute: (continuation, {
        [input.myself()]: track,
        ['albumData']: albumData,
      }) =>
        continuation({
          ['#album']:
            albumData.find(album => album.tracks.includes(track))
              ?? null,
        }),
    },

    {
      dependencies: [input('notFoundMode'), '#album'],
      compute: (continuation, {
        [input('notFoundMode')]: notFoundMode,
        ['#album']: album,
      }) =>
        ((album === null && notFoundMode === 'exit')
          ? continuation.exit(null)
          : continuation.raiseOutput({'#album': album})),
    },
  ],
});