From cfefdbc7a7c72ebde860f78fc5f7520d7f76a50b Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Sat, 4 May 2024 20:56:32 -0300 Subject: [PATCH] Improve proxyLazy --- src/utils/lazy.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/utils/lazy.ts b/src/utils/lazy.ts index 97e6f3e81..4a8557d0f 100644 --- a/src/utils/lazy.ts +++ b/src/utils/lazy.ts @@ -7,15 +7,20 @@ export function makeLazy(factory: () => T, attempts = 5): () => T { let tries = 0; let cache: T; - return () => { + + const getter = () => { if (!cache && attempts > tries++) { cache = factory(); if (!cache && attempts === tries) { - console.error(`Lazy factory failed:\n${factory}`); + console.error(`Lazy factory failed:\n\n${factory}`); } } return cache; }; + + getter.$$vencordLazyFailed = () => tries >= attempts; + + return getter; } // Proxies demand that these properties be unmodified, so proxyLazy @@ -54,20 +59,20 @@ const proxyLazyCache = Symbol.for("vencord.lazy.cached"); * @returns Result of factory function */ export function proxyLazy(factory: () => T, attempts = 5, isChild = false): T { - const get = makeLazy(factory, attempts); + const get = makeLazy(factory, attempts) as any; let isSameTick = true; if (!isChild) setTimeout(() => isSameTick = false, 0); - let failed = false; const proxyDummy = Object.assign(function () { }, { [proxyLazyGet]() { - if (!proxyDummy[proxyLazyCache] && !failed) { - proxyDummy[proxyLazyCache] = get(); + if (!proxyDummy[proxyLazyCache]) { + if (!get.$$vencordLazyFailed()) { + proxyDummy[proxyLazyCache] = get(); + } if (!proxyDummy[proxyLazyCache]) { - failed = true; - throw new Error(`proxyLazy factory failed:\n${factory}`); + throw new Error(`proxyLazy factory failed:\n\n${factory}`); } }