From 9858aa32106f4203401bef08b7cc8fc842882057 Mon Sep 17 00:00:00 2001 From: Igor Oleinikov Date: Mon, 28 Mar 2016 22:46:14 -0700 Subject: [PATCH] fix(take): make 'take' unsubscribe when it reaches the total --- spec/operators/take-spec.ts | 10 ++++++++++ src/operator/take.ts | 1 + 2 files changed, 11 insertions(+) diff --git a/spec/operators/take-spec.ts b/spec/operators/take-spec.ts index f106d31b51..97e6bfde9c 100644 --- a/spec/operators/take-spec.ts +++ b/spec/operators/take-spec.ts @@ -125,4 +125,14 @@ describe('Observable.prototype.take', () => { expectObservable(result, unsub).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); + + it('should unsubscribe from the source when it reaches the limit', () => { + const source = Observable.create(observer => { + expect(observer.isUnsubscribed).toBe(false); + observer.next(42); + expect(observer.isUnsubscribed).toBe(true); + }).take(1); + + source.subscribe(); + }); }); diff --git a/src/operator/take.ts b/src/operator/take.ts index d28f401299..ae42e5e7b2 100644 --- a/src/operator/take.ts +++ b/src/operator/take.ts @@ -47,6 +47,7 @@ class TakeSubscriber extends Subscriber { this.destination.next(value); if (this.count === total) { this.destination.complete(); + this.unsubscribe(); } } }