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

Fixed incorrect changed path when mutating unrelated area of proxy during iteration operations on cloned areas of the proxy. #105

Merged
Merged
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const onChange = (object, onChange, options = {}) => {

// eslint-disable-next-line max-params
const handleChange = (changePath, property, value, previous, applyData) => {
if (smartClone.isCloning) {
if (smartClone.isCloning && smartClone.isPartOfClone(changePath)) {
smartClone.update(changePath, property, previous);
} else {
onChange(path.concat(changePath, property), value, previous, applyData);
Expand Down
37 changes: 37 additions & 0 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,43 @@ const path = {

return object;
},
isSubPath(path, subPath) {
if (isArray(path)) {
if (path.length < subPath.length) {
return false;
}

// eslint-disable-next-line unicorn/no-for-loop
for (let i = 0; i < subPath.length; i++) {
if (path[i] !== subPath[i]) {
return false;
}
}

return true;
}

if (path.length < subPath.length) {
return false;
}

if (path === subPath) {
return true;
}

if (path.startsWith(subPath)) {
return path[subPath.length] === PATH_SEPARATOR;
}

return false;
},
isRootPath(path) {
if (isArray(path)) {
return path.length === 0;
}

return path === '';
},
};

export default path;
4 changes: 4 additions & 0 deletions lib/smart-clone/clone/clone-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,8 @@ export default class CloneObject {
? this._isChanged
: this._onIsChanged(this.clone, value);
}

isPathApplicable(changePath) {
return path.isRootPath(this._path) || path.isSubPath(changePath, this._path);
}
}
4 changes: 4 additions & 0 deletions lib/smart-clone/smart-clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export default class SmartClone {
return this._stack.at(-1).isChanged(isMutable, value, equals);
}

isPartOfClone(changePath) {
return this._stack.at(-1).isPathApplicable(changePath);
}

undo(object) {
if (this._previousClone !== undefined) {
this._previousClone.undo(object);
Expand Down
52 changes: 52 additions & 0 deletions tests/on-change.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,55 @@ test('array path should be the shorter one in the same object for circular refer
t.is(resultPath[1], '2');
t.is(resultPath[2], 'value');
});

test('should trigger when methods are called that mutate unrelated area of proxy when pathAsArray is false', t => {
const object = {
a: [
{
quantity: 1,
},
],
b: {
c: {
quantity: 8,
},
},
};

testRunner(t, object, {pathAsArray: false}, (proxy, verify) => {
// eslint-disable-next-line unicorn/no-array-for-each
proxy.a.forEach(() => {
proxy.b.c = {
quantity: 3,
};
});

verify(1, proxy, 'b.c', {quantity: 3}, {quantity: 8});
});
});

test('should trigger when methods are called that mutate unrelated area of proxy when pathAsArray is true', t => {
const object = {
a: [
{
quantity: 1,
},
],
b: {
c: {
quantity: 8,
},
},
};

testRunner(t, object, {pathAsArray: true}, (proxy, verify) => {
// eslint-disable-next-line unicorn/no-array-for-each
proxy.a.forEach(() => {
proxy.b.c = {
quantity: 3,
};
});

verify(1, proxy, ['b', 'c'], {quantity: 3}, {quantity: 8});
});
});