« get me outta code hell

withFlashAct.js « flash « 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/flash/withFlashAct.js
blob: ada2dcfe2d26bbe5e38257ab628f09099e09490c (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 flash's act. This will early exit if flashActData is missing.
// By default, if there's no flash whose list of flashes includes this flash,
// the output dependency will be null; set {notFoundMode: 'exit'} to early
// exit instead.
//
// This step models with Flash.withAlbum.

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: `withFlashAct`,

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

  outputs: ['#flashAct'],

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

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

    // empty flashActData conditionally exits early or outputs null.

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

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

    withPropertyFromList({
      list: 'flashActData',
      property: input.value('flashes'),
    }),

    {
      dependencies: [input.myself(), '#flashActData.flashes'],
      compute: (continuation, {
        [input.myself()]: track,
        ['#flashActData.flashes']: flashLists,
      }) => continuation({
        ['#flashActIndex']:
          flashLists.findIndex(flashes => flashes.includes(track)),
      }),
    },

    // album not found conditionally exits or outputs null.

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

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

    {
      dependencies: ['flashActData', '#flashActIndex'],
      compute: (continuation, {
        ['flashActData']: flashActData,
        ['#flashActIndex']: flashActIndex,
      }) => continuation.raiseOutput({
        ['#flashAct']:
          flashActData[flashActIndex],
      }),
    },
  ],
});