Skip to content

Commit

Permalink
fix(fast-element): do not notify splices for changes pre-subscription…
Browse files Browse the repository at this point in the history
… (v2) (#6006)

* fix(fast-element): do not notify splices for changes pre-subscription

* Change files

Co-authored-by: EisenbergEffect <[email protected]>
  • Loading branch information
EisenbergEffect and EisenbergEffect committed May 31, 2022
1 parent a5db4c3 commit bbbafc2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix(fast-element): do not notify splices for changes pre-subscription",
"packageName": "@microsoft/fast-element",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,24 @@ describe("The ArrayObserver", () => {
expect(changeArgs![0].removed).members([]);
expect(changeArgs![0].index).equal(0);
});

it("should not deliver splices for changes prior to subscription", async () => {
ArrayObserver.enable();
const array = [1,2,3,4,5];
const observer = Observable.getNotifier(array);
let wasCalled = false;

array.push(6);
observer.subscribe({
handleChange() {
wasCalled = true;
}
});

await Updates.next();

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

describe("The array length observer", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ class DefaultArrayObserver extends SubscriberSet implements ArrayObserver {
setNonEnumerable(subject, "$fastController", this);
}

public subscribe(subscriber: Subscriber): void {
this.flush();
super.subscribe(subscriber);
}

public addSplice(splice: Splice) {
if (this.splices === void 0) {
this.splices = [splice];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,22 @@ export class SubscriberSet implements Notifier {
*/
public notify(args: any): void {
const spillover = this.spillover;
const source = this.source;
const subject = this.subject;

if (spillover === void 0) {
const sub1 = this.sub1;
const sub2 = this.sub2;

if (sub1 !== void 0) {
sub1.handleChange(source, args);
sub1.handleChange(subject, args);
}

if (sub2 !== void 0) {
sub2.handleChange(source, args);
sub2.handleChange(subject, args);
}
} else {
for (let i = 0, ii = spillover.length; i < ii; ++i) {
spillover[i].handleChange(source, args);
spillover[i].handleChange(subject, args);
}
}
}
Expand Down

0 comments on commit bbbafc2

Please sign in to comment.