Skip to content

Commit

Permalink
fix(AsyncSubject): emit value when it's subscribed after complete
Browse files Browse the repository at this point in the history
Currently, if a new subscription happens after `complete` the
AsyncSubject will emit just `complete` instead of `next` and then
`complete`.
  • Loading branch information
luisgabriel authored and kwonoj committed Dec 3, 2015
1 parent 2c5bf41 commit ed0eaf6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
54 changes: 54 additions & 0 deletions spec/subjects/async-subject-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,35 @@ describe('AsyncSubject', function () {
expect(results).toEqual([2, 'done']);
});

it('should emit the last value to subsequent subscriptions after complete', function () {
var subject = new AsyncSubject();
var results = [];

var observer = {
next: function (x) {
results.push(x);
},
error: null,
complete: function () {
results.push('done');
}
};
var subscription = subject.subscribe(observer);

subject.next(1);
expect(results).toEqual([]);
subject.next(2);
expect(results).toEqual([]);
subject.complete();
expect(results).toEqual([2, 'done']);

subscription.unsubscribe();

results = [];
subject.subscribe(observer);
expect(results).toEqual([2, 'done']);
});

it('should just complete if no value has been nexted into it', function () {
var subject = new AsyncSubject();
var results = [];
Expand All @@ -38,6 +67,31 @@ describe('AsyncSubject', function () {
expect(results).toEqual(['done']);
});

it('should just complete to any subscription if no value has been nexted into it', function () {
var subject = new AsyncSubject();
var results = [];

var observer = {
next: function (x) {
results.push(x);
},
error: null,
complete: function () {
results.push('done');
}
};
var subscription = subject.subscribe(observer);

expect(results).toEqual([]);
subject.complete();
expect(results).toEqual(['done']);

subscription.unsubscribe();
results = [];
subject.subscribe(observer);
expect(results).toEqual(['done']);
});

it('should only error if an error is passed into it', function () {
var subject = new AsyncSubject();
var results = [];
Expand Down
9 changes: 3 additions & 6 deletions src/subject/AsyncSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ export class AsyncSubject<T> extends Subject<T> {
}

_subscribe(subscriber: Subscriber<any>): Subscription<T> {
const subscription = super._subscribe(subscriber);
if (!subscription) {
return;
} else if (!subscription.isUnsubscribed && this._hasNext) {
if (this.completeSignal && this._hasNext) {
subscriber.next(this._value);
subscriber.complete();
}
return subscription;

return super._subscribe(subscriber);
}

_next(value: T): void {
Expand Down

0 comments on commit ed0eaf6

Please sign in to comment.