-
Notifications
You must be signed in to change notification settings - Fork 34
/
Initializer.tsx
56 lines (47 loc) · 1.32 KB
/
Initializer.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
import React, { Suspense, useEffect, useMemo, useRef, useState } from 'react';
import { StyleSheet, View } from 'react-native';
import usePromise from '@/hooks/usePromise';
import initialize from '@/initializers';
import { mark } from '@/utils/performance';
import Animation from './Animation';
export default function Initializer({
children,
}: {
children: React.ReactNode;
}) {
const { state, error } = usePromise(initialize, []);
if (error) throw error;
const [isSuspended, setSuspended] = useState(false);
const Fallback = useMemo(
() =>
function Fallback() {
useEffect(() => {
mark('Kitsu.Initializer.Suspend');
setSuspended(true);
return () => {
mark('Kitsu.Initializer.Unsuspend');
setSuspended(false);
};
});
return <View />;
},
[setSuspended]
);
useEffect(() => mark('Kitsu.Initializer.Start'), []);
useEffect(() => {
if (state === 'fulfilled') {
mark('Kitsu.Initializer.Resolved');
}
}, [state]);
return (
<View style={styles.wrapper}>
<Animation isBooted={!isSuspended && state === 'fulfilled'} />
<Suspense fallback={<Fallback />}>{children}</Suspense>
</View>
);
}
const styles = StyleSheet.create({
wrapper: {
...StyleSheet.absoluteFillObject,
},
});