| 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.mixed; |
| 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 |
/** |
| 23 |
* After Completable completes, it relays the signals |
| 24 |
* of the ObservableSource to the downstream observer. |
| 25 |
* |
| 26 |
* @param <R> the result type of the ObservableSource and this operator |
| 27 |
* @since 2.1.15 |
| 28 |
*/ |
| 29 |
public final class CompletableAndThenObservable<R> extends Observable<R> { |
| 30 |
|
| 31 |
final CompletableSource source; |
| 32 |
|
| 33 |
final ObservableSource<? extends R> other; |
| 34 |
|
| 35 |
public CompletableAndThenObservable(CompletableSource source, |
| 36 |
ObservableSource<? extends R> other) { |
| 37 |
this.source = source; |
| 38 |
this.other = other; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected void subscribeActual(Observer<? super R> observer) { |
| 43 |
AndThenObservableObserver<R> parent = new AndThenObservableObserver<>(observer, other); |
| 44 |
observer.onSubscribe(parent); |
| 45 |
source.subscribe(parent); |
| 46 |
} |
| 47 |
|
| 48 |
static final class AndThenObservableObserver<R> |
| 49 |
extends AtomicReference<Disposable> |
| 50 |
implements Observer<R>, CompletableObserver, Disposable { |
| 51 |
|
| 52 |
private static final long serialVersionUID = -8948264376121066672L; |
| 53 |
|
| 54 |
final Observer<? super R> downstream; |
| 55 |
|
| 56 |
ObservableSource<? extends R> other; |
| 57 |
|
| 58 |
AndThenObservableObserver(Observer<? super R> downstream, ObservableSource<? extends R> other) { |
| 59 |
this.other = other; |
| 60 |
this.downstream = downstream; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void onNext(R t) { |
| 65 |
downstream.onNext(t); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onError(Throwable t) { |
| 70 |
downstream.onError(t); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void onComplete() { |
| 75 |
ObservableSource<? extends R> o = other; |
| 76 |
if (o == null) { |
| 77 |
downstream.onComplete(); |
| 78 |
} else { |
| 79 |
other = null; |
| 80 |
o.subscribe(this); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void dispose() { |
| 86 |
DisposableHelper.dispose(this); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public boolean isDisposed() { |
| 91 |
return DisposableHelper.isDisposed(get()); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void onSubscribe(Disposable d) { |
| 96 |
DisposableHelper.replace(this, d); |
| 97 |
} |
| 98 |
|
| 99 |
} |
| 100 |
} |