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
|
// Analogous implementation for withReverseReferenceList, for contributions.
// This is mostly duplicate code and both should be ported to the same
// underlying data form later on. Unique to contributions, the 'mode' option
// controls whether the things themselves, for which the artist is credited,
// are exposed (the default), or the actual contribution objects representing
// the relationship itself, instead.
//
// 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.
import {input, templateCompositeFrom} from '#composite';
import {is} from '#validators';
import {exitWithoutDependency, raiseOutputWithoutDependency}
from '#composite/control-flow';
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: `withReverseContributionList`,
inputs: {
data: inputWikiData({allowMixedTypes: false}),
list: input({type: 'string'}),
mode: input({
validate: is('things', 'contributions'),
defaultValue: 'things',
}),
},
outputs: ['#reverseContributionList'],
steps: () => [
// 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({'#reverseContributionList': []}),
}),
{
dependencies: [input.myself(), input('data'), input('list')],
compute: (continuation, {
[input.myself()]: myself,
[input('data')]: data,
[input('list')]: list,
}) => {
if (!caches.has(list)) {
caches.set(list, new WeakMap());
}
const cache = caches.get(list);
if (!cache.has(data)) {
const cacheRecord = new WeakMap();
for (const referencingThing of data) {
const contributionList = referencingThing[list];
for (const contribution of contributionList) {
const {artist} = contribution;
if (cacheRecord.has(artist)) {
cacheRecord.get(artist).push(contribution);
} else {
cacheRecord.set(artist, [contribution]);
}
}
}
cache.set(data, cacheRecord);
}
return continuation({
['#contributions']:
cache.get(data).get(myself) ?? [],
});
},
},
{
dependencies: ['#contributions', input('mode')],
compute: (continuation, {
['#contributions']: contributions,
[input('mode')]: mode,
}) => continuation({
['#reverseContributionList']:
(mode === 'contributions'
? contributions
: mode === 'things'
? contributions.map(contrib => contrib.thing)
: []),
}),
},
],
});
|