From dd3904badd5fd06397336a7acf3ab4d173322552 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Fri, 21 Jun 2024 22:54:08 -0300 Subject: [PATCH] fix issues related to nested proxyInner --- src/utils/proxyInner.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/utils/proxyInner.ts b/src/utils/proxyInner.ts index e52bc970d..50fa26dc2 100644 --- a/src/utils/proxyInner.ts +++ b/src/utils/proxyInner.ts @@ -80,10 +80,13 @@ export function proxyInner( } // If we're still in the same tick, it means the proxy was immediately used. - // thus, we proxy the get access to make things like destructuring work as expected - // meow here will also be a proxy - // `const { meow } = findByProps("meow");` - if (!isChild && isSameTick) { + // And, if the inner value is still nullish, it means the proxy was used before setInnerValue was called. + // So, proxy the get access to make things like destructuring work as expected. + // We dont need to proxy if the inner value is available, and recursiveSetInnerValue won't ever be called anyways, + // because the top setInnerValue was called before we proxied the get access + // example here will also be a proxy: + // `const { example } = findByProps("example");` + if (isSameTick && proxyDummy[SYM_PROXY_INNER_VALUE] == null && !isChild) { const [recursiveProxy, recursiveSetInnerValue] = proxyInner(errMsg, primitiveErrMsg, true); recursiveSetInnerValues.push((innerValue: T) => { @@ -112,7 +115,9 @@ export function proxyInner( proxyDummy[SYM_PROXY_INNER_VALUE] = innerValue; recursiveSetInnerValues.forEach(setInnerValue => setInnerValue(innerValue)); - if (typeof innerValue === "function") { + // Avoid binding toString if the inner value is null. + // This can happen if we are setting the inner value as another instance of proxyInner, which will cause that proxy to instantly evaluate and throw an error + if (typeof innerValue === "function" && innerValue[SYM_PROXY_INNER_GET] == null) { proxy.toString = innerValue.toString.bind(innerValue); } }