| 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.completable; |
| 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 onError, onComplete or a dispose event. |
| 27 |
* <p>History: 2.0.1 - experimental |
| 28 |
* @since 2.1 |
| 29 |
*/ |
| 30 |
public final class CompletableDoFinally extends Completable { |
| 31 |
|
| 32 |
final CompletableSource source; |
| 33 |
|
| 34 |
final Action onFinally; |
| 35 |
|
| 36 |
public CompletableDoFinally(CompletableSource source, Action onFinally) { |
| 37 |
this.source = source; |
| 38 |
this.onFinally = onFinally; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected void subscribeActual(CompletableObserver observer) { |
| 43 |
source.subscribe(new DoFinallyObserver(observer, onFinally)); |
| 44 |
} |
| 45 |
|
| 46 |
static final class DoFinallyObserver extends AtomicInteger implements CompletableObserver, Disposable { |
| 47 |
|
| 48 |
private static final long serialVersionUID = 4109457741734051389L; |
| 49 |
|
| 50 |
final CompletableObserver downstream; |
| 51 |
|
| 52 |
final Action onFinally; |
| 53 |
|
| 54 |
Disposable upstream; |
| 55 |
|
| 56 |
DoFinallyObserver(CompletableObserver actual, Action onFinally) { |
| 57 |
this.downstream = actual; |
| 58 |
this.onFinally = onFinally; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void onSubscribe(Disposable d) { |
| 63 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 64 |
this.upstream = d; |
| 65 |
|
| 66 |
downstream.onSubscribe(this); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void onError(Throwable t) { |
| 72 |
downstream.onError(t); |
| 73 |
runFinally(); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void onComplete() { |
| 78 |
downstream.onComplete(); |
| 79 |
runFinally(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void dispose() { |
| 84 |
upstream.dispose(); |
| 85 |
runFinally(); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public boolean isDisposed() { |
| 90 |
return upstream.isDisposed(); |
| 91 |
} |
| 92 |
|
| 93 |
void runFinally() { |
| 94 |
if (compareAndSet(0, 1)) { |
| 95 |
try { |
| 96 |
onFinally.run(); |
| 97 |
} catch (Throwable ex) { |
| 98 |
Exceptions.throwIfFatal(ex); |
| 99 |
RxJavaPlugins.onError(ex); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |