forked from Disciplr-Org/Disciplr-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindowRange.ts
More file actions
49 lines (43 loc) · 1.62 KB
/
Copy pathwindowRange.ts
File metadata and controls
49 lines (43 loc) · 1.62 KB
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
/**
* windowRange – lightweight virtual-list math helper.
*
* Windowing is only engaged when the list length exceeds WINDOW_THRESHOLD.
* Below that threshold the full list is returned unchanged so small lists
* incur zero overhead.
*
* WINDOW_THRESHOLD – minimum row count before windowing is active (50).
* WINDOW_SIZE – maximum rows rendered at one time when windowing is on (40).
*/
export const WINDOW_THRESHOLD = 50;
export const WINDOW_SIZE = 40;
export interface WindowResult<T> {
/** Slice of items that should be rendered. */
items: T[];
/** Index of the first rendered item in the source array. */
startIndex: number;
/** Index one past the last rendered item in the source array. */
endIndex: number;
/** True when the source list exceeds WINDOW_THRESHOLD. */
windowed: boolean;
}
/**
* Returns the visible slice of `items` centred around `anchorIndex`.
*
* @param items Full sorted/filtered array.
* @param anchorIndex First item that must be visible (e.g. scroll-top row). Defaults to 0.
* @param windowSize Override for the number of rows to render. Defaults to WINDOW_SIZE.
*/
export function windowRange<T>(
items: T[],
anchorIndex = 0,
windowSize = WINDOW_SIZE,
): WindowResult<T> {
const total = items.length;
if (total <= WINDOW_THRESHOLD) {
return { items, startIndex: 0, endIndex: total, windowed: false };
}
const size = Math.max(1, windowSize);
const start = Math.min(Math.max(0, anchorIndex), Math.max(0, total - size));
const end = Math.min(start + size, total);
return { items: items.slice(start, end), startIndex: start, endIndex: end, windowed: true };
}