-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
CellSizeAndPositionManager.js
313 lines (259 loc) · 9.07 KB
/
CellSizeAndPositionManager.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/** @flow */
import type {Alignment, CellSizeGetter, VisibleCellRange} from '../types';
type CellSizeAndPositionManagerParams = {
cellCount: number,
cellSizeGetter: CellSizeGetter,
estimatedCellSize: number,
};
type ConfigureParams = {
cellCount: number,
estimatedCellSize: number,
cellSizeGetter: CellSizeGetter,
};
type GetUpdatedOffsetForIndex = {
align: Alignment,
containerSize: number,
currentOffset: number,
targetIndex: number,
};
type GetVisibleCellRangeParams = {
containerSize: number,
offset: number,
};
type SizeAndPositionData = {
offset: number,
size: number,
};
/**
* Just-in-time calculates and caches size and position information for a collection of cells.
*/
export default class CellSizeAndPositionManager {
// Cache of size and position data for cells, mapped by cell index.
// Note that invalid values may exist in this map so only rely on cells up to this._lastMeasuredIndex
_cellSizeAndPositionData = {};
// Measurements for cells up to this index can be trusted; cells afterward should be estimated.
_lastMeasuredIndex = -1;
// Used in deferred mode to track which cells have been queued for measurement.
_lastBatchedIndex = -1;
_cellCount: number;
_cellSizeGetter: CellSizeGetter;
_estimatedCellSize: number;
constructor({
cellCount,
cellSizeGetter,
estimatedCellSize,
}: CellSizeAndPositionManagerParams) {
this._cellSizeGetter = cellSizeGetter;
this._cellCount = cellCount;
this._estimatedCellSize = estimatedCellSize;
}
areOffsetsAdjusted() {
return false;
}
configure({cellCount, estimatedCellSize, cellSizeGetter}: ConfigureParams) {
this._cellCount = cellCount;
this._estimatedCellSize = estimatedCellSize;
this._cellSizeGetter = cellSizeGetter;
}
getCellCount(): number {
return this._cellCount;
}
getEstimatedCellSize(): number {
return this._estimatedCellSize;
}
getLastMeasuredIndex(): number {
return this._lastMeasuredIndex;
}
getOffsetAdjustment() {
return 0;
}
/**
* This method returns the size and position for the cell at the specified index.
* It just-in-time calculates (or used cached values) for cells leading up to the index.
*/
getSizeAndPositionOfCell(index: number): SizeAndPositionData {
if (index < 0 || index >= this._cellCount) {
throw Error(
`Requested index ${index} is outside of range 0..${this._cellCount}`,
);
}
if (index > this._lastMeasuredIndex) {
let lastMeasuredCellSizeAndPosition = this.getSizeAndPositionOfLastMeasuredCell();
let offset =
lastMeasuredCellSizeAndPosition.offset +
lastMeasuredCellSizeAndPosition.size;
for (var i = this._lastMeasuredIndex + 1; i <= index; i++) {
let size = this._cellSizeGetter({index: i});
// undefined or NaN probably means a logic error in the size getter.
// null means we're using CellMeasurer and haven't yet measured a given index.
if (size === undefined || isNaN(size)) {
throw Error(`Invalid size returned for cell ${i} of value ${size}`);
} else if (size === null) {
this._cellSizeAndPositionData[i] = {
offset,
size: 0,
};
this._lastBatchedIndex = index;
} else {
this._cellSizeAndPositionData[i] = {
offset,
size,
};
offset += size;
this._lastMeasuredIndex = index;
}
}
}
return this._cellSizeAndPositionData[index];
}
getSizeAndPositionOfLastMeasuredCell(): SizeAndPositionData {
return this._lastMeasuredIndex >= 0
? this._cellSizeAndPositionData[this._lastMeasuredIndex]
: {
offset: 0,
size: 0,
};
}
/**
* Total size of all cells being measured.
* This value will be completely estimated initially.
* As cells are measured, the estimate will be updated.
*/
getTotalSize(): number {
const lastMeasuredCellSizeAndPosition = this.getSizeAndPositionOfLastMeasuredCell();
const totalSizeOfMeasuredCells =
lastMeasuredCellSizeAndPosition.offset +
lastMeasuredCellSizeAndPosition.size;
const numUnmeasuredCells = this._cellCount - this._lastMeasuredIndex - 1;
const totalSizeOfUnmeasuredCells =
numUnmeasuredCells * this._estimatedCellSize;
return totalSizeOfMeasuredCells + totalSizeOfUnmeasuredCells;
}
/**
* Determines a new offset that ensures a certain cell is visible, given the current offset.
* If the cell is already visible then the current offset will be returned.
* If the current offset is too great or small, it will be adjusted just enough to ensure the specified index is visible.
*
* @param align Desired alignment within container; one of "auto" (default), "start", or "end"
* @param containerSize Size (width or height) of the container viewport
* @param currentOffset Container's current (x or y) offset
* @param totalSize Total size (width or height) of all cells
* @return Offset to use to ensure the specified cell is visible
*/
getUpdatedOffsetForIndex({
align = 'auto',
containerSize,
currentOffset,
targetIndex,
}: GetUpdatedOffsetForIndex): number {
if (containerSize <= 0) {
return 0;
}
const datum = this.getSizeAndPositionOfCell(targetIndex);
const maxOffset = datum.offset;
const minOffset = maxOffset - containerSize + datum.size;
let idealOffset;
switch (align) {
case 'start':
idealOffset = maxOffset;
break;
case 'end':
idealOffset = minOffset;
break;
case 'center':
idealOffset = maxOffset - (containerSize - datum.size) / 2;
break;
default:
idealOffset = Math.max(minOffset, Math.min(maxOffset, currentOffset));
break;
}
const totalSize = this.getTotalSize();
return Math.max(0, Math.min(totalSize - containerSize, idealOffset));
}
getVisibleCellRange(params: GetVisibleCellRangeParams): VisibleCellRange {
let {containerSize, offset} = params;
const totalSize = this.getTotalSize();
if (totalSize === 0) {
return {};
}
const maxOffset = offset + containerSize;
const start = this._findNearestCell(offset);
const datum = this.getSizeAndPositionOfCell(start);
offset = datum.offset + datum.size;
let stop = start;
while (offset < maxOffset && stop < this._cellCount - 1) {
stop++;
offset += this.getSizeAndPositionOfCell(stop).size;
}
return {
start,
stop,
};
}
/**
* Clear all cached values for cells after the specified index.
* This method should be called for any cell that has changed its size.
* It will not immediately perform any calculations; they'll be performed the next time getSizeAndPositionOfCell() is called.
*/
resetCell(index: number): void {
this._lastMeasuredIndex = Math.min(this._lastMeasuredIndex, index - 1);
}
_binarySearch(high: number, low: number, offset: number): number {
while (low <= high) {
const middle = low + Math.floor((high - low) / 2);
const currentOffset = this.getSizeAndPositionOfCell(middle).offset;
if (currentOffset === offset) {
return middle;
} else if (currentOffset < offset) {
low = middle + 1;
} else if (currentOffset > offset) {
high = middle - 1;
}
}
if (low > 0) {
return low - 1;
} else {
return 0;
}
}
_exponentialSearch(index: number, offset: number): number {
let interval = 1;
while (
index < this._cellCount &&
this.getSizeAndPositionOfCell(index).offset < offset
) {
index += interval;
interval *= 2;
}
return this._binarySearch(
Math.min(index, this._cellCount - 1),
Math.floor(index / 2),
offset,
);
}
/**
* Searches for the cell (index) nearest the specified offset.
*
* If no exact match is found the next lowest cell index will be returned.
* This allows partially visible cells (with offsets just before/above the fold) to be visible.
*/
_findNearestCell(offset: number): number {
if (isNaN(offset)) {
throw Error(`Invalid offset ${offset} specified`);
}
// Our search algorithms find the nearest match at or below the specified offset.
// So make sure the offset is at least 0 or no match will be found.
offset = Math.max(0, offset);
const lastMeasuredCellSizeAndPosition = this.getSizeAndPositionOfLastMeasuredCell();
const lastMeasuredIndex = Math.max(0, this._lastMeasuredIndex);
if (lastMeasuredCellSizeAndPosition.offset >= offset) {
// If we've already measured cells within this range just use a binary search as it's faster.
return this._binarySearch(lastMeasuredIndex, 0, offset);
} else {
// If we haven't yet measured this high, fallback to an exponential search with an inner binary search.
// The exponential search avoids pre-computing sizes for the full set of cells as a binary search would.
// The overall complexity for this approach is O(log n).
return this._exponentialSearch(lastMeasuredIndex, offset);
}
}
}