aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorV <vendicated@riseup.net>2023-09-23 03:25:19 +0200
committerV <vendicated@riseup.net>2023-09-23 03:25:19 +0200
commit08c5d23636e89626454cd9a58a473c31a6721cb4 (patch)
treeb2c347fa581fbbcd14a0f69d8308bd8918a1b2b0 /src
parentac0f834155c9829949c698874ed3639697f52a5c (diff)
downloadVencord-08c5d23636e89626454cd9a58a473c31a6721cb4.tar.gz
Vencord-08c5d23636e89626454cd9a58a473c31a6721cb4.tar.bz2
Vencord-08c5d23636e89626454cd9a58a473c31a6721cb4.zip
add max attempts to lazys
Diffstat (limited to 'src')
-rw-r--r--src/utils/lazy.ts14
-rw-r--r--src/utils/react.tsx9
2 files changed, 18 insertions, 5 deletions
diff --git a/src/utils/lazy.ts b/src/utils/lazy.ts
index 55aae5e..4bac45b 100644
--- a/src/utils/lazy.ts
+++ b/src/utils/lazy.ts
@@ -16,9 +16,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-export function makeLazy<T>(factory: () => T): () => T {
+export function makeLazy<T>(factory: () => T, attempts = 5): () => T {
+ let tries = 0;
let cache: T;
- return () => cache ?? (cache = factory());
+ return () => {
+ if (!cache && attempts > tries++) {
+ cache = factory();
+ if (!cache && attempts === tries)
+ console.error("Lazy factory failed:", factory);
+ }
+ return cache;
+ };
}
// Proxies demand that these properties be unmodified, so proxyLazy
@@ -85,6 +93,8 @@ export function proxyLazy<T>(factory: () => T, attempts = 5): T {
[kGET]() {
if (!proxyDummy[kCACHE] && attempts > tries++) {
proxyDummy[kCACHE] = factory();
+ if (!proxyDummy[kCACHE] && attempts === tries)
+ console.error("Lazy factory failed:", factory);
}
return proxyDummy[kCACHE];
}
diff --git a/src/utils/react.tsx b/src/utils/react.tsx
index e8c1081..0181c95 100644
--- a/src/utils/react.tsx
+++ b/src/utils/react.tsx
@@ -21,6 +21,8 @@ import { React, useEffect, useMemo, useReducer, useState } from "@webpack/common
import { makeLazy } from "./lazy";
import { checkIntersecting } from "./misc";
+export const NoopComponent = () => null;
+
/**
* Check if an element is on screen
* @param intersectOnly If `true`, will only update the state when the element comes into view
@@ -125,13 +127,14 @@ export function useForceUpdater(withDep?: true) {
* A lazy component. The factory method is called on first render. For example useful
* for const Component = LazyComponent(() => findByDisplayName("...").default)
* @param factory Function returning a Component
+ * @param attempts How many times to try to get the component before giving up
* @returns Result of factory function
*/
-export function LazyComponent<T extends object = any>(factory: () => React.ComponentType<T>) {
- const get = makeLazy(factory);
+export function LazyComponent<T extends object = any>(factory: () => React.ComponentType<T>, attempts = 5) {
+ const get = makeLazy(factory, attempts);
return (props: T) => {
- const Component = get();
+ const Component = get() ?? NoopComponent;
return <Component {...props} />;
};
}