Properly set receiver

This commit is contained in:
Nuckyz 2024-05-23 22:51:23 -03:00
parent 6f25005057
commit 90c26432f5
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
2 changed files with 8 additions and 6 deletions

View file

@ -43,6 +43,7 @@ const handler: ProxyHandler<any> = {
...Object.fromEntries(Object.getOwnPropertyNames(Reflect).map(propName =>
[propName, (target: any, ...args: any[]) => Reflect[propName](target[proxyLazyGet](), ...args)]
)),
set: (target, p, newValue) => Reflect.set(target[proxyLazyGet](), p, newValue, target[proxyLazyGet]()),
ownKeys: target => {
const keys = Reflect.ownKeys(target[proxyLazyGet]());
for (const key of unconfigurable) {
@ -97,7 +98,7 @@ export function proxyLazy<T = AnyObject>(factory: () => T, attempts = 5, isChild
const proxy = new Proxy(proxyDummy, {
...handler,
get(target, p, receiver) {
get(target, p) {
if (p === proxyLazyGet) return target[proxyLazyGet];
if (p === proxyLazyCache) return target[proxyLazyCache];
@ -107,7 +108,7 @@ export function proxyLazy<T = AnyObject>(factory: () => T, attempts = 5, isChild
// `const { meow } = proxyLazy(() => ({ meow: [] }));`
if (!isChild && isSameTick) {
return proxyLazy(
() => Reflect.get(target[proxyLazyGet](), p, receiver),
() => Reflect.get(target[proxyLazyGet](), p, target[proxyLazyGet]()),
attempts,
true
);
@ -115,7 +116,7 @@ export function proxyLazy<T = AnyObject>(factory: () => T, attempts = 5, isChild
const lazyTarget = target[proxyLazyGet]();
if (typeof lazyTarget === "object" || typeof lazyTarget === "function") {
return Reflect.get(lazyTarget, p, receiver);
return Reflect.get(lazyTarget, p, lazyTarget);
}
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.");

View file

@ -22,6 +22,7 @@ const handler: ProxyHandler<any> = {
...Object.fromEntries(Object.getOwnPropertyNames(Reflect).map(propName =>
[propName, (target: any, ...args: any[]) => Reflect[propName](target[proxyInnerGet](), ...args)]
)),
set: (target, p, value) => Reflect.set(target[proxyInnerGet](), p, value, target[proxyInnerGet]()),
ownKeys: target => {
const keys = Reflect.ownKeys(target[proxyInnerGet]());
for (const key of unconfigurable) {
@ -67,7 +68,7 @@ export function proxyInner<T = AnyObject>(
const proxy = new Proxy(proxyDummy, {
...handler,
get(target, p, receiver) {
get(target, p) {
if (p === proxyInnerValue) return target[proxyInnerValue];
if (p === proxyInnerGet) return target[proxyInnerGet];
@ -80,7 +81,7 @@ export function proxyInner<T = AnyObject>(
recursiveSetInnerValues.push((innerValue: T) => {
// Set the inner value of the destructured value as the prop value p of the parent
recursiveSetInnerValue(Reflect.get(innerValue as object, p, receiver));
recursiveSetInnerValue(Reflect.get(innerValue as object, p, innerValue));
});
return recursiveProxy;
@ -88,7 +89,7 @@ export function proxyInner<T = AnyObject>(
const innerTarget = target[proxyInnerGet]();
if (typeof innerTarget === "object" || typeof innerTarget === "function") {
return Reflect.get(innerTarget, p, receiver);
return Reflect.get(innerTarget, p, innerTarget);
}
throw new Error(primitiveErrMsg);