diff --git a/src/utils/lazy.ts b/src/utils/lazy.ts index 92c9c32d6..899b05f1c 100644 --- a/src/utils/lazy.ts +++ b/src/utils/lazy.ts @@ -83,6 +83,10 @@ export function proxyLazy(factory: () => T, attempts = 5, isChild if (!proxyDummy[proxyLazyCache]) { throw new Error(`proxyLazy factory failed:\n\n${factory}`); + } else { + if (typeof proxyDummy[proxyLazyCache] === "function") { + proxy.toString = proxyDummy[proxyLazyCache].toString.bind(proxyDummy[proxyLazyCache]); + } } } @@ -91,7 +95,7 @@ export function proxyLazy(factory: () => T, attempts = 5, isChild [proxyLazyCache]: void 0 as T | undefined }); - return new Proxy(proxyDummy, { + const proxy = new Proxy(proxyDummy, { ...handler, get(target, p, receiver) { if (p === proxyLazyGet) return target[proxyLazyGet]; @@ -117,4 +121,6 @@ export function proxyLazy(factory: () => T, attempts = 5, isChild 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."); } }); + + return proxy; } diff --git a/src/utils/proxyInner.ts b/src/utils/proxyInner.ts index f78aaf32f..96d2f4262 100644 --- a/src/utils/proxyInner.ts +++ b/src/utils/proxyInner.ts @@ -65,17 +65,7 @@ export function proxyInner( [proxyInnerValue]: void 0 as T | undefined }); - // Values destructured in the same tick the proxy was created will push their setInnerValue here - const recursiveSetInnerValues = [] as Array<(innerValue: T) => void>; - - // Once we set the parent inner value, we will call the setInnerValue functions of the destructured values, - // for them to get the proper value from the parent and use as their inner instead - function setInnerValue(innerValue: T) { - proxyDummy[proxyInnerValue] = innerValue; - recursiveSetInnerValues.forEach(setInnerValue => setInnerValue(innerValue)); - } - - return [new Proxy(proxyDummy, { + const proxy = new Proxy(proxyDummy, { ...handler, get(target, p, receiver) { if (p === proxyInnerValue) return target[proxyInnerValue]; @@ -104,5 +94,21 @@ export function proxyInner( throw new Error(primitiveErrMsg); } - }), setInnerValue]; + }); + + // Values destructured in the same tick the proxy was created will push their setInnerValue here + const recursiveSetInnerValues = [] as Array<(innerValue: T) => void>; + + // Once we set the parent inner value, we will call the setInnerValue functions of the destructured values, + // for them to get the proper value from the parent and use as their inner instead + function setInnerValue(innerValue: T) { + proxyDummy[proxyInnerValue] = innerValue; + recursiveSetInnerValues.forEach(setInnerValue => setInnerValue(innerValue)); + + if (typeof innerValue === "function") { + proxy.toString = innerValue.toString.bind(innerValue); + } + } + + return [proxy, setInnerValue]; }