Skip to content

Commit

Permalink
fix(engine): [214] no longer sharing shadow targets (#443) (#446)
Browse files Browse the repository at this point in the history
* fix(engine): no longer sharing shadow targets

* fix(engine): removing readonly to reactive test
  • Loading branch information
davidturissini authored and diervo committed Jun 27, 2018
1 parent 773ce87 commit 119df9c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -711,4 +711,4 @@ describe('ReactiveHandler', () => {
expect(accessSpy).toHaveBeenCalledTimes(2);
expect(accessSpy).toHaveBeenLastCalledWith(obj.foo, 'bar');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,23 @@ describe('ReadOnlyHandler', () => {
writeAndRead.x.foo = 'baz';
}).toThrow();
});

it('should not throw when frozen reactive proxy is converted to read only', () => {
const value = {};
const object = {
foo: value,
};

const membrane = new ReactiveMembrane((value) => value, {
propertyMemberChange: () => {},
propertyMemberAccess: () => {},
});

const reactive = membrane.getProxy(object);
Object.freeze(reactive);
const readOnly = membrane.getReadOnlyProxy(reactive);
expect(() => {
readOnly.foo;
}).not.toThrow();
});
});
14 changes: 2 additions & 12 deletions packages/observable-membrane/src/reactive-membrane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,19 @@ function getReactiveState(membrane: ReactiveMembrane, value: any): ReactiveState
}

reactiveState = ObjectDefineProperties(ObjectCreate(null), {
shadowTarget: {
get(this: ReactiveState) {
const shadowTarget = createShadowTarget(value);
ObjectDefineProperty(this, 'shadowTarget', { value: shadowTarget });
return shadowTarget;
},
configurable: true,
},
reactive: {
get(this: ReactiveState) {
const { shadowTarget } = this;
const reactiveHandler = new ReactiveProxyHandler(membrane, value);
const proxy = new Proxy(shadowTarget, reactiveHandler);
const proxy = new Proxy(createShadowTarget(value), reactiveHandler);
ObjectDefineProperty(this, 'reactive', { value: proxy });
return proxy;
},
configurable: true,
},
readOnly: {
get(this: ReactiveState) {
const { shadowTarget } = this;
const readOnlyHandler = new ReadOnlyHandler(membrane, value);
const proxy = new Proxy(shadowTarget, readOnlyHandler);
const proxy = new Proxy(createShadowTarget(value), readOnlyHandler);
ObjectDefineProperty(this, 'readOnly', { value: proxy });
return proxy;
},
Expand Down

0 comments on commit 119df9c

Please sign in to comment.