forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCellMeasurerCache.js
227 lines (190 loc) · 6.35 KB
/
CellMeasurerCache.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
/** @flow */
import type {CellMeasureCache} from './types';
export const DEFAULT_HEIGHT = 30;
export const DEFAULT_WIDTH = 100;
// Enables more intelligent mapping of a given column and row index to an item ID.
// This prevents a cell cache from being invalidated when its parent collection is modified.
type KeyMapper = (rowIndex: number, columnIndex: number) => any;
type CellMeasurerCacheParams = {
defaultHeight?: number,
defaultWidth?: number,
fixedHeight?: boolean,
fixedWidth?: boolean,
minHeight?: number,
minWidth?: number,
keyMapper?: KeyMapper,
};
type Cache = {
[key: any]: number,
};
type IndexParam = {
index: number,
};
/**
* Caches measurements for a given cell.
*/
export default class CellMeasurerCache implements CellMeasureCache {
_cellHeightCache: Cache = {};
_cellWidthCache: Cache = {};
_columnWidthCache: Cache = {};
_rowHeightCache: Cache = {};
_defaultHeight: number;
_defaultWidth: number;
_minHeight: number;
_minWidth: number;
_keyMapper: KeyMapper;
_hasFixedHeight: boolean;
_hasFixedWidth: boolean;
_columnCount = 0;
_rowCount = 0;
constructor(params: CellMeasurerCacheParams = {}) {
const {
defaultHeight,
defaultWidth,
fixedHeight,
fixedWidth,
keyMapper,
minHeight,
minWidth,
} = params;
this._hasFixedHeight = fixedHeight === true;
this._hasFixedWidth = fixedWidth === true;
this._minHeight = minHeight || 0;
this._minWidth = minWidth || 0;
this._keyMapper = keyMapper || defaultKeyMapper;
this._defaultHeight = Math.max(
this._minHeight,
typeof defaultHeight === 'number' ? defaultHeight : DEFAULT_HEIGHT,
);
this._defaultWidth = Math.max(
this._minWidth,
typeof defaultWidth === 'number' ? defaultWidth : DEFAULT_WIDTH,
);
if (process.env.NODE_ENV !== 'production') {
if (this._hasFixedHeight === false && this._hasFixedWidth === false) {
console.warn(
"CellMeasurerCache should only measure a cell's width or height. " +
'You have configured CellMeasurerCache to measure both. ' +
'This will result in poor performance.',
);
}
if (this._hasFixedHeight === false && this._defaultHeight === 0) {
console.warn(
'Fixed height CellMeasurerCache should specify a :defaultHeight greater than 0. ' +
'Failing to do so will lead to unnecessary layout and poor performance.',
);
}
if (this._hasFixedWidth === false && this._defaultWidth === 0) {
console.warn(
'Fixed width CellMeasurerCache should specify a :defaultWidth greater than 0. ' +
'Failing to do so will lead to unnecessary layout and poor performance.',
);
}
}
}
clear(rowIndex: number, columnIndex: number = 0) {
const key = this._keyMapper(rowIndex, columnIndex);
delete this._cellHeightCache[key];
delete this._cellWidthCache[key];
this._updateCachedColumnAndRowSizes(rowIndex, columnIndex);
}
clearAll() {
this._cellHeightCache = {};
this._cellWidthCache = {};
this._columnWidthCache = {};
this._rowHeightCache = {};
this._rowCount = 0;
this._columnCount = 0;
}
columnWidth = ({index}: IndexParam) => {
const key = this._keyMapper(0, index);
return this._columnWidthCache[key] !== undefined
? this._columnWidthCache[key]
: this._defaultWidth;
};
get defaultHeight(): number {
return this._defaultHeight;
}
get defaultWidth(): number {
return this._defaultWidth;
}
hasFixedHeight(): boolean {
return this._hasFixedHeight;
}
hasFixedWidth(): boolean {
return this._hasFixedWidth;
}
getHeight(rowIndex: number, columnIndex: number = 0): number {
if (this._hasFixedHeight) {
return this._defaultHeight;
} else {
const key = this._keyMapper(rowIndex, columnIndex);
return this._cellHeightCache[key] !== undefined
? Math.max(this._minHeight, this._cellHeightCache[key])
: this._defaultHeight;
}
}
getWidth(rowIndex: number, columnIndex: number = 0): number {
if (this._hasFixedWidth) {
return this._defaultWidth;
} else {
const key = this._keyMapper(rowIndex, columnIndex);
return this._cellWidthCache[key] !== undefined
? Math.max(this._minWidth, this._cellWidthCache[key])
: this._defaultWidth;
}
}
has(rowIndex: number, columnIndex: number = 0): boolean {
const key = this._keyMapper(rowIndex, columnIndex);
return this._cellHeightCache[key] !== undefined;
}
rowHeight = ({index}: IndexParam) => {
const key = this._keyMapper(index, 0);
return this._rowHeightCache[key] !== undefined
? this._rowHeightCache[key]
: this._defaultHeight;
};
set(
rowIndex: number,
columnIndex: number,
width: number,
height: number,
): void {
const key = this._keyMapper(rowIndex, columnIndex);
if (columnIndex >= this._columnCount) {
this._columnCount = columnIndex + 1;
}
if (rowIndex >= this._rowCount) {
this._rowCount = rowIndex + 1;
}
// Size is cached per cell so we don't have to re-measure if cells are re-ordered.
this._cellHeightCache[key] = height;
this._cellWidthCache[key] = width;
this._updateCachedColumnAndRowSizes(rowIndex, columnIndex);
}
_updateCachedColumnAndRowSizes(rowIndex: number, columnIndex: number) {
// :columnWidth and :rowHeight are derived based on all cells in a column/row.
// Pre-cache these derived values for faster lookup later.
// Reads are expected to occur more frequently than writes in this case.
// Only update non-fixed dimensions though to avoid doing unnecessary work.
if (!this._hasFixedWidth) {
let columnWidth = 0;
for (let i = 0; i < this._rowCount; i++) {
columnWidth = Math.max(columnWidth, this.getWidth(i, columnIndex));
}
const columnKey = this._keyMapper(0, columnIndex);
this._columnWidthCache[columnKey] = columnWidth;
}
if (!this._hasFixedHeight) {
let rowHeight = 0;
for (let i = 0; i < this._columnCount; i++) {
rowHeight = Math.max(rowHeight, this.getHeight(rowIndex, i));
}
const rowKey = this._keyMapper(rowIndex, 0);
this._rowHeightCache[rowKey] = rowHeight;
}
}
}
function defaultKeyMapper(rowIndex: number, columnIndex: number) {
return `${rowIndex}-${columnIndex}`;
}