2022-10-21 19:17:06 -04:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-10-14 15:34:35 -04:00
|
|
|
import { makeLazy } from "./misc";
|
|
|
|
|
2022-10-14 16:38:36 -04:00
|
|
|
const ProxyDummy = function () { };
|
|
|
|
|
2022-10-14 15:34:35 -04:00
|
|
|
/**
|
|
|
|
* 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}
|
2022-10-14 15:40:10 -04:00
|
|
|
* @example const mod = proxyLazy(() => findByProps("blah")); console.log(mod.blah);
|
2022-10-14 15:34:35 -04:00
|
|
|
*/
|
|
|
|
export function proxyLazy<T>(factory: () => T): T {
|
|
|
|
const lazy = makeLazy(factory);
|
|
|
|
|
2022-10-14 16:38:36 -04:00
|
|
|
return new Proxy(ProxyDummy, {
|
2022-10-14 15:34:35 -04:00
|
|
|
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),
|
2022-10-14 16:38:36 -04:00
|
|
|
construct: (_, args) => Reflect.construct(lazy() as Function, args),
|
2022-10-14 15:34:35 -04:00
|
|
|
deleteProperty: (_, prop) => delete lazy()[prop],
|
2022-10-14 16:38:36 -04:00
|
|
|
defineProperty: (_, property, attributes) => !!Object.defineProperty(lazy(), property, attributes),
|
|
|
|
getPrototypeOf: () => Object.getPrototypeOf(lazy())
|
2022-10-14 15:34:35 -04:00
|
|
|
}) as any as T;
|
|
|
|
}
|