« get me outta code hell

content-function.js « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content-function.js
blob: 654a294c5d1c801c55ddf86d0ccb43b226545f71 (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
import {annotateFunction, empty} from './util/sugar.js';

export default function contentFunction({
  contentDependencies = [],
  extraDependencies = [],

  data,
  generate,
}) {
  return expectDependencies({
    data,
    generate,

    expectedContentDependencyKeys: contentDependencies,
    expectedExtraDependencyKeys: extraDependencies,
    fulfilledDependencies: {},
  });
}

contentFunction.identifyingSymbol = Symbol(`Is a content function?`);

export function expectDependencies({
  generate,
  data,

  expectedContentDependencyKeys,
  expectedExtraDependencyKeys,
  fulfilledDependencies,
}) {
  if (!generate) {
    throw new Error(`Expected generate function`);
  }

  if (!data) {
    throw new Error(`Expected data function`);
  }

  const fulfilledDependencyKeys = Object.keys(fulfilledDependencies);

  const invalidatingDependencyKeys = Object.entries(fulfilledDependencies)
    .filter(([key, value]) => value.fulfilled === false)
    .map(([key]) => key);

  const missingContentDependencyKeys = expectedContentDependencyKeys
    .filter(key => !fulfilledDependencyKeys.includes(key));

  const missingExtraDependencyKeys = expectedExtraDependencyKeys
    .filter(key => !fulfilledDependencyKeys.includes(key));

  let wrappedGenerate;

  if (!empty(invalidatingDependencyKeys)) {
    wrappedGenerate = function() {
      throw new Error(`Generate invalidated because unfulfilled dependencies provided: ${invalidatingDependencyKeys.join(', ')}`);
    };

    annotateFunction(wrappedGenerate, {name: generate, trait: 'invalidated'});
    wrappedGenerate.fulfilled ??= false;
  }

  if (empty(missingContentDependencyKeys) && empty(missingExtraDependencyKeys)) {
    wrappedGenerate ??= function(data) {
      return generate(data, fulfilledDependencies);
    };

    annotateFunction(wrappedGenerate, {name: generate, trait: 'fulfilled'});
    wrappedGenerate.fulfilled ??= true;

    wrappedGenerate.fulfill = function() {
      throw new Error(`All dependencies already fulfilled`);
    };
  }

  wrappedGenerate ??= function() {
    throw new Error(`Dependencies still needed: ${missingContentDependencyKeys.concat(missingExtraDependencyKeys).join(', ')}`);
  };

  annotateFunction(wrappedGenerate, {name: generate, trait: 'unfulfilled'});
  wrappedGenerate.fulfilled ??= false;
  wrappedGenerate[contentFunction.identifyingSymbol] = true;

  if (empty(missingContentDependencyKeys)) {
    const dataDependencies = {};

    for (const key of expectedContentDependencyKeys) {
      const wrappedDependency = function() {
        throw new Error(`Expected call to this dependency's .data()`);
      };

      annotateFunction(wrappedGenerate, {name: fulfilledDependencies[key], description: 'data only'});
      wrappedDependency.data = fulfilledDependencies[key].data;
      dataDependencies[key] = wrappedDependency;
    }

    wrappedGenerate.data = function(...args) {
      return data(...args, dataDependencies);
    };

    annotateFunction(wrappedGenerate.data, {name: data, trait: 'fulfilled'});
  }

  wrappedGenerate.data ??= function() {
    throw new Error(`Dependencies still needed: ${missingContentDependencyKeys.join(', ')}`);
  };

  annotateFunction(wrappedGenerate.data, {name: data, trait: 'unfulfilled'});

  wrappedGenerate.fulfill ??= function fulfill(dependencies) {
    return expectDependencies({
      generate,
      data,

      expectedContentDependencyKeys,
      expectedExtraDependencyKeys,

      fulfilledDependencies: fulfillDependencies({
        name: generate.name,
        dependencies,

        expectedContentDependencyKeys,
        expectedExtraDependencyKeys,
        fulfilledDependencies,
      }),
    });
  };

  return wrappedGenerate;
}

export function fulfillDependencies({
  name,
  dependencies,
  expectedContentDependencyKeys,
  expectedExtraDependencyKeys,
  fulfilledDependencies,
}) {
  const newFulfilledDependencies = {...fulfilledDependencies};
  const fulfilledDependencyKeys = Object.keys(fulfilledDependencies);

  const errors = [];
  let bail = false;

  for (let [key, value] of Object.entries(dependencies)) {
    if (key.startsWith('u_')) {
      key = key.slice(2);
    }

    if (fulfilledDependencyKeys.includes(key)) {
      errors.push(new Error(`Dependency ${key} is already fulfilled`));
      bail = true;
      continue;
    }

    const isContentKey = expectedContentDependencyKeys.includes(key);
    const isExtraKey = expectedExtraDependencyKeys.includes(key);

    if (!isContentKey && !isExtraKey) {
      errors.push(new Error(`Dependency ${key} is not expected`));
      bail = true;
      continue;
    }

    if (isContentKey && !value[contentFunction.identifyingSymbol]) {
      errors.push(new Error(`Content dependency ${key} is not a content function`));
      bail = true;
      continue;
    }

    if (isExtraKey && value[contentFunction.identifyingSymbol]) {
      errors.push(new Error(`Extra dependency ${key} is a content function`));
      bail = true;
      continue;
    }

    if (!bail) {
      newFulfilledDependencies[key] = value;
    }
  }

  if (!empty(errors)) {
    throw new AggregateError(errors, `Errors fulfilling dependencies for ${name}`);
  }

  return newFulfilledDependencies;
}