From a7f9a53fa6542acac741ef8a55954791076d07c0 Mon Sep 17 00:00:00 2001 From: Tobias Laundal Date: Thu, 23 Apr 2020 13:56:40 +0200 Subject: [PATCH] fix(state stream): ensure state property is always updated Using `reducedState$.subscribe(this.subject)` works fine in all our tests, but when trying to use StateStream in ardoq-front I ran into issues with the `.state` property not being updated. I have not managed to write a test to verify this behavior, but manually creating an Observer fixed the issue. --- src/persistentReducedStream.spec.ts | 3 +++ src/persistentReducedStream.ts | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/persistentReducedStream.spec.ts b/src/persistentReducedStream.spec.ts index a8049631..dc08b548 100644 --- a/src/persistentReducedStream.spec.ts +++ b/src/persistentReducedStream.spec.ts @@ -7,6 +7,9 @@ import { never, of, Subject } from 'rxjs'; import { marbles } from 'rxjs-marbles/ava'; import { incrementMocks } from './internal/testing/mock'; import { map } from 'rxjs/operators'; +import { reducer } from './reducer'; +import { actionCreator } from './actionCreator'; +import { UnknownAction } from './internal'; const { reducers, actionCreators, handlers } = incrementMocks; const { actions, numbers, errors } = incrementMocks.marbles; diff --git a/src/persistentReducedStream.ts b/src/persistentReducedStream.ts index 4c7c61e9..3b6909ea 100644 --- a/src/persistentReducedStream.ts +++ b/src/persistentReducedStream.ts @@ -25,7 +25,15 @@ export class StateStream extends Observable constructor(initialState: State, reducedState$: Observable) { super(); this.subject = new BehaviorSubject(initialState); - this.subscription = reducedState$.subscribe(this.subject); + + // NOTE: .subscribe(this.subject) does not work + // All tests pass with it, but in practice there are cases + // where the subject isn't updated as expected + this.subscription = reducedState$.subscribe({ + complete: () => this.subject.complete(), + error: (err) => this.subject.error(err), + next: (state) => this.subject.next(state), + }); } get state(): State {