| 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.AtomicReference; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 21 |
|
| 22 |
public final class CompletableAndThenCompletable extends Completable { |
| 23 |
|
| 24 |
final CompletableSource source; |
| 25 |
|
| 26 |
final CompletableSource next; |
| 27 |
|
| 28 |
public CompletableAndThenCompletable(CompletableSource source, CompletableSource next) { |
| 29 |
this.source = source; |
| 30 |
this.next = next; |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
protected void subscribeActual(CompletableObserver observer) { |
| 35 |
source.subscribe(new SourceObserver(observer, next)); |
| 36 |
} |
| 37 |
|
| 38 |
static final class SourceObserver |
| 39 |
extends AtomicReference<Disposable> |
| 40 |
implements CompletableObserver, Disposable { |
| 41 |
|
| 42 |
private static final long serialVersionUID = -4101678820158072998L; |
| 43 |
|
| 44 |
final CompletableObserver actualObserver; |
| 45 |
|
| 46 |
final CompletableSource next; |
| 47 |
|
| 48 |
SourceObserver(CompletableObserver actualObserver, CompletableSource next) { |
| 49 |
this.actualObserver = actualObserver; |
| 50 |
this.next = next; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void onSubscribe(Disposable d) { |
| 55 |
if (DisposableHelper.setOnce(this, d)) { |
| 56 |
actualObserver.onSubscribe(this); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void onError(Throwable e) { |
| 62 |
actualObserver.onError(e); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void onComplete() { |
| 67 |
next.subscribe(new NextObserver(this, actualObserver)); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void dispose() { |
| 72 |
DisposableHelper.dispose(this); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public boolean isDisposed() { |
| 77 |
return DisposableHelper.isDisposed(get()); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
static final class NextObserver implements CompletableObserver { |
| 82 |
|
| 83 |
final AtomicReference<Disposable> parent; |
| 84 |
|
| 85 |
final CompletableObserver downstream; |
| 86 |
|
| 87 |
NextObserver(AtomicReference<Disposable> parent, CompletableObserver downstream) { |
| 88 |
this.parent = parent; |
| 89 |
this.downstream = downstream; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onSubscribe(Disposable d) { |
| 94 |
DisposableHelper.replace(parent, d); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onComplete() { |
| 99 |
downstream.onComplete(); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void onError(Throwable e) { |
| 104 |
downstream.onError(e); |
| 105 |
} |
| 106 |
} |
| 107 |
} |