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

fix(repeatWhen): wait for source to complete after notifier completes #2209

Merged
merged 1 commit into from
Jan 29, 2017
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
6 changes: 3 additions & 3 deletions spec/operators/repeatWhen-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ describe('Observable.prototype.repeatWhen', () => {
const source = cold('-1--2--|');
const subs = ['^ ! ',
' ^ ! ',
' ^ !'];
' ^ !'];
const notifier = hot('-------------r------------r-|');
const expected = '-1--2---------1--2---------1|';
const expected = '-1--2---------1--2---------1--2--|';

const result = source.repeatWhen((notifications: any) => notifier);

Expand Down Expand Up @@ -320,4 +320,4 @@ describe('Observable.prototype.repeatWhen', () => {
expectObservable(result).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
});
});
68 changes: 36 additions & 32 deletions src/operator/repeatWhen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ import { subscribeToResult } from '../util/subscribeToResult';
* @owner Observable
*/
export function repeatWhen<T>(this: Observable<T>, notifier: (notifications: Observable<any>) => Observable<any>): Observable<T> {
return this.lift(new RepeatWhenOperator(notifier, this));
return this.lift(new RepeatWhenOperator(notifier));
}

class RepeatWhenOperator<T> implements Operator<T, T> {
constructor(protected notifier: (notifications: Observable<any>) => Observable<any>,
protected source: Observable<T>) {
constructor(protected notifier: (notifications: Observable<any>) => Observable<any>) {
}

call(subscriber: Subscriber<T>, source: any): TeardownLogic {
return source.subscribe(new RepeatWhenSubscriber(subscriber, this.notifier, this.source));
return source.subscribe(new RepeatWhenSubscriber(subscriber, this.notifier, source));
}
}

Expand All @@ -50,40 +49,39 @@ class RepeatWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
private notifications: Subject<any>;
private retries: Observable<any>;
private retriesSubscription: Subscription;
private sourceIsBeingSubscribedTo: boolean = true;

constructor(destination: Subscriber<R>,
private notifier: (notifications: Observable<any>) => Observable<any>,
private source: Observable<T>) {
super(destination);
}

complete() {
if (!this.isStopped) {
notifyNext(outerValue: T, innerValue: R,
outerIndex: number, innerIndex: number,
innerSub: InnerSubscriber<T, R>): void {
this.source.subscribe(this);
this.sourceIsBeingSubscribedTo = true;
}

let notifications = this.notifications;
let retries: any = this.retries;
let retriesSubscription = this.retriesSubscription;

if (!retries) {
notifications = new Subject();
retries = tryCatch(this.notifier)(notifications);
if (retries === errorObject) {
return super.complete();
}
retriesSubscription = subscribeToResult(this, retries);
} else {
this.notifications = null;
this.retriesSubscription = null;
}
notifyComplete(innerSub: InnerSubscriber<T, R>): void {
if (this.sourceIsBeingSubscribedTo === false) {
return super.complete();
}
}

this.unsubscribe();
this.closed = false;
complete() {
this.sourceIsBeingSubscribedTo = false;

this.notifications = notifications;
this.retries = retries;
this.retriesSubscription = retriesSubscription;
if (!this.isStopped) {
if (!this.retries) {
this.subscribeToRetries();
} else if (this.retriesSubscription.closed) {
return super.complete();
}

notifications.next();
this.temporarilyUnsubscribe();
this.notifications.next();
}
}

Expand All @@ -100,10 +98,17 @@ class RepeatWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
this.retries = null;
}

notifyNext(outerValue: T, innerValue: R,
outerIndex: number, innerIndex: number,
innerSub: InnerSubscriber<T, R>): void {
private subscribeToRetries() {
this.notifications = new Subject();
const retries = tryCatch(this.notifier)(this.notifications);
if (retries === errorObject) {
return super.complete();
}
this.retries = retries;
this.retriesSubscription = subscribeToResult(this, retries);
}

private temporarilyUnsubscribe() {
const { notifications, retries, retriesSubscription } = this;
this.notifications = null;
this.retries = null;
Expand All @@ -116,7 +121,6 @@ class RepeatWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
this.notifications = notifications;
this.retries = retries;
this.retriesSubscription = retriesSubscription;

this.source.subscribe(this);
}

}