-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPreload.tsx
40 lines (36 loc) · 1.05 KB
/
Preload.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
import React, { useEffect } from 'react';
import 'regenerator-runtime/runtime';
interface PreloadProps {
images: string[],
onPreloaded: () => void,
}
function Preload(props: PreloadProps): JSX.Element {
const { images } = props;
useEffect(() => {
// void means the promise's result is being ignored.
// we do this to avoid an eslint rule: @typescript-eslint/no-floating-promises
void cacheImages(images);
}, []);
const cacheImages = async (srcArray: Array<string>) => {
const promises = await srcArray.map((src: string) => {
return new Promise<string>(function (resolve, reject): void {
const img = new Image();
img.src = src;
img.onload = () => resolve(src);
img.onerror = () => reject(src);
});
});
await Promise.all(promises);
props.onPreloaded();
};
return (
<>
<div className='loading-screen'>
<div id={'loading-anim-rocket'} />
<div id={'loading-anim-planet'} />
Contacting Alien Species...
</div>
</>
);
}
export default Preload;