13
13
14
14
package io .reactivex .internal .operators .completable ;
15
15
16
+ import java .util .concurrent .atomic .AtomicReference ;
17
+
16
18
import io .reactivex .*;
17
19
import io .reactivex .disposables .Disposable ;
18
20
import io .reactivex .exceptions .*;
19
21
import io .reactivex .functions .Function ;
20
- import io .reactivex .internal .disposables .SequentialDisposable ;
22
+ import io .reactivex .internal .disposables .DisposableHelper ;
23
+ import io .reactivex .internal .functions .ObjectHelper ;
21
24
22
25
public final class CompletableResumeNext extends Completable {
23
26
@@ -35,20 +38,32 @@ public CompletableResumeNext(CompletableSource source,
35
38
36
39
@ Override
37
40
protected void subscribeActual (final CompletableObserver observer ) {
38
-
39
- final SequentialDisposable sd = new SequentialDisposable ();
40
- observer .onSubscribe (sd );
41
- source .subscribe (new ResumeNext (observer , sd ));
41
+ ResumeNextObserver parent = new ResumeNextObserver (observer , errorMapper );
42
+ observer .onSubscribe (parent );
43
+ source .subscribe (parent );
42
44
}
43
45
44
- final class ResumeNext implements CompletableObserver {
46
+ static final class ResumeNextObserver
47
+ extends AtomicReference <Disposable >
48
+ implements CompletableObserver , Disposable {
49
+
50
+ private static final long serialVersionUID = 5018523762564524046L ;
45
51
46
52
final CompletableObserver downstream ;
47
- final SequentialDisposable sd ;
48
53
49
- ResumeNext (CompletableObserver observer , SequentialDisposable sd ) {
54
+ final Function <? super Throwable , ? extends CompletableSource > errorMapper ;
55
+
56
+ boolean once ;
57
+
58
+ ResumeNextObserver (CompletableObserver observer , Function <? super Throwable , ? extends CompletableSource > errorMapper ) {
50
59
this .downstream = observer ;
51
- this .sd = sd ;
60
+ this .errorMapper = errorMapper ;
61
+ }
62
+
63
+
64
+ @ Override
65
+ public void onSubscribe (Disposable d ) {
66
+ DisposableHelper .replace (this , d );
52
67
}
53
68
54
69
@ Override
@@ -58,48 +73,33 @@ public void onComplete() {
58
73
59
74
@ Override
60
75
public void onError (Throwable e ) {
76
+ if (once ) {
77
+ downstream .onError (e );
78
+ return ;
79
+ }
80
+ once = true ;
81
+
61
82
CompletableSource c ;
62
83
63
84
try {
64
- c = errorMapper .apply (e );
85
+ c = ObjectHelper . requireNonNull ( errorMapper .apply (e ), "The errorMapper returned a null CompletableSource" );
65
86
} catch (Throwable ex ) {
66
87
Exceptions .throwIfFatal (ex );
67
- downstream .onError (new CompositeException (ex , e ));
88
+ downstream .onError (new CompositeException (e , ex ));
68
89
return ;
69
90
}
70
91
71
- if (c == null ) {
72
- NullPointerException npe = new NullPointerException ("The CompletableConsumable returned is null" );
73
- npe .initCause (e );
74
- downstream .onError (npe );
75
- return ;
76
- }
77
-
78
- c .subscribe (new OnErrorObserver ());
92
+ c .subscribe (this );
79
93
}
80
94
81
95
@ Override
82
- public void onSubscribe ( Disposable d ) {
83
- sd . update ( d );
96
+ public boolean isDisposed ( ) {
97
+ return DisposableHelper . isDisposed ( get () );
84
98
}
85
99
86
- final class OnErrorObserver implements CompletableObserver {
87
-
88
- @ Override
89
- public void onComplete () {
90
- downstream .onComplete ();
91
- }
92
-
93
- @ Override
94
- public void onError (Throwable e ) {
95
- downstream .onError (e );
96
- }
97
-
98
- @ Override
99
- public void onSubscribe (Disposable d ) {
100
- sd .update (d );
101
- }
102
-
100
+ @ Override
101
+ public void dispose () {
102
+ DisposableHelper .dispose (this );
103
103
}
104
104
}
105
105
}
0 commit comments