diff options
author | Justice Almanzar <superdash993@gmail.com> | 2023-02-09 15:21:14 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-09 21:21:14 +0100 |
commit | 6114bc6b168607613d06ae8fb4f29e74c763a417 (patch) | |
tree | 334bacfef5c30f059a6c6ee0af194707b1dd3d1d | |
parent | ae98401bd350bd327142d2d07444ce62cce9238c (diff) | |
download | Vencord-6114bc6b168607613d06ae8fb4f29e74c763a417.tar.gz Vencord-6114bc6b168607613d06ae8fb4f29e74c763a417.tar.bz2 Vencord-6114bc6b168607613d06ae8fb4f29e74c763a417.zip |
make proxies enumerable (#476)
Co-authored-by: Ven <vendicated@riseup.net>
-rw-r--r-- | src/utils/proxyLazy.ts | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/utils/proxyLazy.ts b/src/utils/proxyLazy.ts index 42b5a91..b1fca6e 100644 --- a/src/utils/proxyLazy.ts +++ b/src/utils/proxyLazy.ts @@ -22,6 +22,9 @@ 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"); + for (const method of [ "apply", "construct", @@ -38,11 +41,11 @@ for (const method of [ "setPrototypeOf" ]) { handler[method] = - (target: any, ...args: any[]) => Reflect[method](target.get(), ...args); + (target: any, ...args: any[]) => Reflect[method](target[GET_KEY](), ...args); } handler.ownKeys = target => { - const v = target.get(); + const v = target[GET_KEY](); const keys = Reflect.ownKeys(v); for (const key of unconfigurable) { if (!keys.includes(key)) keys.push(key); @@ -54,7 +57,10 @@ handler.getOwnPropertyDescriptor = (target, p) => { if (typeof p === "string" && unconfigurable.includes(p)) return Reflect.getOwnPropertyDescriptor(target, p); - return Reflect.getOwnPropertyDescriptor(target.get(), p); + const descriptor = Reflect.getOwnPropertyDescriptor(target[GET_KEY](), p); + + if (descriptor) Object.defineProperty(target, p, descriptor); + return descriptor; }; /** @@ -67,10 +73,10 @@ handler.getOwnPropertyDescriptor = (target, p) => { * @example const mod = proxyLazy(() => findByProps("blah")); console.log(mod.blah); */ export function proxyLazy<T>(factory: () => T): T { - const proxyDummy: { (): void; cachedValue?: T; get(): T; } = function () { }; - - proxyDummy.cachedValue = void 0; - proxyDummy.get = () => proxyDummy.cachedValue ??= factory(); + const proxyDummy: { (): void; [CACHED_KEY]?: T; [GET_KEY](): T; } = Object.assign(function () { }, { + [CACHED_KEY]: void 0, + [GET_KEY]: () => proxyDummy[CACHED_KEY] ??= factory(), + }); return new Proxy(proxyDummy, handler) as any; } |