-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathDimensionCache.js
263 lines (241 loc) · 8.43 KB
/
DimensionCache.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import forOwn from 'lodash/forOwn';
export default class DimensionCache {
constructor(fillerItem, baseIndex = null, baseAmount = null) {
this.baseAccumIndex = baseIndex;
this.baseAccum = baseAmount;
this.highestAccum = null;
this.highestAccumIndex = null;
this.lowestAccum = null;
this.lowestAccumIndex = null;
this.fillerItem = fillerItem; // used for estimation on large, contiguous sets of non-present rows.
this.cache = {};
this.requests = {};
this.accumRequests = {};
this.length = 0;
}
set = (index, value) => {
this.cache[index] = value;
this.length += 1;
if (this.requests[index]) {
this.requests[index](value);
delete this.requests[index];
}
// if there's any accumulation requests, we want to clear any that we can,
// so we go in both directions of baseIndex, accumulating and fullfilling until we reach an
// empty spot in the cache.
if (Object.keys(this.accumRequests).length > 0) {
// let n = this.highestAccumIndex || parseInt(Object.keys(this.cache)[0], 10);
let currentHighIndex = this.highestAccumIndex || this.baseAccumIndex;
let currentHighAccum = this.highestAccum || this.baseAccum || null;
while (this.cache[currentHighIndex]) {
if (this.accumRequests[currentHighIndex]) {
this.accumRequests[currentHighIndex]({ result: currentHighAccum, estimate: false });
delete this.accumRequests[currentHighIndex];
}
currentHighAccum += this.cache[currentHighIndex];
currentHighIndex += 1;
}
if (this.accumRequests[currentHighIndex]) {
this.accumRequests[currentHighIndex]({ result: currentHighAccum, estimate: false });
// if there are other requests waiting in line, update estimate values...
forOwn(this.accumRequests, (func, key) => {
if (key > currentHighIndex) {
func({ result: currentHighAccum + ((key - currentHighIndex) * this.fillerItem), estimate: true });
}
});
}
let currentLowIndex = this.lowestAccumindex || this.baseAccumIndex;
let currentLowAccum = this.lowestAccum || this.baseAccum || null;
/* example - accumRequests contains request for 30...
* lowestAccumIndex == 32, lowestAccum = 1002.
* this.cache contains 32: 24..31: 48..30: 36...
* (31) 1002 - 48 = 954 ... (30) 954 - 36 = 918 ...
* accumRequests[30]({ result: 918, estimate: false });
*/
while (this.cache[currentLowIndex]) {
if (this.accumRequests[currentLowIndex]) {
this.accumRequests[currentLowIndex]({ result: currentLowAccum, estimate: false });
delete this.accumRequests[currentLowIndex];
}
currentLowIndex -= 1;
currentLowAccum -= this.cache[currentLowIndex];
}
if (this.accumRequests[currentLowIndex]) {
this.accumRequests[currentLowIndex]({ result: currentLowAccum, estimate: false });
// if there are other requests waiting in line, update estimate values...
forOwn(this.accumRequests, (func, key) => {
if (key < currentLowIndex) {
func({ result: currentLowAccum - ((currentLowIndex - key) * this.fillerItem), estimate: true });
}
});
}
}
};
// eslint... react/no-this-in-sfc triggers here since it returns null, but this is not an sfc....
/* eslint-disable react/no-this-in-sfc */
request = (index, cb) => {
if (this.cache[index] >= 0) {
return cb ? cb(this.cache[index]) : this.cache[index];
} else if (cb) {
this.requests[index] = cb;
}
return null;
};
// accumulate in respect to a certain index/amount
requestAccumulated = (index, cb) => {
let n = this.baseAccumIndex;
if (index < n) {
let currentLowAccum = this.lowestAccum || this.baseAccum;
if (this.lowestAccum !== null) {
n = this.lowestAccumIndex;
}
// backward accumulation requires all keys between index and base to be filled...
let isLowEstimate = false;
while (n > index) {
n -= 1;
if (this.cache[n]) {
currentLowAccum -= this.cache[n];
} else {
currentLowAccum -= this.fillerItem;
isLowEstimate = true;
}
}
if (!isLowEstimate) {
if (currentLowAccum < this.lowestAccum) {
this.lowestAccum = currentLowAccum;
this.lowestAccumIndex = index;
forOwn(this.accumRequests, (func, key) => {
if (key < this.lowestAccumIndex) {
func({ result: this.lowestAccum - ((this.lowestAccumIndex - key) * this.fillerItem), estimate: true });
}
});
}
} else if (cb) {
this.accumRequests[index] = cb;
}
return cb ? cb({ result: currentLowAccum, estimate: isLowEstimate }) : currentLowAccum;
} else {
let currentHighAccum = this.highestAccum || this.baseAccum;
if (this.highestAccum !== null) {
n = this.highestAccumIndex;
}
let isEstimate = false;
while (n < index) {
if (this.cache[n]) {
currentHighAccum += this.cache[n];
} else {
currentHighAccum += this.fillerItem;
isEstimate = true;
}
n += 1;
}
if (!isEstimate) {
if (currentHighAccum > this.highestAccum) {
this.highestAccum = currentHighAccum;
this.highestAccumIndex = index;
forOwn(this.accumRequests, (func, key) => {
if (key > this.highestAccumIndex) {
const result = this.highestAccum + ((parseInt(key, 10) - this.highestAccumIndex) * this.fillerItem);
func({ result, estimate: true });
}
});
}
} else if (cb) {
this.accumRequests[index] = cb;
}
return cb ? cb({ result:currentHighAccum, estimate: isEstimate }) : currentHighAccum;
}
}
findIndex = (func) => {
const found = Object.keys(this.cache).find(k => func(this.cache[k]));
return found ? parseInt(found, 10) : null;
}
forEach = (func) => {
return Object.keys(this.cache).forEach(k => func(this.cache[k], k));
};
getIndexByPosition = (position, average) => {
let currentAccum;
let currentIndex;
if (position <= 0) return 0;
if (position >= this.baseAccum) {
currentAccum = this.highestAccum || this.baseAccum;
currentIndex = this.highestAccumIndex || this.baseAccumIndex;
if (position < currentAccum) {
while (currentAccum > position) {
currentIndex -= 1;
if (this.cache[currentIndex]) {
currentAccum -= this.cache[currentIndex];
} else {
currentAccum -= average;
}
}
} else {
while (currentAccum < position) {
currentIndex += 1;
if (this.cache[currentIndex]) {
currentAccum += this.cache[currentIndex];
} else {
currentAccum += average;
}
}
}
} else {
currentAccum = this.lowestAccum || this.baseAccum;
currentIndex = this.lowestAccumIndex || this.baseAccumIndex;
if (position > currentAccum) {
while (currentAccum < position) {
currentIndex += 1;
if (this.cache[currentIndex]) {
currentAccum += this.cache[currentIndex];
} else {
currentAccum += average;
}
}
} else {
while (currentAccum > position) {
currentIndex -= 1;
if (this.cache[currentIndex]) {
currentAccum -= this.cache[currentIndex];
} else {
currentAccum -= average;
}
}
}
}
return currentIndex;
}
clearCache = () => {
this.cache = this.zeroSet ? { 0: 0 } : {};
this.startIndex = this.zeroSet ? 0 : null;
this.length = this.zeroSet ? 1 : 0;
};
clearRequests = () => {
this.requests = {};
}
clearRequest = (index) => {
delete this.requests[index];
}
clearAccumRequest = (index) => {
delete this.accumRequests[index];
}
clearAccumRequests = () => {
this.accumRequests = {};
this.highestAccum = null;
this.highestAccumIndex = null;
}
clearAll = () => {
this.clearRequests();
this.clearAccumRequests();
this.clearCache();
};
rebase = (index, amount) => {
this.baseAccumIndex = index;
this.baseAccum = amount;
this.highestAccum = null;
this.highestAccumIndex = null;
this.lowestAccum = null;
this.lowestAccumIndex = null;
this.cache = {};
this.length = 0;
}
}