Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/completable/CompletablePeek.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.completable;
15
16 import io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.exceptions.*;
19 import io.reactivex.rxjava3.functions.*;
20 import io.reactivex.rxjava3.internal.disposables.*;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 public final class CompletablePeek extends Completable {
24
25 final CompletableSource source;
26 final Consumer<? super Disposable> onSubscribe;
27 final Consumer<? super Throwable> onError;
28 final Action onComplete;
29 final Action onTerminate;
30 final Action onAfterTerminate;
31 final Action onDispose;
32
33 public CompletablePeek(CompletableSource source, Consumer<? super Disposable> onSubscribe,
34 Consumer<? super Throwable> onError,
35 Action onComplete,
36 Action onTerminate,
37 Action onAfterTerminate,
38 Action onDispose) {
39 this.source = source;
40 this.onSubscribe = onSubscribe;
41 this.onError = onError;
42 this.onComplete = onComplete;
43 this.onTerminate = onTerminate;
44 this.onAfterTerminate = onAfterTerminate;
45 this.onDispose = onDispose;
46 }
47
48 @Override
49 protected void subscribeActual(final CompletableObserver observer) {
50
51 source.subscribe(new CompletableObserverImplementation(observer));
52 }
53
54 final class CompletableObserverImplementation implements CompletableObserver, Disposable {
55
56 final CompletableObserver downstream;
57
58 Disposable upstream;
59
60 CompletableObserverImplementation(CompletableObserver downstream) {
61 this.downstream = downstream;
62 }
63
64 @Override
65 public void onSubscribe(final Disposable d) {
66 try {
67 onSubscribe.accept(d);
68 } catch (Throwable ex) {
69 Exceptions.throwIfFatal(ex);
70 d.dispose();
71 this.upstream = DisposableHelper.DISPOSED;
72 EmptyDisposable.error(ex, downstream);
73 return;
74 }
75 if (DisposableHelper.validate(this.upstream, d)) {
76 this.upstream = d;
77 downstream.onSubscribe(this);
78 }
79 }
80
81 @Override
82 public void onError(Throwable e) {
83 if (upstream == DisposableHelper.DISPOSED) {
84 RxJavaPlugins.onError(e);
85 return;
86 }
87 try {
88 onError.accept(e);
89 onTerminate.run();
90 } catch (Throwable ex) {
91 Exceptions.throwIfFatal(ex);
92 e = new CompositeException(e, ex);
93 }
94
95 downstream.onError(e);
96
97 doAfter();
98 }
99
100 @Override
101 public void onComplete() {
102 if (upstream == DisposableHelper.DISPOSED) {
103 return;
104 }
105
106 try {
107 onComplete.run();
108 onTerminate.run();
109 } catch (Throwable e) {
110 Exceptions.throwIfFatal(e);
111 downstream.onError(e);
112 return;
113 }
114
115 downstream.onComplete();
116
117 doAfter();
118 }
119
120 void doAfter() {
121 try {
122 onAfterTerminate.run();
123 } catch (Throwable ex) {
124 Exceptions.throwIfFatal(ex);
125 RxJavaPlugins.onError(ex);
126 }
127 }
128
129 @Override
130 public void dispose() {
131 try {
132 onDispose.run();
133 } catch (Throwable e) {
134 Exceptions.throwIfFatal(e);
135 RxJavaPlugins.onError(e);
136 }
137 upstream.dispose();
138 }
139
140 @Override
141 public boolean isDisposed() {
142 return upstream.isDisposed();
143 }
144 }
145 }