Skip to content

Commit

Permalink
fix: prevent array splice notification on non-mutating splice() calls (
Browse files Browse the repository at this point in the history
…#6786)

* always project splices

* Change files

* Update packages/web-components/fast-element/src/observation/arrays.spec.ts

---------

Co-authored-by: nicholasrice <[email protected]>
  • Loading branch information
nicholasrice and nicholasrice authored Jul 19, 2023
1 parent 57f3c22 commit ff0b93a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Prevent notification of array splices when operation does not mutate array values",
"packageName": "@microsoft/fast-element",
"email": "[email protected]",
"dependentChangeType": "prerelease"
}
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,25 @@ describe("The ArrayObserver", () => {

expect(wasCalled).to.be.false;
})

it("should not deliver splices for .splice() when .splice() does not change the items in the array", async () => {
ArrayObserver.enable();
const array = [1,2,3,4,5];
const observer = Observable.getNotifier(array);
let splices;

observer.subscribe({
handleChange(source, args) {
splices = args
}
});

array.splice(0, array.length, ...array);

await Updates.next();

expect(splices.length).to.equal(0);
})
});

describe("The array length observer", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ let defaultSpliceStrategy: SpliceStrategy = Object.freeze({
if (changes === void 0) {
return emptyArray;
}
return changes.length > 1 ? project(current, changes) : changes;
return project(current, changes);
}

return resetSplices;
Expand Down

0 comments on commit ff0b93a

Please sign in to comment.