« get me outta code hell

withResolvedReference.js « wiki-data « composite « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite/wiki-data/withResolvedReference.js
blob: 0fa5c554ff2b3de5805e728e3a61a39f38b08815 (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
// Resolves a reference by using the provided find function to match it
// within the provided thingData dependency. This will early exit if the
// data dependency is null, or, if notFoundMode is set to 'exit', if the find
// function doesn't match anything for the reference. Otherwise, the data
// object is provided on the output dependency; or null, if the reference
// doesn't match anything or itself was null to begin with.

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

import {
  exitWithoutDependency,
  raiseOutputWithoutDependency,
} from '#composite/control-flow';

import inputWikiData from './inputWikiData.js';

export default templateCompositeFrom({
  annotation: `withResolvedReference`,

  inputs: {
    ref: input({type: 'string', acceptsNull: true}),

    data: inputWikiData({allowMixedTypes: false}),
    find: input({type: 'function'}),

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

  outputs: ['#resolvedReference'],

  steps: () => [
    raiseOutputWithoutDependency({
      dependency: input('ref'),
      output: input.value({
        ['#resolvedReference']: null,
      }),
    }),

    exitWithoutDependency({
      dependency: input('data'),
    }),

    {
      dependencies: [
        input('ref'),
        input('data'),
        input('find'),
        input('notFoundMode'),
      ],

      compute(continuation, {
        [input('ref')]: ref,
        [input('data')]: data,
        [input('find')]: findFunction,
        [input('notFoundMode')]: notFoundMode,
      }) {
        const match = findFunction(ref, data, {mode: 'quiet'});

        if (match === null && notFoundMode === 'exit') {
          return continuation.exit(null);
        }

        return continuation.raiseOutput({
          ['#resolvedReference']: match ?? null,
        });
      },
    },
  ],
});