Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleDoOnTerminate.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.single;
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.Action;
20
21 public final class SingleDoOnTerminate<T> extends Single<T> {
22
23 final SingleSource<T> source;
24
25 final Action onTerminate;
26
27 public SingleDoOnTerminate(SingleSource<T> source, Action onTerminate) {
28 this.source = source;
29 this.onTerminate = onTerminate;
30 }
31
32 @Override
33 protected void subscribeActual(final SingleObserver<? super T> observer) {
34 source.subscribe(new DoOnTerminate(observer));
35 }
36
37 final class DoOnTerminate implements SingleObserver<T> {
38
39 final SingleObserver<? super T> downstream;
40
41 DoOnTerminate(SingleObserver<? super T> observer) {
42 this.downstream = observer;
43 }
44
45 @Override
46 public void onSubscribe(Disposable d) {
47 downstream.onSubscribe(d);
48 }
49
50 @Override
51 public void onSuccess(T value) {
52 try {
53 onTerminate.run();
54 } catch (Throwable ex) {
55 Exceptions.throwIfFatal(ex);
56 downstream.onError(ex);
57 return;
58 }
59
60 downstream.onSuccess(value);
61 }
62
63 @Override
64 public void onError(Throwable e) {
65 try {
66 onTerminate.run();
67 } catch (Throwable ex) {
68 Exceptions.throwIfFatal(ex);
69 e = new CompositeException(e, ex);
70 }
71
72 downstream.onError(e);
73 }
74 }
75 }