Make it possible to destructure lazy webpack finds

This commit is contained in:
V 2023-11-22 06:48:59 +01:00 committed by Nuckyz
parent ffe6512693
commit b21b6d7e5d
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
4 changed files with 36 additions and 18 deletions

View file

@ -43,7 +43,6 @@ for (const method of [
"construct",
"defineProperty",
"deleteProperty",
"get",
"getOwnPropertyDescriptor",
"getPrototypeOf",
"has",
@ -86,7 +85,11 @@ handler.getOwnPropertyDescriptor = (target, p) => {
* Note that the example below exists already as an api, see {@link findByPropsLazy}
* @example const mod = proxyLazy(() => findByProps("blah")); console.log(mod.blah);
*/
export function proxyLazy<T>(factory: () => T, attempts = 5): T {
export function proxyLazy<T>(factory: () => T, attempts = 5, isChild = false): T {
let isSameTick = true;
if (!isChild)
setTimeout(() => isSameTick = false, 0);
let tries = 0;
const proxyDummy = Object.assign(function () { }, {
[kCACHE]: void 0 as T | undefined,
@ -100,5 +103,21 @@ export function proxyLazy<T>(factory: () => T, attempts = 5): T {
}
});
return new Proxy(proxyDummy, handler) as any;
return new Proxy(proxyDummy, {
...handler,
get(target, p, receiver) {
// if we're still in the same tick, it means the lazy was immediately used.
// thus, we lazy proxy the get access to make things like destructuring work as expected
// meow here will also be a lazy
// `const { meow } = findByPropsLazy("meow");`
if (!isChild && isSameTick)
return proxyLazy(
() => Reflect.get(target[kGET](), p, receiver),
attempts,
true
);
return Reflect.get(target[kGET](), p, receiver);
}
}) as any;
}