Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(takeUntil): no longer subscribes to source if notifier synchrono… #3504

Merged
merged 1 commit into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions spec/operators/takeUntil-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '^ ! ';
Expand Down
12 changes: 8 additions & 4 deletions src/internal/operators/takeUntil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ class TakeUntilOperator<T> implements Operator<T, T> {
}

call(subscriber: Subscriber<T>, 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;
}
}

Expand All @@ -62,10 +68,8 @@ class TakeUntilOperator<T> implements Operator<T, T> {
*/
class TakeUntilSubscriber<T, R> extends OuterSubscriber<T, R> {

constructor(destination: Subscriber<any>,
private notifier: Observable<any>) {
constructor(destination: Subscriber<any>, ) {
super(destination);
this.add(subscribeToResult(this, notifier));
}

notifyNext(outerValue: T, innerValue: R,
Expand Down