1
+ import Operator from '../Operator' ;
2
+ import Observer from '../Observer' ;
3
+ import Subscriber from '../Subscriber' ;
4
+ import Observable from '../Observable' ;
5
+ import Subject from '../Subject' ;
6
+ import Subscription from '../Subscription' ;
7
+
8
+ import tryCatch from '../util/tryCatch' ;
9
+ import { errorObject } from '../util/errorObject' ;
10
+
11
+ export default function retryWhen < T > ( notifier : ( errors :Observable < any > ) => Observable < any > ) {
12
+ return this . lift ( new RetryWhenOperator ( notifier , this ) ) ;
13
+ }
14
+
15
+ export class RetryWhenOperator < T , R > extends Operator < T , R > {
16
+ constructor ( protected notifier : ( errors : Observable < any > ) => Observable < any > , protected original :Observable < T > ) {
17
+ super ( ) ;
18
+ }
19
+
20
+ call ( observer : Observer < T > ) : Observer < T > {
21
+ return new RetryWhenSubscriber < T > ( observer , this . notifier , this . original ) ;
22
+ }
23
+ }
24
+
25
+ export class RetryWhenSubscriber < T > extends Subscriber < T > {
26
+ errors : Subject < any > ;
27
+ retryNotifications : Observable < any > ;
28
+ retryNotificationSubscription : Subscription < any > ;
29
+
30
+ constructor ( destination : Observer < T > , public notifier : ( errors : Observable < any > ) => Observable < any > , public original : Observable < T > ) {
31
+ super ( destination ) ;
32
+ }
33
+
34
+ _error ( err : any ) {
35
+ if ( ! this . retryNotifications ) {
36
+ this . errors = new Subject ( ) ;
37
+ const notifications = tryCatch ( this . notifier ) . call ( this , this . errors ) ;
38
+ if ( notifications === errorObject ) {
39
+ this . destination . error ( errorObject . e ) ;
40
+ } else {
41
+ this . retryNotifications = notifications ;
42
+ this . retryNotificationSubscription = notifications . subscribe ( new RetryNotificationSubscriber ( this ) ) ;
43
+ this . add ( this . retryNotificationSubscription ) ;
44
+ }
45
+ }
46
+ this . errors . next ( err ) ;
47
+ }
48
+
49
+ finalError ( err : any ) {
50
+ this . destination . error ( err ) ;
51
+ }
52
+
53
+ resubscribe ( ) {
54
+ this . original . subscribe ( this ) ;
55
+ }
56
+ }
57
+
58
+ export class RetryNotificationSubscriber < T > extends Subscriber < T > {
59
+ constructor ( public parent : RetryWhenSubscriber < any > ) {
60
+ super ( null ) ;
61
+ }
62
+
63
+ _next ( value : T ) {
64
+ this . parent . resubscribe ( ) ;
65
+ }
66
+
67
+ _error ( err : any ) {
68
+ this . parent . finalError ( err ) ;
69
+ }
70
+
71
+ _complete ( ) {
72
+ this . parent . complete ( ) ;
73
+ }
74
+ }
0 commit comments