blob: fdf1f4eb0eac46e645afe9907d5a0dfdeadc1084 (
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
|
import * as validation from "@jet/environment/json/validation";
import * as serverData from "./server-data";
const DERIVED_CACHE_KEY = "_jet-internal:derived-data";
export function value(data, key, generator) {
if (!data) {
return null;
}
return validation.context(key, () => {
let computedDataStore = data[DERIVED_CACHE_KEY];
let returnValue = null;
if (!computedDataStore) {
computedDataStore = {};
data[DERIVED_CACHE_KEY] = computedDataStore;
returnValue = generateValue(computedDataStore, key, generator);
}
else {
returnValue = serverData.traverse(computedDataStore, key);
if (!returnValue) {
returnValue = generateValue(computedDataStore, key, generator);
}
}
return returnValue;
});
}
/**
* Computes an unknown value and saves it back into computedDataStore
* @param {JSONData} computedDataStore The dictionary in which to store the data
* @param {string} key The key to store it with
* @param {() => T} generator A function that will generate the value
* @returns {T} The value that was generated
*/
function generateValue(computedDataStore, key, generator) {
const generatedValue = generator();
computedDataStore[key] = generatedValue;
return generatedValue;
}
//# sourceMappingURL=derived-data.js.map
|