Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleDoAfterTerminate.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.Exceptions;
19 import io.reactivex.rxjava3.functions.Action;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 /**
24 * Calls an action after pushing the current item or an error to the downstream.
25 * <p>History: 2.0.6 - experimental
26 * @param <T> the value type
27 * @since 2.1
28 */
29 public final class SingleDoAfterTerminate<T> extends Single<T> {
30
31 final SingleSource<T> source;
32
33 final Action onAfterTerminate;
34
35 public SingleDoAfterTerminate(SingleSource<T> source, Action onAfterTerminate) {
36 this.source = source;
37 this.onAfterTerminate = onAfterTerminate;
38 }
39
40 @Override
41 protected void subscribeActual(SingleObserver<? super T> observer) {
42 source.subscribe(new DoAfterTerminateObserver<>(observer, onAfterTerminate));
43 }
44
45 static final class DoAfterTerminateObserver<T> implements SingleObserver<T>, Disposable {
46
47 final SingleObserver<? super T> downstream;
48
49 final Action onAfterTerminate;
50
51 Disposable upstream;
52
53 DoAfterTerminateObserver(SingleObserver<? super T> actual, Action onAfterTerminate) {
54 this.downstream = actual;
55 this.onAfterTerminate = onAfterTerminate;
56 }
57
58 @Override
59 public void onSubscribe(Disposable d) {
60 if (DisposableHelper.validate(this.upstream, d)) {
61 this.upstream = d;
62
63 downstream.onSubscribe(this);
64 }
65 }
66
67 @Override
68 public void onSuccess(T t) {
69 downstream.onSuccess(t);
70
71 onAfterTerminate();
72 }
73
74 @Override
75 public void onError(Throwable e) {
76 downstream.onError(e);
77
78 onAfterTerminate();
79 }
80
81 @Override
82 public void dispose() {
83 upstream.dispose();
84 }
85
86 @Override
87 public boolean isDisposed() {
88 return upstream.isDisposed();
89 }
90
91 private void onAfterTerminate() {
92 try {
93 onAfterTerminate.run();
94 } catch (Throwable ex) {
95 Exceptions.throwIfFatal(ex);
96 RxJavaPlugins.onError(ex);
97 }
98 }
99 }
100 }