From 4ad91fa3cd554285fd4e84b34a868f7ac3b729f1 Mon Sep 17 00:00:00 2001 From: ersimont <8042088+ersimont@users.noreply.github.com> Date: Mon, 15 Jul 2019 16:05:46 -0400 Subject: [PATCH] fix: observables were not emitting properly when the number of subscribers changed mid-emit --- projects/ng-app-state/src/lib/store-object.spec.ts | 13 +++++++++++++ .../lib/tree-based-observable/observable-node.ts | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/projects/ng-app-state/src/lib/store-object.spec.ts b/projects/ng-app-state/src/lib/store-object.spec.ts index 91581d4..20f8a3b 100644 --- a/projects/ng-app-state/src/lib/store-object.spec.ts +++ b/projects/ng-app-state/src/lib/store-object.spec.ts @@ -190,6 +190,19 @@ describe("StoreObject", () => { counterStore.$.subscribe(spy); expectSingleCallAndReset(spy, 3); }); + + it("works when the number of subscribers changes mid-emit (production bug)", () => { + const spy = jasmine.createSpy(); + + store("counter") + .$.pipe(take(2)) + .subscribe(); + store("counter").$.subscribe(spy); + expectSingleCallAndReset(spy, 0); + + store("counter").set(1); + expectSingleCallAndReset(spy, 1); + }); }); describe(".batch()", () => { diff --git a/projects/ng-app-state/src/lib/tree-based-observable/observable-node.ts b/projects/ng-app-state/src/lib/tree-based-observable/observable-node.ts index c751b3b..966d506 100644 --- a/projects/ng-app-state/src/lib/tree-based-observable/observable-node.ts +++ b/projects/ng-app-state/src/lib/tree-based-observable/observable-node.ts @@ -74,7 +74,7 @@ export class ObservableNode extends Observable { } private emit() { - for (const subscriber of this.subscribers) { + for (const subscriber of this.subscribers.slice()) { subscriber.next(this.value); } }