Add better explanation for destructuring primitives

This commit is contained in:
Nuckyz 2024-05-04 17:49:20 -03:00
parent 240705652c
commit 70103e8111
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
3 changed files with 12 additions and 7 deletions

View file

@ -96,7 +96,7 @@ export function proxyLazy<T = any>(factory: () => T, attempts = 5, isChild = fal
return Reflect.get(lazyTarget, p, receiver);
}
throw new Error("proxyLazy called on a primitive value");
throw new Error("proxyLazy called on a primitive value. This can happen if you try to destructure a primitive at the same tick as the proxy was created.");
}
}) as T;
}

View file

@ -35,17 +35,22 @@ const handler: ProxyHandler<any> = {
/**
* A proxy which has an inner value that can be set later.
* When a property is accessed, the proxy looks for the property value in its inner value, and errors if it's not set.
* @param err The error to throw when the inner value is not set
* @param err The error message to throw when the inner value is not set
* @param primitiveErr The error message to throw when the inner value is a primitive
* @returns A proxy which will act like the inner value when accessed
*/
export function proxyInner<T = any>(err = new Error("Proxy inner value is undefined, setInnerValue was never called."), isChild = false): [proxy: T, setInnerValue: (innerValue: T) => void] {
export function proxyInner<T = any>(
errMsg = "Proxy inner value is undefined, setInnerValue was never called.",
primitiveErrMsg = "proxyInner called on a primitive value. This can happen if you try to destructure a primitive at the same tick as the proxy was created.",
isChild = false
): [proxy: T, setInnerValue: (innerValue: T) => void] {
let isSameTick = true;
if (!isChild) setTimeout(() => isSameTick = false, 0);
const proxyDummy = Object.assign(function () { }, {
[proxyInnerGet]: function () {
if (proxyDummy[proxyInnerValue] == null) {
throw err;
throw new Error(errMsg);
}
return proxyDummy[proxyInnerValue];
@ -74,7 +79,7 @@ export function proxyInner<T = any>(err = new Error("Proxy inner value is undefi
// meow here will also be a proxy
// `const { meow } = findByProps("meow");`
if (!isChild && isSameTick) {
const [recursiveProxy, recursiveSetInnerValue] = proxyInner(err, true);
const [recursiveProxy, recursiveSetInnerValue] = proxyInner(errMsg, primitiveErrMsg, true);
recursiveSetInnerValues.push((innerValue: T) => {
// Set the inner value of the destructured value as the prop value p of the parent
@ -89,7 +94,7 @@ export function proxyInner<T = any>(err = new Error("Proxy inner value is undefi
return Reflect.get(innerTarget, p, receiver);
}
throw new Error("proxyInner called on a primitive value");
throw new Error(primitiveErrMsg);
}
}) as T, setInnerValue];

View file

@ -167,7 +167,7 @@ export function find<T = any>(filter: FilterFn, callback: (mod: any) => any = m
if (IS_DEV && !isIndirect) webpackSearchHistory.push(["find", [filter]]);
const [proxy, setInnerValue] = proxyInner<T>(new Error(`Webpack find matched no module. Filter: ${printFilter(filter)}`));
const [proxy, setInnerValue] = proxyInner<T>(`Webpack find matched no module. Filter: ${printFilter(filter)}`, "Webpack find with proxy called on a primitive value. This can happen if you try to destructure a primitive in the top level definition of the find.");
waitFor(filter, mod => setInnerValue(callback(mod)), { isIndirect: true });
if (proxy[proxyInnerValue] != null) return proxy[proxyInnerValue] as T;