-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdataReaderInitializer.js
39 lines (39 loc) · 1.12 KB
/
dataReaderInitializer.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeDataReader = void 0;
const cache_1 = require("./cache");
function initializeDataReader(apiFn, ...parameters) {
const apiFnCache = (0, cache_1.resourceCache)(apiFn);
const cachedResource = apiFnCache.get(...parameters);
if (cachedResource) {
return cachedResource;
}
let data;
let status = 'init';
let error;
const fetchingPromise = apiFn(...parameters)
.then((result) => {
data = result;
status = 'done';
return result;
})
.catch((err) => {
error = err;
status = 'error';
});
function dataReaderFn(modifier) {
if (status === 'init') {
throw fetchingPromise;
}
else if (status === 'error') {
throw error;
}
return typeof modifier === 'function'
? modifier(data)
: data;
}
apiFnCache.set(dataReaderFn, ...parameters);
return dataReaderFn;
}
exports.initializeDataReader = initializeDataReader;
//# sourceMappingURL=dataReaderInitializer.js.map