Skip to content

Commit

Permalink
fix(Subject): ensure subject properly throws ObjectUnsubscribedError …
Browse files Browse the repository at this point in the history
…when unsubscribed then resubscribed to (#2318)

- Also changes one AsyncSubject test that seemed to have a typo where the author intended to dispose of a subscription, but disposed of the subject itself instead

*
  • Loading branch information
benlesh authored Feb 1, 2017
1 parent a77821b commit 41489eb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions spec/Subject-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ describe('Subject', () => {
expect(() => {
subject.subscribe(
function (x) { results3.push(x); },
function (err) {
expect(false).to.equal('should not throw error: ' + err.toString());
}
);
}).to.throw(Rx.ObjectUnsubscribedError);

Expand Down
4 changes: 2 additions & 2 deletions spec/subjects/AsyncSubject-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ describe('AsyncSubject', () => {
const expected = new Error('bad');
const subject = new AsyncSubject();
const observer = new TestObserver();
subject.subscribe(observer);
const subscription = subject.subscribe(observer);

subject.next(1);
expect(observer.results).to.deep.equal([]);

subject.error(expected);
expect(observer.results).to.deep.equal([expected]);

subject.unsubscribe();
subscription.unsubscribe();

observer.results = [];
subject.subscribe(observer);
Expand Down
2 changes: 1 addition & 1 deletion src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class Observable<T> implements Subscribable<T> {
return sink;
}

private _trySubscribe(sink: Subscriber<T>): TeardownLogic {
protected _trySubscribe(sink: Subscriber<T>): TeardownLogic {
try {
return this._subscribe(sink);
} catch (err) {
Expand Down
10 changes: 9 additions & 1 deletion src/Subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Operator } from './Operator';
import { Observer } from './Observer';
import { Observable } from './Observable';
import { Subscriber } from './Subscriber';
import { ISubscription, Subscription } from './Subscription';
import { ISubscription, Subscription, TeardownLogic } from './Subscription';
import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';
import { SubjectSubscription } from './SubjectSubscription';
import { $$rxSubscriber } from './symbol/rxSubscriber';
Expand Down Expand Up @@ -99,6 +99,14 @@ export class Subject<T> extends Observable<T> implements ISubscription {
this.observers = null;
}

protected _trySubscribe(subscriber: Subscriber<T>): TeardownLogic {
if (this.closed) {
throw new ObjectUnsubscribedError();
} else {
return super._trySubscribe(subscriber);
}
}

protected _subscribe(subscriber: Subscriber<T>): Subscription {
if (this.closed) {
throw new ObjectUnsubscribedError();
Expand Down

0 comments on commit 41489eb

Please sign in to comment.