aboutsummaryrefslogtreecommitdiff
path: root/src/utils/proxyLazy.ts
blob: a5c0835aec1f1854305585c2710c53542eb8da90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { makeLazy } from "./misc";

const ProxyDummy = function () { };

/**
 * 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(() => findByProps("blah")); console.log(mod.blah);
 */
export function proxyLazy<T>(factory: () => T): T {
    const lazy = makeLazy(factory);

    return new Proxy(ProxyDummy, {
        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) => Reflect.construct(lazy() as Function, args),
        deleteProperty: (_, prop) => delete lazy()[prop],
        defineProperty: (_, property, attributes) => !!Object.defineProperty(lazy(), property, attributes),
        getPrototypeOf: () => Object.getPrototypeOf(lazy())
    }) as any as T;
}