Skip to content

Commit

Permalink
fix(repeat): preserve Subscriber chain in repeat()
Browse files Browse the repository at this point in the history
Fix repeat() operator to add its own Subscriber to the destination Subscriber, in order to avoid
breaking unsubscription chains.

For issue #875.
  • Loading branch information
staltz authored and benlesh committed Dec 8, 2015
1 parent d9d7551 commit d9a7328
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/operator/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class FirstRepeatSubscriber<T> extends Subscriber<T> {
constructor(public destination: Subscriber<T>,
private count: number,
private source: Observable<T>) {
super(null);
super();
destination.add(this);
this.lastSubscription = this;
}

Expand Down Expand Up @@ -56,20 +57,23 @@ class FirstRepeatSubscriber<T> extends Subscriber<T> {
}

resubscribe(count: number): void {
this.lastSubscription.unsubscribe();
const { destination, lastSubscription } = this;
destination.remove(lastSubscription);
lastSubscription.unsubscribe();
if (count - 1 === 0) {
this.destination.complete();
destination.complete();
} else {
const nextSubscriber = new MoreRepeatSubscriber(this, count - 1);
this.lastSubscription = this.source.subscribe(nextSubscriber);
destination.add(this.lastSubscription);
}
}
}

class MoreRepeatSubscriber<T> extends Subscriber<T> {
constructor(private parent: FirstRepeatSubscriber<T>,
private count: number) {
super(null);
super();
}

_next(value: T): void {
Expand Down

0 comments on commit d9a7328

Please sign in to comment.