-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
175 lines (151 loc) · 3.92 KB
/
index.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
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
'use strict';
// ttl - the time to live in ms
// fetchFunction - should return a promise that maps:
// [id1, id2, ...] => { id1: { data1 }, id2: { data2 }, ... }
function timedCacheWrapper(ttl, fetchFunction){
const expireTime = {};
const fetchedData = {};
const fetchHistory = [];
return (ids) => {
const now = new Date().getTime();
const ret = {};
const idsAsFetch = [];
for(let id of ids) {
const t = expireTime[id] || 0;
if (now > t) {
idsAsFetch.push(id);
}
else {
ret[id] = fetchedData[id];
}
}
if(idsAsFetch.length == 0) {
return Promise.resolve(ret);
}
const promiseToReturn = fetchFunction(idsAsFetch)
.then( (data) => {
const expire = new Date().getTime() + ttl;
Object.assign(fetchedData, data);
for(let id in data) {
expireTime[id] = expire;
fetchHistory.push(id);
}
return data;
})
.then( (data) => Object.assign(ret, data) );
if(ttl === 0) {
return promiseToReturn;
}
// while we are waiting for a result, cleanup old data
while(true) {
const id = fetchHistory[0];
if(fetchHistory.length == 0) break;
if(expireTime[id] > now) break;
fetchHistory.shift();
delete expireTime[id];
delete fetchedData[id];
}
return promiseToReturn;
};
}
function hashRowsByUniqColumnFactory(column) {
return (rows) => {
const hash = {};
for(let row of rows) {
const key = row[column];
if( key === undefined ) throw new Error(`Missing value for ${column} on ${row}`);
hash[key] = row;
}
return hash;
};
}
function hashRowsByColumnFactory(column){
return (rows) => {
const hash = {};
for(let row of rows) {
const key = row[column];
if( key === undefined ) throw new Error(`Missing value for ${column} on ${row}`);
hash[key] = hash[key] || [];
hash[key].push(row);
}
return hash;
};
}
function cachePromiseById(ttl, fetchFunction) {
const idToValue = (ids) =>
fetchFunction(ids[0])
.then( (data) => {
const h = {};
h[ids[0]] = data;
return h;
});
const cachedFunction = timedCacheWrapper(ttl, idToValue);
return (id) =>
cachedFunction([id])
.then( data => data[id] );
}
function cachePromisePerId(ttl, fetchFunction, idAttribue = 'id') {
const idsAsValues = (ids) =>
fetchFunction(ids)
.then(hashRowsByColumnFactory(idAttribue));
const cachedFunction = timedCacheWrapper(ttl, idsAsValues);
return (ids) =>
cachedFunction(ids)
.then(h => Object.values(h).reduce( (a,b) => a.concat(b), []));
}
function cachePromisePerHashKey(ttl, fetchFunction) {
return timedCacheWrapper(ttl, fetchFunction);
}
// Callback function support
function nullCB() {
return null;
}
function cacheCallbackById(ttl, fetchFunction){
const f = cachePromiseById(ttl, promisify(fetchFunction));
return callbackify(f);
}
function cacheCallbackPerId(ttl, fetchFunction){
const f = cachePromisePerId(ttl, promisify(fetchFunction));
return callbackify(f);
}
function cacheCallbackPerHashKey(ttl, fetchFunction){
const f = cachePromisePerHashKey(ttl, promisify(fetchFunction));
return callbackify(f);
}
module.exports = {
promise: {
idsAsHashKeys: cachePromisePerHashKey,
idsAsAttributes: cachePromisePerId,
idToValue: cachePromiseById
},
callback: {
idsAsHashKeys: cacheCallbackPerHashKey,
idsAsAttributes: cacheCallbackPerId,
idToValue: cacheCallbackById
}
};
function promisify(f) {
return (x) => {
return new Promise( (resolve, reject) => {
f(x, (err, data) => {
if(err){
reject(err);
}
else {
resolve(data);
}
});
});
};
}
function callbackify(f) {
return async (ids, cb = nullCB) => {
try {
const data = await f(ids);
cb(null, data);
}
catch (err) {
cb(err);
}
};
}