Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue with array methods that re-assign indexes #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/reactive-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
toString,
isUndefined,
unwrap,
ArrayConcat,
isArray,
ObjectDefineProperty,
Expand Down Expand Up @@ -29,9 +28,9 @@ function wrapValue(membrane: ReactiveMembrane, value: any): any {
* We only need to unwrap if value is specified
* @param descriptor external descrpitor provided to define new property on original value
*/
function unwrapDescriptor(descriptor: PropertyDescriptor): PropertyDescriptor {
function unwrapDescriptor(membrane: ReactiveMembrane, descriptor: PropertyDescriptor): PropertyDescriptor {
if (hasOwnProperty.call(descriptor, 'value')) {
descriptor.value = unwrap(descriptor.value);
descriptor.value = membrane.unwrapProxy(descriptor.value);
}
return descriptor;
}
Expand Down Expand Up @@ -71,8 +70,10 @@ export class ReactiveProxyHandler {
return membrane.getProxy(value);
}
set(shadowTarget: ReactiveMembraneShadowTarget, key: PropertyKey, value: any): boolean {
const { originalTarget, membrane: { valueMutated } } = this;
const { originalTarget, membrane } = this;
const oldValue = originalTarget[key];
const { valueMutated } = membrane;
value = membrane.unwrapProxy(value);
if (oldValue !== value) {
originalTarget[key] = value;
valueMutated(originalTarget, key);
Expand Down Expand Up @@ -182,7 +183,7 @@ export class ReactiveProxyHandler {
const originalDescriptor = getOwnPropertyDescriptor(originalTarget, key) as PropertyDescriptor;
descriptor.value = originalDescriptor.value;
}
ObjectDefineProperty(originalTarget, key, unwrapDescriptor(descriptor));
ObjectDefineProperty(originalTarget, key, unwrapDescriptor(membrane, descriptor));
if (configurable === false) {
ObjectDefineProperty(shadowTarget, key, wrapDescriptor(membrane, descriptor, wrapValue));
}
Expand Down
8 changes: 7 additions & 1 deletion src/reactive-membrane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ export class ReactiveMembrane {
}

unwrapProxy(p: any) {
return unwrap(p);
const unwrapped = unwrap(p);
const distorted = this.valueDistortion(unwrapped);
const reactiveState = this.objectGraph.get(distorted);
if (reactiveState && reactiveState.readOnly === p) {
return p;
}
return distorted;
}

private getReactiveState(value: any, distortedValue: any): ReactiveState {
Expand Down