From 70103e81119ad41f1e26d384ed0f60a7ae14e241 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Sat, 4 May 2024 17:49:20 -0300 Subject: [PATCH] Add better explanation for destructuring primitives --- src/utils/lazy.ts | 2 +- src/utils/proxyInner.ts | 15 ++++++++++----- src/webpack/webpack.tsx | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/utils/lazy.ts b/src/utils/lazy.ts index 5fa1a8689..97e6f3e81 100644 --- a/src/utils/lazy.ts +++ b/src/utils/lazy.ts @@ -96,7 +96,7 @@ export function proxyLazy(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; } diff --git a/src/utils/proxyInner.ts b/src/utils/proxyInner.ts index ef41f1fe0..698363c71 100644 --- a/src/utils/proxyInner.ts +++ b/src/utils/proxyInner.ts @@ -35,17 +35,22 @@ const handler: ProxyHandler = { /** * 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(err = new Error("Proxy inner value is undefined, setInnerValue was never called."), isChild = false): [proxy: T, setInnerValue: (innerValue: T) => void] { +export function proxyInner( + 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(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(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]; diff --git a/src/webpack/webpack.tsx b/src/webpack/webpack.tsx index b09ba456e..23b3e0992 100644 --- a/src/webpack/webpack.tsx +++ b/src/webpack/webpack.tsx @@ -167,7 +167,7 @@ export function find(filter: FilterFn, callback: (mod: any) => any = m if (IS_DEV && !isIndirect) webpackSearchHistory.push(["find", [filter]]); - const [proxy, setInnerValue] = proxyInner(new Error(`Webpack find matched no module. Filter: ${printFilter(filter)}`)); + const [proxy, setInnerValue] = proxyInner(`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;