Properly set receiver
This commit is contained in:
parent
6f25005057
commit
90c26432f5
|
@ -43,6 +43,7 @@ const handler: ProxyHandler<any> = {
|
||||||
...Object.fromEntries(Object.getOwnPropertyNames(Reflect).map(propName =>
|
...Object.fromEntries(Object.getOwnPropertyNames(Reflect).map(propName =>
|
||||||
[propName, (target: any, ...args: any[]) => Reflect[propName](target[proxyLazyGet](), ...args)]
|
[propName, (target: any, ...args: any[]) => Reflect[propName](target[proxyLazyGet](), ...args)]
|
||||||
)),
|
)),
|
||||||
|
set: (target, p, newValue) => Reflect.set(target[proxyLazyGet](), p, newValue, target[proxyLazyGet]()),
|
||||||
ownKeys: target => {
|
ownKeys: target => {
|
||||||
const keys = Reflect.ownKeys(target[proxyLazyGet]());
|
const keys = Reflect.ownKeys(target[proxyLazyGet]());
|
||||||
for (const key of unconfigurable) {
|
for (const key of unconfigurable) {
|
||||||
|
@ -97,7 +98,7 @@ export function proxyLazy<T = AnyObject>(factory: () => T, attempts = 5, isChild
|
||||||
|
|
||||||
const proxy = new Proxy(proxyDummy, {
|
const proxy = new Proxy(proxyDummy, {
|
||||||
...handler,
|
...handler,
|
||||||
get(target, p, receiver) {
|
get(target, p) {
|
||||||
if (p === proxyLazyGet) return target[proxyLazyGet];
|
if (p === proxyLazyGet) return target[proxyLazyGet];
|
||||||
if (p === proxyLazyCache) return target[proxyLazyCache];
|
if (p === proxyLazyCache) return target[proxyLazyCache];
|
||||||
|
|
||||||
|
@ -107,7 +108,7 @@ export function proxyLazy<T = AnyObject>(factory: () => T, attempts = 5, isChild
|
||||||
// `const { meow } = proxyLazy(() => ({ meow: [] }));`
|
// `const { meow } = proxyLazy(() => ({ meow: [] }));`
|
||||||
if (!isChild && isSameTick) {
|
if (!isChild && isSameTick) {
|
||||||
return proxyLazy(
|
return proxyLazy(
|
||||||
() => Reflect.get(target[proxyLazyGet](), p, receiver),
|
() => Reflect.get(target[proxyLazyGet](), p, target[proxyLazyGet]()),
|
||||||
attempts,
|
attempts,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
@ -115,7 +116,7 @@ export function proxyLazy<T = AnyObject>(factory: () => T, attempts = 5, isChild
|
||||||
|
|
||||||
const lazyTarget = target[proxyLazyGet]();
|
const lazyTarget = target[proxyLazyGet]();
|
||||||
if (typeof lazyTarget === "object" || typeof lazyTarget === "function") {
|
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.");
|
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.");
|
||||||
|
|
|
@ -22,6 +22,7 @@ const handler: ProxyHandler<any> = {
|
||||||
...Object.fromEntries(Object.getOwnPropertyNames(Reflect).map(propName =>
|
...Object.fromEntries(Object.getOwnPropertyNames(Reflect).map(propName =>
|
||||||
[propName, (target: any, ...args: any[]) => Reflect[propName](target[proxyInnerGet](), ...args)]
|
[propName, (target: any, ...args: any[]) => Reflect[propName](target[proxyInnerGet](), ...args)]
|
||||||
)),
|
)),
|
||||||
|
set: (target, p, value) => Reflect.set(target[proxyInnerGet](), p, value, target[proxyInnerGet]()),
|
||||||
ownKeys: target => {
|
ownKeys: target => {
|
||||||
const keys = Reflect.ownKeys(target[proxyInnerGet]());
|
const keys = Reflect.ownKeys(target[proxyInnerGet]());
|
||||||
for (const key of unconfigurable) {
|
for (const key of unconfigurable) {
|
||||||
|
@ -67,7 +68,7 @@ export function proxyInner<T = AnyObject>(
|
||||||
|
|
||||||
const proxy = new Proxy(proxyDummy, {
|
const proxy = new Proxy(proxyDummy, {
|
||||||
...handler,
|
...handler,
|
||||||
get(target, p, receiver) {
|
get(target, p) {
|
||||||
if (p === proxyInnerValue) return target[proxyInnerValue];
|
if (p === proxyInnerValue) return target[proxyInnerValue];
|
||||||
if (p === proxyInnerGet) return target[proxyInnerGet];
|
if (p === proxyInnerGet) return target[proxyInnerGet];
|
||||||
|
|
||||||
|
@ -80,7 +81,7 @@ export function proxyInner<T = AnyObject>(
|
||||||
|
|
||||||
recursiveSetInnerValues.push((innerValue: T) => {
|
recursiveSetInnerValues.push((innerValue: T) => {
|
||||||
// Set the inner value of the destructured value as the prop value p of the parent
|
// 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;
|
return recursiveProxy;
|
||||||
|
@ -88,7 +89,7 @@ export function proxyInner<T = AnyObject>(
|
||||||
|
|
||||||
const innerTarget = target[proxyInnerGet]();
|
const innerTarget = target[proxyInnerGet]();
|
||||||
if (typeof innerTarget === "object" || typeof innerTarget === "function") {
|
if (typeof innerTarget === "object" || typeof innerTarget === "function") {
|
||||||
return Reflect.get(innerTarget, p, receiver);
|
return Reflect.get(innerTarget, p, innerTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(primitiveErrMsg);
|
throw new Error(primitiveErrMsg);
|
||||||
|
|
Loading…
Reference in a new issue