ProxyLazy: Limit attempts

This commit is contained in:
V 2023-07-06 00:53:43 +02:00
parent cd2cbfa0ef
commit cb5f23d9b5
No known key found for this signature in database
GPG Key ID: A1DC0CFB5615D905

@ -27,8 +27,8 @@ const unconfigurable = ["arguments", "caller", "prototype"];
const handler: ProxyHandler<any> = {}; const handler: ProxyHandler<any> = {};
const GET_KEY = Symbol.for("vencord.lazy.get"); const kGET = Symbol.for("vencord.lazy.get");
const CACHED_KEY = Symbol.for("vencord.lazy.cached"); const kCACHE = Symbol.for("vencord.lazy.cached");
for (const method of [ for (const method of [
"apply", "apply",
@ -46,11 +46,11 @@ for (const method of [
"setPrototypeOf" "setPrototypeOf"
]) { ]) {
handler[method] = handler[method] =
(target: any, ...args: any[]) => Reflect[method](target[GET_KEY](), ...args); (target: any, ...args: any[]) => Reflect[method](target[kGET](), ...args);
} }
handler.ownKeys = target => { handler.ownKeys = target => {
const v = target[GET_KEY](); const v = target[kGET]();
const keys = Reflect.ownKeys(v); const keys = Reflect.ownKeys(v);
for (const key of unconfigurable) { for (const key of unconfigurable) {
if (!keys.includes(key)) keys.push(key); if (!keys.includes(key)) keys.push(key);
@ -62,7 +62,7 @@ handler.getOwnPropertyDescriptor = (target, p) => {
if (typeof p === "string" && unconfigurable.includes(p)) if (typeof p === "string" && unconfigurable.includes(p))
return Reflect.getOwnPropertyDescriptor(target, 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); if (descriptor) Object.defineProperty(target, p, descriptor);
return 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. * 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 * On first property access, the lazy is evaluated
* @param factory lazy factory * @param factory lazy factory
* @param attempts how many times to try to evaluate the lazy before giving up
* @returns Proxy * @returns Proxy
* *
* Note that the example below exists already as an api, see {@link findByPropsLazy} * Note that the example below exists already as an api, see {@link findByPropsLazy}
* @example const mod = proxyLazy(() => findByProps("blah")); console.log(mod.blah); * @example const mod = proxyLazy(() => findByProps("blah")); console.log(mod.blah);
*/ */
export function proxyLazy<T>(factory: () => T): T { export function proxyLazy<T>(factory: () => T, attempts = 5): T {
const proxyDummy: { (): void;[CACHED_KEY]?: T;[GET_KEY](): T; } = Object.assign(function () { }, { let tries = 0;
[CACHED_KEY]: void 0, const proxyDummy = Object.assign(function () { }, {
[GET_KEY]: () => proxyDummy[CACHED_KEY] ??= factory(), [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; return new Proxy(proxyDummy, handler) as any;