proxyLazy: Fix constructors

This commit is contained in:
Vendicated 2022-10-14 22:38:36 +02:00
parent 53794ec180
commit a4e98f9252
No known key found for this signature in database
GPG Key ID: EC781ADFB93EFFA3

@ -1,5 +1,7 @@
import { makeLazy } from "./misc"; 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. * 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
@ -12,14 +14,15 @@ import { makeLazy } from "./misc";
export function proxyLazy<T>(factory: () => T): T { export function proxyLazy<T>(factory: () => T): T {
const lazy = makeLazy(factory); const lazy = makeLazy(factory);
return new Proxy(() => null, { return new Proxy(ProxyDummy, {
get: (_, prop) => lazy()[prop], get: (_, prop) => lazy()[prop],
set: (_, prop, value) => lazy()[prop] = value, set: (_, prop, value) => lazy()[prop] = value,
has: (_, prop) => prop in lazy(), has: (_, prop) => prop in lazy(),
apply: (_, $this, args) => (lazy() as Function).apply($this, args), apply: (_, $this, args) => (lazy() as Function).apply($this, args),
ownKeys: () => Reflect.ownKeys(lazy() as object), ownKeys: () => Reflect.ownKeys(lazy() as object),
construct: (_, args, newTarget) => Reflect.construct(lazy() as Function, args, newTarget), construct: (_, args) => Reflect.construct(lazy() as Function, args),
deleteProperty: (_, prop) => delete lazy()[prop], deleteProperty: (_, prop) => delete lazy()[prop],
defineProperty: (_, property, attributes) => !!Object.defineProperty(lazy(), property, attributes) defineProperty: (_, property, attributes) => !!Object.defineProperty(lazy(), property, attributes),
getPrototypeOf: () => Object.getPrototypeOf(lazy())
}) as any as T; }) as any as T;
} }