Skip to content

Commit

Permalink
fix(util/toSubscriber): Supplies the Subscriber constructor with empt…
Browse files Browse the repository at this point in the history
…yObserver as destination if no

1921
  • Loading branch information
trxcllnt committed Sep 11, 2016
1 parent f63dde9 commit 8e7e4e3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 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
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 8e7e4e3

Please sign in to comment.