aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorVendicated <vendicated@riseup.net>2022-08-31 20:47:07 +0200
committerVendicated <vendicated@riseup.net>2022-08-31 20:47:07 +0200
commita7ccbcfca4f181790b86199a7f31d1ae19e0253f (patch)
tree56be6cef5437dff886dcefa04f930780d552c68c /src/utils
parent98cb301df53305f397ac6e1b4e603c930820f228 (diff)
downloadVencord-a7ccbcfca4f181790b86199a7f31d1ae19e0253f.tar.gz
Vencord-a7ccbcfca4f181790b86199a7f31d1ae19e0253f.tar.bz2
Vencord-a7ccbcfca4f181790b86199a7f31d1ae19e0253f.zip
Refactor webpack; Add ErrorBoundary
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/misc.tsx25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/utils/misc.tsx b/src/utils/misc.tsx
index ded052b..aca7661 100644
--- a/src/utils/misc.tsx
+++ b/src/utils/misc.tsx
@@ -1,4 +1,4 @@
-import { React } from "../webpack";
+import { React } from "../webpack/common";
/**
* Makes a lazy function. On first call, the value is computed.
@@ -16,17 +16,28 @@ export function lazy<T>(factory: () => T): () => T {
* Await a promise
* @param factory Factory
* @param fallbackValue The fallback value that will be used until the promise resolved
- * @returns A state that will either be null or the result of the promise
+ * @returns [value, error, isPending]
*/
-export function useAwaiter<T>(factory: () => Promise<T>, fallbackValue: T | null = null): T | null {
- const [res, setRes] = React.useState<T | null>(fallbackValue);
+export function useAwaiter<T>(factory: () => Promise<T>): [T | null, any, boolean];
+export function useAwaiter<T>(factory: () => Promise<T>, fallbackValue: T): [T, any, boolean];
+export function useAwaiter<T>(factory: () => Promise<T>, fallbackValue: T | null = null): [T | null, any, boolean] {
+ const [state, setState] = React.useState({
+ value: fallbackValue,
+ error: null as any,
+ pending: true
+ });
React.useEffect(() => {
- factory().then(setRes);
+ let isAlive = true;
+ factory()
+ .then(value => isAlive && setState({ value, error: null, pending: false }))
+ .catch(error => isAlive && setState({ value: null, error, pending: false }));
+
+ return () => void (isAlive = false);
}, []);
- return res;
-}
+ return [state.value, state.error, state.pending];
+};
/**
* A lazy component. The factory method is called on first render. For example useful