« 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: cbd16dcd7392f53dace68484448104848d0c6e62 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// 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.
//
// This step models with Flash.withFlashAct.

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

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

export default templateCompositeFrom({
  annotation: `withAlbum`,

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

  outputs: ['#album'],

  steps: () => [
    // null albumData is always an early exit.

    exitWithoutDependency({
      dependency: 'albumData',
      mode: input.value('null'),
    }),

    // empty albumData conditionally exits early or outputs null.

    withResultOfAvailabilityCheck({
      from: 'albumData',
      mode: input.value('empty'),
    }).outputs({
      '#availability': '#albumDataAvailability',
    }),

    {
      dependencies: [input('notFoundMode'), '#albumDataAvailability'],
      compute(continuation, {
        [input('notFoundMode')]: notFoundMode,
        ['#albumDataAvailability']: albumDataIsAvailable,
      }) {
        if (albumDataIsAvailable) return continuation();
        switch (notFoundMode) {
          case 'exit': return continuation.exit(null);
          case 'null': return continuation.raiseOutput({'#album': null});
        }
      },
    },

    withPropertyFromList({
      list: 'albumData',
      property: input.value('tracks'),
    }),

    {
      dependencies: [input.myself(), '#albumData.tracks'],
      compute: (continuation, {
        [input.myself()]: track,
        ['#albumData.tracks']: trackLists,
      }) => continuation({
        ['#albumIndex']:
          trackLists.findIndex(tracks => tracks.includes(track)),
      }),
    },

    // album not found conditionally exits or outputs null.

    withResultOfAvailabilityCheck({
      from: '#albumIndex',
      mode: input.value('index'),
    }).outputs({
      '#availability': '#albumAvailability',
    }),

    {
      dependencies: [input('notFoundMode'), '#albumAvailability'],
      compute(continuation, {
        [input('notFoundMode')]: notFoundMode,
        ['#albumAvailability']: albumIsAvailable,
      }) {
        if (albumIsAvailable) return continuation();
        switch (notFoundMode) {
          case 'exit': return continuation.exit(null);
          case 'null': return continuation.raiseOutput({'#album': null});
        }
      },
    },

    {
      dependencies: ['albumData', '#albumIndex'],
      compute: (continuation, {
        ['albumData']: albumData,
        ['#albumIndex']: albumIndex,
      }) => continuation.raiseOutput({
        ['#album']:
          albumData[albumIndex],
      }),
    },
  ],
});