-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathdefaultOverscanIndicesGetter.js
37 lines (32 loc) · 1.02 KB
/
defaultOverscanIndicesGetter.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
// @flow
import type {OverscanIndicesGetterParams, OverscanIndices} from './types';
export const SCROLL_DIRECTION_BACKWARD = -1;
export const SCROLL_DIRECTION_FORWARD = 1;
export const SCROLL_DIRECTION_HORIZONTAL = 'horizontal';
export const SCROLL_DIRECTION_VERTICAL = 'vertical';
/**
* Calculates the number of cells to overscan before and after a specified range.
* This function ensures that overscanning doesn't exceed the available cells.
*/
export default function defaultOverscanIndicesGetter({
cellCount,
overscanCellsCount,
scrollDirection,
startIndex,
stopIndex,
}: OverscanIndicesGetterParams): OverscanIndices {
if (scrollDirection === SCROLL_DIRECTION_FORWARD) {
return {
overscanStartIndex: Math.max(0, startIndex),
overscanStopIndex: Math.min(
cellCount - 1,
stopIndex + overscanCellsCount,
),
};
} else {
return {
overscanStartIndex: Math.max(0, startIndex - overscanCellsCount),
overscanStopIndex: Math.min(cellCount - 1, stopIndex),
};
}
}