| 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 java.util.concurrent.atomic.AtomicInteger; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 21 |
import io.reactivex.rxjava3.functions.Action; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 23 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 24 |
|
| 25 |
/** |
| 26 |
* Execute an action after an onSuccess, onError, onComplete or a dispose event. |
| 27 |
* <p>History: 2.0.1 - experimental |
| 28 |
* @param <T> the value type |
| 29 |
* @since 2.1 |
| 30 |
*/ |
| 31 |
public final class MaybeDoFinally<T> extends AbstractMaybeWithUpstream<T, T> { |
| 32 |
|
| 33 |
final Action onFinally; |
| 34 |
|
| 35 |
public MaybeDoFinally(MaybeSource<T> source, Action onFinally) { |
| 36 |
super(source); |
| 37 |
this.onFinally = onFinally; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void subscribeActual(MaybeObserver<? super T> observer) { |
| 42 |
source.subscribe(new DoFinallyObserver<>(observer, onFinally)); |
| 43 |
} |
| 44 |
|
| 45 |
static final class DoFinallyObserver<T> extends AtomicInteger implements MaybeObserver<T>, Disposable { |
| 46 |
|
| 47 |
private static final long serialVersionUID = 4109457741734051389L; |
| 48 |
|
| 49 |
final MaybeObserver<? super T> downstream; |
| 50 |
|
| 51 |
final Action onFinally; |
| 52 |
|
| 53 |
Disposable upstream; |
| 54 |
|
| 55 |
DoFinallyObserver(MaybeObserver<? super T> actual, Action onFinally) { |
| 56 |
this.downstream = actual; |
| 57 |
this.onFinally = onFinally; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void onSubscribe(Disposable d) { |
| 62 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 63 |
this.upstream = d; |
| 64 |
|
| 65 |
downstream.onSubscribe(this); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void onSuccess(T t) { |
| 71 |
downstream.onSuccess(t); |
| 72 |
runFinally(); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void onError(Throwable t) { |
| 77 |
downstream.onError(t); |
| 78 |
runFinally(); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void onComplete() { |
| 83 |
downstream.onComplete(); |
| 84 |
runFinally(); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void dispose() { |
| 89 |
upstream.dispose(); |
| 90 |
runFinally(); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public boolean isDisposed() { |
| 95 |
return upstream.isDisposed(); |
| 96 |
} |
| 97 |
|
| 98 |
void runFinally() { |
| 99 |
if (compareAndSet(0, 1)) { |
| 100 |
try { |
| 101 |
onFinally.run(); |
| 102 |
} catch (Throwable ex) { |
| 103 |
Exceptions.throwIfFatal(ex); |
| 104 |
RxJavaPlugins.onError(ex); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |