Skip to content

Commit

Permalink
fix(AsyncSubject): do not allow change value after complete
Browse files Browse the repository at this point in the history
closes #1800
  • Loading branch information
kwonoj committed Jul 17, 2016
1 parent 75a355f commit 801f282
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
15 changes: 15 additions & 0 deletions spec/subjects/AsyncSubject-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ describe('AsyncSubject', () => {
expect(observer.results).to.deep.equal([2, 'done']);
});

it('should not allow change value after complete', () => {
const subject = new AsyncSubject();
const observer = new TestObserver();
const otherObserver = new TestObserver();
subject.subscribe(observer);

subject.next(1);
expect(observer.results).to.deep.equal([]);
subject.complete();
expect(observer.results).to.deep.equal([1, 'done']);
subject.next(2);
subject.subscribe(otherObserver);
expect(otherObserver.results).to.deep.equal([1, 'done']);
});

it('should not emit values if unsubscribed before complete', () => {
const subject = new AsyncSubject();
const observer = new TestObserver();
Expand Down
14 changes: 7 additions & 7 deletions src/AsyncSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import {Subscription} from './Subscription';
* @class AsyncSubject<T>
*/
export class AsyncSubject<T> extends Subject<T> {
value: T = null;

hasNext: boolean = false;

hasCompleted: boolean = false;
private value: T = null;
private hasNext: boolean = false;
private hasCompleted: boolean = false;

protected _subscribe(subscriber: Subscriber<any>): Subscription {
if (this.hasCompleted && this.hasNext) {
Expand All @@ -26,8 +24,10 @@ export class AsyncSubject<T> extends Subject<T> {
}

next(value: T): void {
this.value = value;
this.hasNext = true;
if (!this.hasCompleted) {
this.value = value;
this.hasNext = true;
}
}

complete(): void {
Expand Down

0 comments on commit 801f282

Please sign in to comment.