diff --git a/spec/operators/takeUntil-spec.ts b/spec/operators/takeUntil-spec.ts index 6edd45eddc..2bcfef8948 100644 --- a/spec/operators/takeUntil-spec.ts +++ b/spec/operators/takeUntil-spec.ts @@ -55,6 +55,15 @@ describe('Observable.prototype.takeUntil', () => { expectSubscriptions(e2.subscriptions).toBe(e2subs); }); + it('should complete without subscribing to the source when notifier synchronously emits', () => { + const e1 = hot('----a--|'); + const e2 = Observable.of(0); + const expected = '(|) '; + + expectObservable(e1.takeUntil(e2)).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe([]); + }); + it('should allow unsubscribing explicitly and early', () => { const e1 = hot('--a--b--c--d--e--f--g--|'); const e1subs = '^ ! '; diff --git a/src/internal/operators/takeUntil.ts b/src/internal/operators/takeUntil.ts index 9f70687d0e..b792280dd8 100644 --- a/src/internal/operators/takeUntil.ts +++ b/src/internal/operators/takeUntil.ts @@ -51,7 +51,13 @@ class TakeUntilOperator implements Operator { } call(subscriber: Subscriber, source: any): TeardownLogic { - return source.subscribe(new TakeUntilSubscriber(subscriber, this.notifier)); + const takeUntilSubscriber = new TakeUntilSubscriber(subscriber); + const notifierSubscription = subscribeToResult(takeUntilSubscriber, this.notifier); + if (notifierSubscription && !notifierSubscription.closed) { + takeUntilSubscriber.add(notifierSubscription); + return source.subscribe(takeUntilSubscriber); + } + return takeUntilSubscriber; } } @@ -62,10 +68,8 @@ class TakeUntilOperator implements Operator { */ class TakeUntilSubscriber extends OuterSubscriber { - constructor(destination: Subscriber, - private notifier: Observable) { + constructor(destination: Subscriber, ) { super(destination); - this.add(subscribeToResult(this, notifier)); } notifyNext(outerValue: T, innerValue: R,