« get me outta code hell

withUniqueReferencingThing.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/withUniqueReferencingThing.js
blob: ce04f8380d5873e6c5f8c2c4d36a4cd7211eb0d2 (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
// Like withReverseReferenceList, but this is specifically for special "unique"
// references, meaning this thing is referenced by exactly one or zero things
// in the data list.

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

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

import inputWikiData from './inputWikiData.js';
import withReverseReferenceList from './withReverseReferenceList.js';

export default templateCompositeFrom({
  annotation: `withUniqueReferencingThing`,

  inputs: {
    data: inputWikiData({allowMixedTypes: false}),
    list: input({type: 'string'}),
  },

  outputs: ['#uniqueReferencingThing'],

  steps: () => [
    // withReverseRefernceList does this check too, but it early exits with
    // an empty array. That's no good here!
    exitWithoutDependency({
      dependency: input('data'),
      mode: input.value('empty'),
    }),

    withReverseReferenceList({
      data: input('data'),
      list: input('list'),
    }),

    raiseOutputWithoutDependency({
      dependency: '#reverseReferenceList',
      mode: input.value('empty'),
      output: input.value({'#uniqueReferencingThing': null}),
    }),

    {
      dependencies: ['#reverseReferenceList'],
      compute: (continuation, {
        ['#reverseReferenceList']: reverseReferenceList,
      }) => continuation({
        ['#uniqueReferencingThing']:
          reverseReferenceList[0],
      }),
    },
  ],
});