| 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.observable; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.disposables.*; |
| 22 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 23 |
import io.reactivex.rxjava3.functions.Function; |
| 24 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 25 |
import io.reactivex.rxjava3.internal.observers.BasicIntQueueDisposable; |
| 26 |
import io.reactivex.rxjava3.internal.util.AtomicThrowable; |
| 27 |
|
| 28 |
/** |
| 29 |
* Maps a sequence of values into CompletableSources and awaits their termination. |
| 30 |
* @param <T> the value type |
| 31 |
*/ |
| 32 |
public final class ObservableFlatMapCompletable<T> extends AbstractObservableWithUpstream<T, T> { |
| 33 |
|
| 34 |
final Function<? super T, ? extends CompletableSource> mapper; |
| 35 |
|
| 36 |
final boolean delayErrors; |
| 37 |
|
| 38 |
public ObservableFlatMapCompletable(ObservableSource<T> source, |
| 39 |
Function<? super T, ? extends CompletableSource> mapper, boolean delayErrors) { |
| 40 |
super(source); |
| 41 |
this.mapper = mapper; |
| 42 |
this.delayErrors = delayErrors; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
protected void subscribeActual(Observer<? super T> observer) { |
| 47 |
source.subscribe(new FlatMapCompletableMainObserver<>(observer, mapper, delayErrors)); |
| 48 |
} |
| 49 |
|
| 50 |
static final class FlatMapCompletableMainObserver<T> extends BasicIntQueueDisposable<T> |
| 51 |
implements Observer<T> { |
| 52 |
private static final long serialVersionUID = 8443155186132538303L; |
| 53 |
|
| 54 |
final Observer<? super T> downstream; |
| 55 |
|
| 56 |
final AtomicThrowable errors; |
| 57 |
|
| 58 |
final Function<? super T, ? extends CompletableSource> mapper; |
| 59 |
|
| 60 |
final boolean delayErrors; |
| 61 |
|
| 62 |
final CompositeDisposable set; |
| 63 |
|
| 64 |
Disposable upstream; |
| 65 |
|
| 66 |
volatile boolean disposed; |
| 67 |
|
| 68 |
FlatMapCompletableMainObserver(Observer<? super T> observer, Function<? super T, ? extends CompletableSource> mapper, boolean delayErrors) { |
| 69 |
this.downstream = observer; |
| 70 |
this.mapper = mapper; |
| 71 |
this.delayErrors = delayErrors; |
| 72 |
this.errors = new AtomicThrowable(); |
| 73 |
this.set = new CompositeDisposable(); |
| 74 |
this.lazySet(1); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void onSubscribe(Disposable d) { |
| 79 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 80 |
this.upstream = d; |
| 81 |
|
| 82 |
downstream.onSubscribe(this); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void onNext(T value) { |
| 88 |
CompletableSource cs; |
| 89 |
|
| 90 |
try { |
| 91 |
cs = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null CompletableSource"); |
| 92 |
} catch (Throwable ex) { |
| 93 |
Exceptions.throwIfFatal(ex); |
| 94 |
upstream.dispose(); |
| 95 |
onError(ex); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
getAndIncrement(); |
| 100 |
|
| 101 |
InnerObserver inner = new InnerObserver(); |
| 102 |
|
| 103 |
if (!disposed && set.add(inner)) { |
| 104 |
cs.subscribe(inner); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public void onError(Throwable e) { |
| 110 |
if (errors.tryAddThrowableOrReport(e)) { |
| 111 |
if (delayErrors) { |
| 112 |
if (decrementAndGet() == 0) { |
| 113 |
errors.tryTerminateConsumer(downstream); |
| 114 |
} |
| 115 |
} else { |
| 116 |
disposed = true; |
| 117 |
upstream.dispose(); |
| 118 |
set.dispose(); |
| 119 |
errors.tryTerminateConsumer(downstream); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public void onComplete() { |
| 126 |
if (decrementAndGet() == 0) { |
| 127 |
errors.tryTerminateConsumer(downstream); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void dispose() { |
| 133 |
disposed = true; |
| 134 |
upstream.dispose(); |
| 135 |
set.dispose(); |
| 136 |
errors.tryTerminateAndReport(); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public boolean isDisposed() { |
| 141 |
return upstream.isDisposed(); |
| 142 |
} |
| 143 |
|
| 144 |
@Nullable |
| 145 |
@Override |
| 146 |
public T poll() { |
| 147 |
return null; // always empty |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public boolean isEmpty() { |
| 152 |
return true; // always empty |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void clear() { |
| 157 |
// nothing to clear |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public int requestFusion(int mode) { |
| 162 |
return mode & ASYNC; |
| 163 |
} |
| 164 |
|
| 165 |
void innerComplete(InnerObserver inner) { |
| 166 |
set.delete(inner); |
| 167 |
onComplete(); |
| 168 |
} |
| 169 |
|
| 170 |
void innerError(InnerObserver inner, Throwable e) { |
| 171 |
set.delete(inner); |
| 172 |
onError(e); |
| 173 |
} |
| 174 |
|
| 175 |
final class InnerObserver extends AtomicReference<Disposable> implements CompletableObserver, Disposable { |
| 176 |
private static final long serialVersionUID = 8606673141535671828L; |
| 177 |
|
| 178 |
@Override |
| 179 |
public void onSubscribe(Disposable d) { |
| 180 |
DisposableHelper.setOnce(this, d); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void onComplete() { |
| 185 |
innerComplete(this); |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public void onError(Throwable e) { |
| 190 |
innerError(this, e); |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public void dispose() { |
| 195 |
DisposableHelper.dispose(this); |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
public boolean isDisposed() { |
| 200 |
return DisposableHelper.isDisposed(get()); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |