« get me outta code hell

withReverseReferenceList.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/withReverseReferenceList.js
blob: 2da9194d9d60c329d180f5c385fe9fc8ca9cae7e (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Check out the info on reverseReferenceList!
// This is its composable form.
//
// This implementation uses a global cache (via WeakMap) to attempt to speed
// up subsequent similar accesses.
//
// This has absolutely not been rigorously tested with altering properties of
// data objects in a wiki data array which is reused. If a new wiki data array
// is used, a fresh cache will always be created.
//
// Note that this implementation is mirrored in withReverseContributionList,
// so any changes should be reflected there (until these are combined).

import {input, templateCompositeFrom} from '#composite';
import {sortByDate} from '#sort';
import {stitchArrays} from '#sugar';

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

import inputWikiData from './inputWikiData.js';

// Mapping of reference list property to WeakMap.
// Each WeakMap maps a wiki data array to another weak map,
// which in turn maps each referenced thing to an array of
// things referencing it.
const caches = new Map();

export default templateCompositeFrom({
  annotation: `withReverseReferenceList`,

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

  outputs: ['#reverseReferenceList'],

  steps: () => [
    // Common behavior --

    // Early exit with an empty array if the data list isn't available.
    exitWithoutDependency({
      dependency: input('data'),
      value: input.value([]),
    }),

    // Raise an empty array (don't early exit) if the data list is empty.
    raiseOutputWithoutDependency({
      dependency: input('data'),
      mode: input.value('empty'),
      output: input.value({'#reverseReferenceList': []}),
    }),

    // Check for an existing cache record which corresponds to this
    // input('list') and input('data'). If it exists, query it for the
    // current thing, and raise that; if it doesn't, create it, put it
    // where it needs to be, and provide it so the next steps can fill
    // it in.
    {
      dependencies: [input('list'), input('data'), input.myself()],

      compute: (continuation, {
        [input('list')]: list,
        [input('data')]: data,
        [input.myself()]: myself,
      }) => {
        if (!caches.has(list)) {
          const cache = new WeakMap();
          caches.set(list, cache);

          const cacheRecord = new WeakMap();
          cache.set(data, cacheRecord);

          return continuation({
            ['#cacheRecord']: cacheRecord,
          });
        }

        const cache = caches.get(list);

        if (!cache.has(data)) {
          const cacheRecord = new WeakMap();
          cache.set(data, cacheRecord);

          return continuation({
            ['#cacheRecord']: cacheRecord,
          });
        }

        return continuation.raiseOutput({
          ['#reverseReferenceList']:
            cache.get(data).get(myself) ?? [],
        });
      },
    },

    // Unique behavior for reference lists --

    {
      dependencies: [input('list')],
      compute: (continuation, {
        [input('list')]: list,
      }) => continuation({
        ['#referenceMap']:
          thing => thing[list],
      }),
    },

    withMappedList({
      list: input('data'),
      map: '#referenceMap',
    }).outputs({
      '#mappedList': '#referencedThings',
    }),

    {
      dependencies: [input('data')],
      compute: (continuation, {
        [input('data')]: data,
      }) => continuation({
        ['#referencingThings']:
          data,
      }),
    },

    // Common behavior --

    // Actually fill in the cache record. Since we're building up a *reverse*
    // reference list, track connections in terms of the referenced thing.
    // Although we gather all referenced things into a set and provide that
    // for sorting purposes in the next step, we *don't* reprovide the cache
    // record, because we're mutating that in-place - we'll just reuse its
    // existing '#cacheRecord' dependency.
    {
      dependencies: ['#cacheRecord', '#referencingThings', '#referencedThings'],
      compute: (continuation, {
        ['#cacheRecord']: cacheRecord,
        ['#referencingThings']: referencingThings,
        ['#referencedThings']: referencedThings,
      }) => {
        const allReferencedThings = new Set();

        stitchArrays({
          referencingThing: referencingThings,
          referencedThings: referencedThings,
        }).forEach(({referencingThing, referencedThings}) => {
            for (const referencedThing of referencedThings) {
              if (cacheRecord.has(referencedThing)) {
                cacheRecord.get(referencedThing).push(referencingThing);
              } else {
                cacheRecord.set(referencedThing, [referencingThing]);
                allReferencedThings.add(referencedThing);
              }
            }
          });

        return continuation({
          ['#allReferencedThings']:
            allReferencedThings,
        });
      },
    },

    // Sort the entries in the cache records, too, just by date - the rest of
    // sorting should be handled outside of withReverseContributionList, either
    // preceding (changing the 'data' input) or following (sorting the output).
    // Again we're mutating in place, so no need to reprovide '#cacheRecord'
    // here.
    {
      dependencies: ['#cacheRecord', '#allReferencedThings'],
      compute: (continuation, {
        ['#cacheRecord']: cacheRecord,
        ['#allReferencedThings']: allReferencedThings,
      }) => {
        for (const referencedThing of allReferencedThings) {
          if (cacheRecord.has(referencedThing)) {
            const referencingThings = cacheRecord.get(referencedThing);
            sortByDate(referencingThings);
          }
        }

        return continuation();
      },
    },

    // Then just pluck out the current object from the now-filled cache record!
    {
      dependencies: ['#cacheRecord', input.myself()],
      compute: (continuation, {
        ['#cacheRecord']: cacheRecord,
        [input.myself()]: myself,
      }) => continuation({
        ['#reverseReferenceList']:
          cacheRecord.get(myself) ?? [],
      }),
    },
  ],
});