Skip to content

Commit

Permalink
handle external proxification (#53)
Browse files Browse the repository at this point in the history
skip generation of a replace operation, if an object within the tree is replaced with a proxified version of itself by external code
  • Loading branch information
warpech committed Jun 6, 2019
1 parent edb513a commit c461b49
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/jsonpatcherproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ const JSONPatcherProxy = (function() {
if (isObject(newValue)) {
const subtreeMetadata = newValue[instance._metadataSymbol];
if (subtreeMetadata) {
if(subtreeMetadata.parent === tree && subtreeMetadata.keyInParent === key) {
//this is the same object that we already proxified, proxified now by someone else. In this case, remain silent
return Reflect.set(tree, key, newValue);
}
subtreeMetadata.parent = tree;
subtreeMetadata.keyInParent = key;
/*
Expand Down
24 changes: 24 additions & 0 deletions test/spec/proxySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,30 @@ describe('proxy', function() {
obj2.phoneNumbers[1].number = '456';
expect(observedObj).toReallyEqual(obj2); //objects should be still the same
});

it('should generate replace (deep object, proxified)', function() {
const obj = generateDeepObjectFixture();
const jsonPatcherProxy = new JSONPatcherProxy(obj);
let observedObj = jsonPatcherProxy.observe(true);

//begin external proxification
observedObj = new Proxy(observedObj, {});
observedObj.phoneNumbers = new Proxy(observedObj.phoneNumbers, {});
observedObj.phoneNumbers[0] = new Proxy(observedObj.phoneNumbers[0], {});
observedObj.phoneNumbers[1] = new Proxy(observedObj.phoneNumbers[1], {});
//end external proxification

observedObj.phoneNumbers[0].number = '123';

const patches = jsonPatcherProxy.generate();
expect(patches).toReallyEqual([
{
op: 'replace',
path: '/phoneNumbers/0/number',
value: '123'
}
]);
});

it('should generate replace (changes in new array cell, primitive values)', function() {
var arr = [1];
Expand Down

0 comments on commit c461b49

Please sign in to comment.