-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): add retryWhen operator. closes #129
- Loading branch information
Showing
5 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.retryWhen()', function () { | ||
it('should retry when notified via returned notifier on thrown error', function (done) { | ||
var retried = false; | ||
var expected = [1, 2, 1, 2]; | ||
var i = 0; | ||
Observable.of(1, 2, 3) | ||
.map(function (n) { | ||
if (n === 3) { | ||
throw 'bad'; | ||
} | ||
return n; | ||
}) | ||
.retryWhen(function (errors) { | ||
return errors.map(function (x) { | ||
expect(x).toBe('bad'); | ||
if (retried) { | ||
throw 'done'; | ||
} | ||
retried = true; | ||
return x; | ||
}); | ||
}) | ||
.subscribe(function (x) { | ||
expect(x).toBe(expected[i++]); | ||
}, | ||
function (err) { | ||
expect(err).toBe('done'); | ||
done(); | ||
}) | ||
}); | ||
|
||
it('should retry when notified and complete on returned completion', function (done) { | ||
var expected = [1, 2, 1, 2]; | ||
Observable.of(1, 2, 3) | ||
.map(function (n) { | ||
if (n === 3) { | ||
throw 'bad'; | ||
} | ||
return n; | ||
}) | ||
.retryWhen(function (errors) { | ||
return errors.take(1); | ||
}) | ||
.subscribe(function (n) { | ||
expect(n).toBe(expected.shift()); | ||
}, function (err) { | ||
throw 'error should not be called'; | ||
}, function () { | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Operator from '../Operator'; | ||
import Observer from '../Observer'; | ||
import Subscriber from '../Subscriber'; | ||
import Observable from '../Observable'; | ||
import Subject from '../Subject'; | ||
import Subscription from '../Subscription'; | ||
|
||
import tryCatch from '../util/tryCatch'; | ||
import {errorObject} from '../util/errorObject'; | ||
|
||
export default function retryWhen<T>(notifier: (errors:Observable<any>) => Observable<any>) { | ||
return this.lift(new RetryWhenOperator(notifier, this)); | ||
} | ||
|
||
export class RetryWhenOperator<T, R> extends Operator<T, R> { | ||
constructor(protected notifier: (errors: Observable<any>) => Observable<any>, protected original:Observable<T>) { | ||
super(); | ||
} | ||
|
||
call(observer: Observer<T>): Observer<T> { | ||
return new RetryWhenSubscriber<T>(observer, this.notifier, this.original); | ||
} | ||
} | ||
|
||
export class RetryWhenSubscriber<T> extends Subscriber<T> { | ||
errors: Subject<any>; | ||
retryNotifications: Observable<any>; | ||
retryNotificationSubscription: Subscription<any>; | ||
|
||
constructor(destination: Observer<T>, public notifier: (errors: Observable<any>) => Observable<any>, public original: Observable<T>) { | ||
super(destination); | ||
} | ||
|
||
_error(err: any) { | ||
if (!this.retryNotifications) { | ||
this.errors = new Subject(); | ||
const notifications = tryCatch(this.notifier).call(this, this.errors); | ||
if (notifications === errorObject) { | ||
this.destination.error(errorObject.e); | ||
} else { | ||
this.retryNotifications = notifications; | ||
this.retryNotificationSubscription = notifications.subscribe(new RetryNotificationSubscriber(this)); | ||
this.add(this.retryNotificationSubscription); | ||
} | ||
} | ||
this.errors.next(err); | ||
} | ||
|
||
finalError(err: any) { | ||
this.destination.error(err); | ||
} | ||
|
||
resubscribe() { | ||
this.original.subscribe(this); | ||
} | ||
} | ||
|
||
export class RetryNotificationSubscriber<T> extends Subscriber<T> { | ||
constructor(public parent: RetryWhenSubscriber<any>) { | ||
super(null); | ||
} | ||
|
||
_next(value: T) { | ||
this.parent.resubscribe(); | ||
} | ||
|
||
_error(err: any) { | ||
this.parent.finalError(err); | ||
} | ||
|
||
_complete() { | ||
this.parent.complete(); | ||
} | ||
} |