aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorV <vendicated@riseup.net>2023-07-06 00:53:43 +0200
committerV <vendicated@riseup.net>2023-07-06 00:54:49 +0200
commitcb5f23d9b50afc8c89c4aaa6ddc315d7591760a3 (patch)
tree1823433a2625130a1812396f77df7c2d903fe1d1 /src/utils
parentcd2cbfa0efe3ccb5e350d9df546bc62803588b44 (diff)
downloadVencord-cb5f23d9b50afc8c89c4aaa6ddc315d7591760a3.tar.gz
Vencord-cb5f23d9b50afc8c89c4aaa6ddc315d7591760a3.tar.bz2
Vencord-cb5f23d9b50afc8c89c4aaa6ddc315d7591760a3.zip
ProxyLazy: Limit attempts
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/lazy.ts25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/utils/lazy.ts b/src/utils/lazy.ts
index 1e1dd5f..55aae5e 100644
--- a/src/utils/lazy.ts
+++ b/src/utils/lazy.ts
@@ -27,8 +27,8 @@ const unconfigurable = ["arguments", "caller", "prototype"];
const handler: ProxyHandler<any> = {};
-const GET_KEY = Symbol.for("vencord.lazy.get");
-const CACHED_KEY = Symbol.for("vencord.lazy.cached");
+const kGET = Symbol.for("vencord.lazy.get");
+const kCACHE = Symbol.for("vencord.lazy.cached");
for (const method of [
"apply",
@@ -46,11 +46,11 @@ for (const method of [
"setPrototypeOf"
]) {
handler[method] =
- (target: any, ...args: any[]) => Reflect[method](target[GET_KEY](), ...args);
+ (target: any, ...args: any[]) => Reflect[method](target[kGET](), ...args);
}
handler.ownKeys = target => {
- const v = target[GET_KEY]();
+ const v = target[kGET]();
const keys = Reflect.ownKeys(v);
for (const key of unconfigurable) {
if (!keys.includes(key)) keys.push(key);
@@ -62,7 +62,7 @@ handler.getOwnPropertyDescriptor = (target, p) => {
if (typeof p === "string" && unconfigurable.includes(p))
return Reflect.getOwnPropertyDescriptor(target, p);
- const descriptor = Reflect.getOwnPropertyDescriptor(target[GET_KEY](), p);
+ const descriptor = Reflect.getOwnPropertyDescriptor(target[kGET](), p);
if (descriptor) Object.defineProperty(target, p, descriptor);
return descriptor;
@@ -72,15 +72,22 @@ handler.getOwnPropertyDescriptor = (target, p) => {
* Wraps the result of {@see makeLazy} in a Proxy you can consume as if it wasn't lazy.
* On first property access, the lazy is evaluated
* @param factory lazy factory
+ * @param attempts how many times to try to evaluate the lazy before giving up
* @returns Proxy
*
* Note that the example below exists already as an api, see {@link findByPropsLazy}
* @example const mod = proxyLazy(() => findByProps("blah")); console.log(mod.blah);
*/
-export function proxyLazy<T>(factory: () => T): T {
- const proxyDummy: { (): void;[CACHED_KEY]?: T;[GET_KEY](): T; } = Object.assign(function () { }, {
- [CACHED_KEY]: void 0,
- [GET_KEY]: () => proxyDummy[CACHED_KEY] ??= factory(),
+export function proxyLazy<T>(factory: () => T, attempts = 5): T {
+ let tries = 0;
+ const proxyDummy = Object.assign(function () { }, {
+ [kCACHE]: void 0 as T | undefined,
+ [kGET]() {
+ if (!proxyDummy[kCACHE] && attempts > tries++) {
+ proxyDummy[kCACHE] = factory();
+ }
+ return proxyDummy[kCACHE];
+ }
});
return new Proxy(proxyDummy, handler) as any;