-
Notifications
You must be signed in to change notification settings - Fork 0
/
withPauseWhenNotVisible.tsx
59 lines (48 loc) · 1.67 KB
/
withPauseWhenNotVisible.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
import * as React from "react";
import { useIsFocused, useFocusEffect } from "@react-navigation/native";
import { Text } from "../components/Themed";
const InnerWrapper: React.FC<{ visiblePromise: Promise<void> | undefined }> = ({
visiblePromise,
children,
}) => {
if (visiblePromise) throw visiblePromise;
return <>{children}</>;
};
const OuterWrapper: React.FC<{ label: string }> = ({ children, label }) => {
const isFocused = useIsFocused();
const [pauseUpdates, setPauseUpdates] = React.useState(!isFocused);
React.useEffect(() => {
if (isFocused) {
setPauseUpdates(false);
} else {
const timeoutId = setTimeout(() => setPauseUpdates(true), 500);
return () => clearTimeout(timeoutId);
}
}, [isFocused]);
console.log("Render", label, isFocused);
const resolveWhenVisible = React.useRef<
{ promise: Promise<void>; resolve: () => void } | undefined
>(undefined);
if (pauseUpdates && !resolveWhenVisible.current) {
let resolve: () => void;
const promise = new Promise<void>((innerResolve) => (resolve = innerResolve));
//@ts-expect-error
resolveWhenVisible.current = { promise, resolve };
}
if (!pauseUpdates && resolveWhenVisible.current) {
resolveWhenVisible.current.resolve();
resolveWhenVisible.current = undefined;
}
return (
<React.Suspense fallback={<Text>Suspended {label}</Text>}>
<InnerWrapper visiblePromise={resolveWhenVisible.current?.promise}>{children}</InnerWrapper>
</React.Suspense>
);
};
export const withPauseWhenNotVisible = (Component: React.ComponentType, label: string) => {
return () => (
<OuterWrapper label={label}>
<Component />
</OuterWrapper>
);
};