Skip to content

Commit

Permalink
fix(state stream): ensure state property is always updated
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tlaundal committed Apr 23, 2020
1 parent 4de42f0 commit a7f9a53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/persistentReducedStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/persistentReducedStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ export class StateStream<State> extends Observable<State>
constructor(initialState: State, reducedState$: Observable<State>) {
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 {
Expand Down

0 comments on commit a7f9a53

Please sign in to comment.