-
-
Notifications
You must be signed in to change notification settings - Fork 742
/
Perf.tsx
142 lines (125 loc) · 3.17 KB
/
Perf.tsx
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
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { SafeAreaView, StyleSheet, Text, View } from 'react-native';
import BottomSheet from '@gorhom/bottom-sheet';
import Button from '../components/button';
import { SafeAreaProvider } from 'react-native-safe-area-context';
const App = () => {
// state
const [end, setEnd] = useState(false);
const [mount, setMount] = useState(false);
const [showResult, setShowResult] = useState(false);
// ref
const bottomSheetRef = useRef<BottomSheet>(null);
// variables
const snapPoints = useMemo(() => [150, 300, 450], []);
const initialSnapPoint = useRef(0);
const startPerfTime = useRef(0);
const endPerfTime = useRef(0);
// callbacks
const finishPerformanceTest = useCallback(() => {
endPerfTime.current = Date.now();
setShowResult(true);
}, []);
const handleMountPress = useCallback(() => {
startPerfTime.current = Date.now();
setMount(state => !state);
}, []);
const handleAutoAnimatePress = useCallback(() => {
// initialSnapPoint.current = -1;
// handleMountPress();
let index = 0;
let loop = 1;
const timer = setInterval(() => {
if (loop === 4) {
clearInterval(timer);
setEnd(state => !state);
return;
}
if (index > snapPoints.length - 1) {
index = 0;
loop++;
}
bottomSheetRef.current?.snapTo(index++);
}, 1000);
return () => {
clearInterval(timer);
};
}, [snapPoints]);
// effects
useEffect(() => {
const timeout = setTimeout(() => {
handleMountPress();
clearTimeout(timeout);
}, 5000);
const timeout2 = setTimeout(() => {
handleAutoAnimatePress();
clearTimeout(timeout2);
}, 10000);
return () => {
clearTimeout(timeout);
clearTimeout(timeout2);
};
}, [handleMountPress, handleAutoAnimatePress]);
// render
return end ? (
<View style={styles.endContainer} />
) : (
<SafeAreaProvider>
<SafeAreaView style={styles.container}>
<Text style={styles.version}>Bottom Sheet v2 alpha-0</Text>
<Text style={styles.version}>Reanimated v2 alpha-6</Text>
{showResult && (
<Text style={styles.measure}>
{endPerfTime.current - startPerfTime.current}ms
</Text>
)}
{mount && (
<BottomSheet
ref={bottomSheetRef}
initialSnapIndex={initialSnapPoint.current}
snapPoints={snapPoints}
>
<View style={styles.content} onLayout={finishPerformanceTest} />
</BottomSheet>
)}
</SafeAreaView>
</SafeAreaProvider>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#555',
},
endContainer: {
flex: 1,
backgroundColor: 'red',
},
content: {
flex: 1,
backgroundColor: 'white',
},
buttonContainer: {
marginHorizontal: 24,
marginBottom: 6,
},
measure: {
marginHorizontal: 24,
fontSize: 64,
fontWeight: 'bold',
color: 'white',
},
version: {
marginHorizontal: 24,
marginTop: 12,
color: 'white',
fontSize: 20,
},
});
export default App;