Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleDoAfterSuccess.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.Consumer;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 /**
24 * Calls a consumer after pushing the current item to the downstream.
25 * <p>History: 2.0.1 - experimental
26 * @param <T> the value type
27 * @since 2.1
28 */
29 public final class SingleDoAfterSuccess<T> extends Single<T> {
30
31 final SingleSource<T> source;
32
33 final Consumer<? super T> onAfterSuccess;
34
35 public SingleDoAfterSuccess(SingleSource<T> source, Consumer<? super T> onAfterSuccess) {
36 this.source = source;
37 this.onAfterSuccess = onAfterSuccess;
38 }
39
40 @Override
41 protected void subscribeActual(SingleObserver<? super T> observer) {
42 source.subscribe(new DoAfterObserver<>(observer, onAfterSuccess));
43 }
44
45 static final class DoAfterObserver<T> implements SingleObserver<T>, Disposable {
46
47 final SingleObserver<? super T> downstream;
48
49 final Consumer<? super T> onAfterSuccess;
50
51 Disposable upstream;
52
53 DoAfterObserver(SingleObserver<? super T> actual, Consumer<? super T> onAfterSuccess) {
54 this.downstream = actual;
55 this.onAfterSuccess = onAfterSuccess;
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 try {
72 onAfterSuccess.accept(t);
73 } catch (Throwable ex) {
74 Exceptions.throwIfFatal(ex);
75 // remember, onSuccess is a terminal event and we can't call onError
76 RxJavaPlugins.onError(ex);
77 }
78 }
79
80 @Override
81 public void onError(Throwable e) {
82 downstream.onError(e);
83 }
84
85 @Override
86 public void dispose() {
87 upstream.dispose();
88 }
89
90 @Override
91 public boolean isDisposed() {
92 return upstream.isDisposed();
93 }
94 }
95 }