From 8e7e4e31e936baf9b57089b8fd70d60120a3339c Mon Sep 17 00:00:00 2001 From: Paul Taylor Date: Sat, 10 Sep 2016 17:22:08 -0700 Subject: [PATCH] fix(util/toSubscriber): Supplies the Subscriber constructor with emptyObserver as destination if no 1921 --- spec/Observable-spec.ts | 54 ++++++++++++++++++++++++++++++++++++++++ src/util/toSubscriber.ts | 6 ++--- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/spec/Observable-spec.ts b/spec/Observable-spec.ts index 22702b5f57..8d96a0b027 100644 --- a/spec/Observable-spec.ts +++ b/spec/Observable-spec.ts @@ -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) => { + 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) => { + 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; diff --git a/src/util/toSubscriber.ts b/src/util/toSubscriber.ts index ea7faa4d59..53c73f4781 100644 --- a/src/util/toSubscriber.ts +++ b/src/util/toSubscriber.ts @@ -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( nextOrObserver?: PartialObserver | ((value: T) => void), @@ -18,8 +18,8 @@ export function toSubscriber( } if (!nextOrObserver && !error && !complete) { - return new Subscriber(); + return new Subscriber(emptyObserver); } return new Subscriber(nextOrObserver, error, complete); -} \ No newline at end of file +}