Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeDoAfterSuccess.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.maybe;
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 MaybeDoAfterSuccess<T> extends AbstractMaybeWithUpstream<T, T> {
30
31 final Consumer<? super T> onAfterSuccess;
32
33 public MaybeDoAfterSuccess(MaybeSource<T> source, Consumer<? super T> onAfterSuccess) {
34 super(source);
35 this.onAfterSuccess = onAfterSuccess;
36 }
37
38 @Override
39 protected void subscribeActual(MaybeObserver<? super T> observer) {
40 source.subscribe(new DoAfterObserver<>(observer, onAfterSuccess));
41 }
42
43 static final class DoAfterObserver<T> implements MaybeObserver<T>, Disposable {
44
45 final MaybeObserver<? super T> downstream;
46
47 final Consumer<? super T> onAfterSuccess;
48
49 Disposable upstream;
50
51 DoAfterObserver(MaybeObserver<? super T> actual, Consumer<? super T> onAfterSuccess) {
52 this.downstream = actual;
53 this.onAfterSuccess = onAfterSuccess;
54 }
55
56 @Override
57 public void onSubscribe(Disposable d) {
58 if (DisposableHelper.validate(this.upstream, d)) {
59 this.upstream = d;
60
61 downstream.onSubscribe(this);
62 }
63 }
64
65 @Override
66 public void onSuccess(T t) {
67 downstream.onSuccess(t);
68
69 try {
70 onAfterSuccess.accept(t);
71 } catch (Throwable ex) {
72 Exceptions.throwIfFatal(ex);
73 // remember, onSuccess is a terminal event and we can't call onError
74 RxJavaPlugins.onError(ex);
75 }
76 }
77
78 @Override
79 public void onError(Throwable e) {
80 downstream.onError(e);
81 }
82
83 @Override
84 public void onComplete() {
85 downstream.onComplete();
86 }
87
88 @Override
89 public void dispose() {
90 upstream.dispose();
91 }
92
93 @Override
94 public boolean isDisposed() {
95 return upstream.isDisposed();
96 }
97 }
98 }