From ff0b93a7fb4ca416629f51cc233078494aaf5f60 Mon Sep 17 00:00:00 2001 From: Nicholas Rice <3213292+nicholasrice@users.noreply.github.com> Date: Wed, 19 Jul 2023 16:12:00 -0700 Subject: [PATCH] fix: prevent array splice notification on non-mutating splice() calls (#6786) * always project splices * Change files * Update packages/web-components/fast-element/src/observation/arrays.spec.ts --------- Co-authored-by: nicholasrice --- ...-44cfe95a-1f60-4e2e-a3f1-1d02aba63bab.json | 7 +++++++ .../src/observation/arrays.spec.ts | 19 +++++++++++++++++++ .../fast-element/src/observation/arrays.ts | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 change/@microsoft-fast-element-44cfe95a-1f60-4e2e-a3f1-1d02aba63bab.json diff --git a/change/@microsoft-fast-element-44cfe95a-1f60-4e2e-a3f1-1d02aba63bab.json b/change/@microsoft-fast-element-44cfe95a-1f60-4e2e-a3f1-1d02aba63bab.json new file mode 100644 index 00000000000..86d2fd7d98a --- /dev/null +++ b/change/@microsoft-fast-element-44cfe95a-1f60-4e2e-a3f1-1d02aba63bab.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Prevent notification of array splices when operation does not mutate array values", + "packageName": "@microsoft/fast-element", + "email": "nicholasrice@users.noreply.github.com", + "dependentChangeType": "prerelease" +} diff --git a/packages/web-components/fast-element/src/observation/arrays.spec.ts b/packages/web-components/fast-element/src/observation/arrays.spec.ts index b43b84b3d12..27da19775f0 100644 --- a/packages/web-components/fast-element/src/observation/arrays.spec.ts +++ b/packages/web-components/fast-element/src/observation/arrays.spec.ts @@ -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", () => { diff --git a/packages/web-components/fast-element/src/observation/arrays.ts b/packages/web-components/fast-element/src/observation/arrays.ts index 7388aa1724d..f6c2226f948 100644 --- a/packages/web-components/fast-element/src/observation/arrays.ts +++ b/packages/web-components/fast-element/src/observation/arrays.ts @@ -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;