aboutsummaryrefslogtreecommitdiff
path: root/src/utils/proxyLazy.ts
diff options
context:
space:
mode:
authorVendicated <vendicated@riseup.net>2022-10-14 21:34:35 +0200
committerVendicated <vendicated@riseup.net>2022-10-14 21:34:35 +0200
commit296336535f6d23c586c7e83edf0420b58cbf011a (patch)
treeaa2a12bd071b36d44f43da992aa9fddfdd11bf50 /src/utils/proxyLazy.ts
parent563f2fb1dc2067345e4fbd71ce8da7d60d2928e1 (diff)
downloadVencord-296336535f6d23c586c7e83edf0420b58cbf011a.tar.gz
Vencord-296336535f6d23c586c7e83edf0420b58cbf011a.tar.bz2
Vencord-296336535f6d23c586c7e83edf0420b58cbf011a.zip
Fix modals, add wp.mapMangledModule
Diffstat (limited to 'src/utils/proxyLazy.ts')
-rw-r--r--src/utils/proxyLazy.ts25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/utils/proxyLazy.ts b/src/utils/proxyLazy.ts
new file mode 100644
index 0000000..0912403
--- /dev/null
+++ b/src/utils/proxyLazy.ts
@@ -0,0 +1,25 @@
+import { makeLazy } from "./misc";
+
+/**
+ * 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
+ * @returns Proxy
+ *
+ * Note that the example below exists already as an api, see {@link lazyWebpack}
+ * @example const mod = proxyLazy(makeLazy(() => findByProps("blah"))); console.log(mod.blah);
+ */
+export function proxyLazy<T>(factory: () => T): T {
+ const lazy = makeLazy(factory);
+
+ return new Proxy(() => null, {
+ get: (_, prop) => lazy()[prop],
+ set: (_, prop, value) => lazy()[prop] = value,
+ has: (_, prop) => prop in lazy(),
+ apply: (_, $this, args) => (lazy() as Function).apply($this, args),
+ ownKeys: () => Reflect.ownKeys(lazy() as object),
+ construct: (_, args, newTarget) => Reflect.construct(lazy() as Function, args, newTarget),
+ deleteProperty: (_, prop) => delete lazy()[prop],
+ defineProperty: (_, property, attributes) => !!Object.defineProperty(lazy(), property, attributes)
+ }) as any as T;
+}