-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathuseVisibleRanges.tsx
More file actions
130 lines (103 loc) · 3.63 KB
/
Copy pathuseVisibleRanges.tsx
File metadata and controls
130 lines (103 loc) · 3.63 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
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
import { useRef } from "react";
import type { SharedValue } from "react-native-reanimated";
import { useDerivedValue } from "react-native-reanimated";
type Range = [number, number];
export interface VisibleRanges {
negativeRange: Range;
positiveRange: Range;
}
export type IVisibleRanges = SharedValue<VisibleRanges>;
function normalizeWindowSize(total: number, windowSize?: number) {
return typeof windowSize === "number" && Number.isFinite(windowSize) && windowSize > 0
? windowSize
: total;
}
function normalizeLoopIndex(currentIndex: number, total: number) {
return currentIndex < 0 ? (currentIndex % total) + total : currentIndex;
}
export function computeVisibleRanges(params: {
total: number;
windowSize?: number;
currentIndex: number;
loop?: boolean;
}): VisibleRanges {
"worklet";
const { total = 0, loop } = params;
const windowSize = normalizeWindowSize(total, params.windowSize);
if (total <= 0) {
return {
negativeRange: [0, 0],
positiveRange: [0, -1],
};
}
const positiveCount = Math.round(windowSize / 2);
const negativeCount = windowSize - positiveCount;
let currentIndex = params.currentIndex;
if (!Number.isFinite(currentIndex)) currentIndex = 0;
if (!loop) {
currentIndex = Math.max(0, Math.min(total - 1, currentIndex));
return {
negativeRange: [Math.max(0, currentIndex - (windowSize - 1)), currentIndex],
positiveRange: [currentIndex, Math.min(total - 1, currentIndex + (windowSize - 1))],
};
}
currentIndex = normalizeLoopIndex(currentIndex, total);
const negativeRange: Range = [
(currentIndex - negativeCount + total) % total,
(currentIndex - 1 + total) % total,
];
const positiveRange: Range = [
(currentIndex + total) % total,
(currentIndex + positiveCount + total) % total,
];
if (negativeRange[0] < total && negativeRange[0] > negativeRange[1]) {
negativeRange[1] = total - 1;
positiveRange[0] = 0;
}
if (positiveRange[0] > positiveRange[1]) {
negativeRange[1] = total - 1;
positiveRange[0] = 0;
}
return { negativeRange, positiveRange };
}
export function useVisibleRanges(options: {
total: number;
viewSize: number;
windowSize?: number;
translation: SharedValue<number>;
loop?: boolean;
}): IVisibleRanges {
const { total = 0, viewSize, translation, windowSize: _windowSize, loop } = options;
const windowSize = normalizeWindowSize(total, _windowSize);
const cachedRanges = useRef<VisibleRanges | null>(null);
const ranges = useDerivedValue(() => {
if (total <= 0) {
return computeVisibleRanges({ total, currentIndex: 0, windowSize, loop });
}
// Prevent division by zero when viewSize is not yet measured
if (!Number.isFinite(viewSize) || viewSize <= 0) {
return {
negativeRange: [0, 0] as Range,
positiveRange: [0, Math.min(total - 1, windowSize - 1)] as Range,
};
}
let currentIndex = Math.round(-translation.value / viewSize);
if (!Number.isFinite(currentIndex)) currentIndex = 0;
const newRanges = computeVisibleRanges({ total, windowSize, currentIndex, loop });
if (
cachedRanges.current &&
isArraysEqual(cachedRanges.current.negativeRange, newRanges.negativeRange) &&
isArraysEqual(cachedRanges.current.positiveRange, newRanges.positiveRange)
) {
return cachedRanges.current;
}
cachedRanges.current = newRanges;
return newRanges;
}, [loop, total, windowSize, translation, viewSize]);
return ranges;
}
function isArraysEqual(a: number[], b: number[]): boolean {
"worklet";
if (a.length !== b.length) return false;
return a.every((value, index) => value === b[index]);
}