Skip to content

Commit

Permalink
Merge pull request #1935 from trxcllnt/fix-1921
Browse files Browse the repository at this point in the history
Fixes #1921
  • Loading branch information
jayphelps authored Sep 11, 2016
2 parents f63dde9 + 4d23f87 commit d876a35
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
54 changes: 54 additions & 0 deletions spec/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,60 @@ describe('Observable', () => {
source.subscribe();
});

it('should run unsubscription logic when an error is sent synchronously and subscribe is called with no arguments', () => {
let unsubscribeCalled = false;
const source = new Observable((subscriber: Rx.Subscriber<string>) => {
subscriber.error(0);
return () => {
unsubscribeCalled = true;
};
});

try {
source.subscribe();
} catch (e) {
// error'ing to an empty Observer re-throws, so catch and ignore it here.
}

expect(unsubscribeCalled).to.be.true;
});

it('should run unsubscription logic when an error is sent asynchronously and subscribe is called with no arguments', (done: MochaDone) => {
let unsubscribeCalled = false;
const source = new Observable((subscriber: Rx.Subscriber<string>) => {
const id = setInterval(() => {
try {
subscriber.error(0);
} catch (e) {
// asynchronously error'ing to an empty Observer re-throws, so catch and ignore it here.
}
}, 1);
return () => {
clearInterval(id);
unsubscribeCalled = true;
};
});

source.subscribe();

setTimeout(() => {
let err;
let errHappened = false;
try {
expect(unsubscribeCalled).to.be.true;
} catch (e) {
err = e;
errHappened = true;
} finally {
if (!errHappened) {
done();
} else {
done(err);
}
}
}, 100);
});

it('should return a Subscription that calls the unsubscribe function returned by the subscriber', () => {
let unsubscribeCalled = false;

Expand Down
2 changes: 1 addition & 1 deletion src/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {

unsubscribe() {
const { done, xhr } = this;
if (!done && xhr && xhr.readyState !== 4) {
if (!done && xhr && xhr.readyState !== 4 && typeof xhr.abort === 'function') {
xhr.abort();
}
super.unsubscribe();
Expand Down
6 changes: 3 additions & 3 deletions src/util/toSubscriber.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PartialObserver } from '../Observer';
import { Subscriber } from '../Subscriber';
import { $$rxSubscriber } from '../symbol/rxSubscriber';
import { PartialObserver, empty as emptyObserver } from '../Observer';

export function toSubscriber<T>(
nextOrObserver?: PartialObserver<T> | ((value: T) => void),
Expand All @@ -18,8 +18,8 @@ export function toSubscriber<T>(
}

if (!nextOrObserver && !error && !complete) {
return new Subscriber();
return new Subscriber(emptyObserver);
}

return new Subscriber(nextOrObserver, error, complete);
}
}

0 comments on commit d876a35

Please sign in to comment.