-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathindex.js
319 lines (290 loc) · 9.64 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
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
314
315
316
317
318
319
/**
* Copyright Schrodinger, LLC
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule reducers
*/
'use strict';
import clone from 'lodash/clone';
import pick from 'lodash/pick';
import IntegerBufferSet from '../vendor_upstream/struct/IntegerBufferSet';
import PrefixIntervalTree from '../vendor_upstream/struct/PrefixIntervalTree';
import shallowEqual from '../vendor_upstream/core/shallowEqual';
import convertColumnElementsToData from '../helper/convertColumnElementsToData';
import { getScrollAnchor, scrollTo } from './scrollAnchor';
import columnStateHelper from './columnStateHelper';
import computeRenderedRows from './computeRenderedRows';
import Scrollbar from '../plugins/Scrollbar';
import { createSlice, original } from '@reduxjs/toolkit';
/**
* @typedef {{
* rowBufferSet: IntegerBufferSet,
* rowOffsetIntervalTree: PrefixIntervalTree,
* storedHeights: !Array.<number>
* }}
*/
const InternalState = {};
/**
* Returns the default initial state for the redux store.
* This must be a brand new, independent object for each table instance
* or issues may occur due to multiple tables sharing data.
*
* @return {!Object}
*/
function getInitialState() {
const internalState = createInternalState();
return {
/*
* Input state set from props
*/
columnElements: [],
columnGroupElements: [],
elementTemplates: {
cell: [],
footer: [],
groupHeader: [],
header: [],
},
elementHeights: {
footerHeight: 0,
groupHeaderHeight: 0,
headerHeight: 0,
},
propsRevision: null,
rowSettings: {
bufferRowCount: undefined,
rowAttributesGetter: undefined,
rowHeight: 0,
rowHeightGetter: () => 0,
rowsCount: 0,
subRowHeight: 0,
subRowHeightGetter: () => 0,
},
scrollFlags: {
overflowX: 'auto',
overflowY: 'auto',
showScrollbarX: true,
showScrollbarY: true,
},
tableSize: {
height: undefined,
maxHeight: 0,
ownerHeight: undefined,
useMaxHeight: false,
width: 0,
},
/*
* Output state passed as props to the the rendered FixedDataTable
* NOTE (jordan) rows may contain undefineds if we don't need all the buffer positions
*/
firstRowIndex: 0,
firstRowOffset: 0,
maxScrollX: 0,
maxScrollY: 0,
rowOffsets: {},
rows: [], // rowsToRender
scrollContentHeight: 0,
scrollX: 0,
scrollbarXHeight: Scrollbar.SIZE,
scrollY: 0,
scrollbarYWidth: Scrollbar.SIZE,
scrolling: false,
/**
* Internal state is only used by reducers.
* NOTE (jordan, pradeep): Internal state is altered in place, so don't trust it for redux history or immutabability checks.
* We also purposefully avoid keeping the raw internal state as part of the redux store.
* Instead a getter can be used to retrieve the internal state.
*
* 1. Large data structures in internal state like `rowHeights` are mutated by reducers.
* Since we don't care about immutability, we avoid overheads seen in a typical immutable data structure.
*
* 2. Immer internally uses proxies on the entire redux store state inorder to detect state mutations in reducers,
* but watching large data structures is inefficient and slows down reducers.
* Internal state isn't a direct part of the redux store state because we separated it through a getter.
* This means there's no proxies watching over the internal state, and hence mutating it has no overheads.
*
* @type {!Function}
*/
getInternal: () => internalState,
};
}
/** @returns {!InternalState} */
function createInternalState() {
return {
rowBufferSet: new IntegerBufferSet(),
rowOffsetIntervalTree: null, // PrefixIntervalTree
storedHeights: [],
};
}
const slice = createSlice({
name: 'FDT',
/*
* NOTE (pradeep, wcjordan): The initial state will be populated through the `initialize` reducer.
* We can't preset the state using the `initialState` field because we need a brand new, independent object
* for each table instance, or issues may occur due to multiple tables sharing data (see #369 for an example)
*/
initialState: {},
reducers: {
initialize(state, action) {
const props = action.payload;
Object.assign(state, getInitialState());
setStateFromProps(state, props);
initializeRowHeightsAndOffsets(state);
const scrollAnchor = getScrollAnchor(state, props);
computeRenderedRows(state, scrollAnchor);
columnStateHelper.initialize(state, props, {});
},
propChange(state, action) {
const { newProps, oldProps } = action.payload;
const oldState = clone(original(state));
setStateFromProps(state, newProps);
if (
oldProps.rowsCount !== newProps.rowsCount ||
oldProps.rowHeight !== newProps.rowHeight ||
oldProps.subRowHeight !== newProps.subRowHeight
) {
initializeRowHeightsAndOffsets(state);
}
if (oldProps.rowsCount !== newProps.rowsCount) {
state.getInternal().rowBufferSet = new IntegerBufferSet();
}
const scrollAnchor = getScrollAnchor(state, newProps, oldProps);
// If anything has changed in state, update our rendered rows
if (!shallowEqual(state, oldState) || scrollAnchor.changed) {
computeRenderedRows(state, scrollAnchor);
}
columnStateHelper.initialize(state, newProps, oldProps);
// if scroll values have changed, then we're scrolling!
if (
state.scrollX !== oldState.scrollX ||
state.scrollY !== oldState.scrollY
) {
state.scrolling = state.scrolling || true;
}
// TODO REDUX_MIGRATION solve w/ evil-diff
// TODO (jordan) check if relevant props unchanged and
// children column widths and flex widths are unchanged
// alternatively shallow diff and reconcile props
},
scrollEnd(state) {
state.scrolling = false;
const previousScrollAnchor = {
firstIndex: state.firstRowIndex,
firstOffset: state.firstRowOffset,
lastIndex: state.lastIndex,
};
computeRenderedRows(state, previousScrollAnchor);
},
scrollToY(state, action) {
const scrollY = action.payload;
state.scrolling = true;
const scrollAnchor = scrollTo(state, scrollY);
computeRenderedRows(state, scrollAnchor);
},
scrollToX(state, action) {
const scrollX = action.payload;
state.scrolling = true;
state.scrollX = scrollX;
},
},
});
/**
* Initialize row heights (storedHeights) & offsets based on the default rowHeight
*
* @param {!Object} state
* @private
*/
function initializeRowHeightsAndOffsets(state) {
const { rowHeight, rowsCount, subRowHeight } = state.rowSettings;
const defaultFullRowHeight = rowHeight + subRowHeight;
const rowOffsetIntervalTree = PrefixIntervalTree.uniform(
rowsCount,
defaultFullRowHeight
);
const scrollContentHeight = rowsCount * defaultFullRowHeight;
const storedHeights = new Array(rowsCount);
for (let idx = 0; idx < rowsCount; idx++) {
storedHeights[idx] = defaultFullRowHeight;
}
state.scrollContentHeight = scrollContentHeight;
Object.assign(state.getInternal(), {
rowOffsetIntervalTree,
storedHeights,
});
}
/**
* @param {!Object} state
* @param {!Object} props
* @return {!Object}
* @private
*/
function setStateFromProps(state, props) {
const {
columnGroupElements,
columnElements,
elementTemplates,
useGroupHeader,
} = convertColumnElementsToData(props.children);
Object.assign(state, {
columnGroupElements,
columnElements,
elementTemplates,
propsRevision: state.propsRevision + 1,
});
// NOTE (pradeep): We pre-freeze these large collections to avoid
// performance bottle necks
//
// From Immer's docs:
// Immer freezes everything recursively. For large data objects
// that won't be changed in the future this might be over-kill,
// in that case it can be more efficient to shallowly
// pre-freeze data using the freeze utility.
Object.freeze(state.columnElements);
Object.freeze(state.columnGroupElements);
Object.freeze(state.elementTemplates);
state.elementHeights = Object.assign(
{},
state.elementHeights,
pick(props, [
'cellGroupWrapperHeight',
'footerHeight',
'groupHeaderHeight',
'headerHeight',
])
);
if (!useGroupHeader) {
state.elementHeights.groupHeaderHeight = 0;
}
state.rowSettings = Object.assign(
{},
state.rowSettings,
pick(props, ['bufferRowCount', 'rowHeight', 'rowsCount', 'subRowHeight'])
);
const { rowHeight, subRowHeight } = state.rowSettings;
state.rowSettings.rowHeightGetter =
props.rowHeightGetter || (() => rowHeight);
state.rowSettings.subRowHeightGetter =
props.subRowHeightGetter || (() => subRowHeight || 0);
state.rowSettings.rowAttributesGetter = props.rowAttributesGetter;
state.scrollFlags = Object.assign(
{},
state.scrollFlags,
pick(props, ['overflowX', 'overflowY', 'showScrollbarX', 'showScrollbarY'])
);
state.tableSize = Object.assign(
{},
state.tableSize,
pick(props, ['height', 'maxHeight', 'ownerHeight', 'width'])
);
state.tableSize.useMaxHeight = state.tableSize.height === undefined;
state.scrollbarXHeight = props.scrollbarXHeight;
state.scrollbarYWidth = props.scrollbarYWidth;
}
const { reducer, actions } = slice;
export const { initialize, propChange, scrollEnd, scrollToX, scrollToY } =
actions;
export default reducer;