| 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.Objects; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 21 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 22 |
import io.reactivex.rxjava3.functions.Function; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 |
|
| 25 |
/** |
| 26 |
* Maps the success value of the source MaybeSource into a Completable. |
| 27 |
* @param <T> the value type of the source MaybeSource |
| 28 |
*/ |
| 29 |
public final class MaybeFlatMapCompletable<T> extends Completable { |
| 30 |
|
| 31 |
final MaybeSource<T> source; |
| 32 |
|
| 33 |
final Function<? super T, ? extends CompletableSource> mapper; |
| 34 |
|
| 35 |
public MaybeFlatMapCompletable(MaybeSource<T> source, Function<? super T, ? extends CompletableSource> mapper) { |
| 36 |
this.source = source; |
| 37 |
this.mapper = mapper; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void subscribeActual(CompletableObserver observer) { |
| 42 |
FlatMapCompletableObserver<T> parent = new FlatMapCompletableObserver<>(observer, mapper); |
| 43 |
observer.onSubscribe(parent); |
| 44 |
source.subscribe(parent); |
| 45 |
} |
| 46 |
|
| 47 |
static final class FlatMapCompletableObserver<T> |
| 48 |
extends AtomicReference<Disposable> |
| 49 |
implements MaybeObserver<T>, CompletableObserver, Disposable { |
| 50 |
|
| 51 |
private static final long serialVersionUID = -2177128922851101253L; |
| 52 |
|
| 53 |
final CompletableObserver downstream; |
| 54 |
|
| 55 |
final Function<? super T, ? extends CompletableSource> mapper; |
| 56 |
|
| 57 |
FlatMapCompletableObserver(CompletableObserver actual, |
| 58 |
Function<? super T, ? extends CompletableSource> mapper) { |
| 59 |
this.downstream = actual; |
| 60 |
this.mapper = mapper; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void dispose() { |
| 65 |
DisposableHelper.dispose(this); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public boolean isDisposed() { |
| 70 |
return DisposableHelper.isDisposed(get()); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void onSubscribe(Disposable d) { |
| 75 |
DisposableHelper.replace(this, d); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void onSuccess(T value) { |
| 80 |
CompletableSource cs; |
| 81 |
|
| 82 |
try { |
| 83 |
cs = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null CompletableSource"); |
| 84 |
} catch (Throwable ex) { |
| 85 |
Exceptions.throwIfFatal(ex); |
| 86 |
onError(ex); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if (!isDisposed()) { |
| 91 |
cs.subscribe(this); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void onError(Throwable e) { |
| 97 |
downstream.onError(e); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void onComplete() { |
| 102 |
downstream.onComplete(); |
| 103 |
} |
| 104 |
} |
| 105 |
} |