|
| 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.internal.operators.mixed; |
| 15 | + |
| 16 | +import java.util.concurrent.atomic.AtomicReference; |
| 17 | + |
| 18 | +import org.reactivestreams.Subscription; |
| 19 | + |
| 20 | +import io.reactivex.*; |
| 21 | +import io.reactivex.annotations.Experimental; |
| 22 | +import io.reactivex.disposables.Disposable; |
| 23 | +import io.reactivex.exceptions.Exceptions; |
| 24 | +import io.reactivex.functions.Function; |
| 25 | +import io.reactivex.internal.disposables.DisposableHelper; |
| 26 | +import io.reactivex.internal.functions.ObjectHelper; |
| 27 | +import io.reactivex.internal.subscriptions.SubscriptionHelper; |
| 28 | +import io.reactivex.internal.util.*; |
| 29 | +import io.reactivex.plugins.RxJavaPlugins; |
| 30 | + |
| 31 | +/** |
| 32 | + * Maps the upstream values into {@link CompletableSource}s, subscribes to the newer one while |
| 33 | + * disposing the subscription to the previous {@code CompletableSource}, thus keeping at most one |
| 34 | + * active {@code CompletableSource} running. |
| 35 | + * |
| 36 | + * @param <T> the upstream value type |
| 37 | + * @since 2.1.11 - experimental |
| 38 | + */ |
| 39 | +@Experimental |
| 40 | +public final class FlowableSwitchMapCompletable<T> extends Completable { |
| 41 | + |
| 42 | + final Flowable<T> source; |
| 43 | + |
| 44 | + final Function<? super T, ? extends CompletableSource> mapper; |
| 45 | + |
| 46 | + final boolean delayErrors; |
| 47 | + |
| 48 | + public FlowableSwitchMapCompletable(Flowable<T> source, |
| 49 | + Function<? super T, ? extends CompletableSource> mapper, boolean delayErrors) { |
| 50 | + this.source = source; |
| 51 | + this.mapper = mapper; |
| 52 | + this.delayErrors = delayErrors; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void subscribeActual(CompletableObserver s) { |
| 57 | + source.subscribe(new SwitchMapCompletableObserver<T>(s, mapper, delayErrors)); |
| 58 | + } |
| 59 | + |
| 60 | + static final class SwitchMapCompletableObserver<T> implements FlowableSubscriber<T>, Disposable { |
| 61 | + |
| 62 | + final CompletableObserver downstream; |
| 63 | + |
| 64 | + final Function<? super T, ? extends CompletableSource> mapper; |
| 65 | + |
| 66 | + final boolean delayErrors; |
| 67 | + |
| 68 | + final AtomicThrowable errors; |
| 69 | + |
| 70 | + final AtomicReference<SwitchMapInnerObserver> inner; |
| 71 | + |
| 72 | + static final SwitchMapInnerObserver INNER_DISPOSED = new SwitchMapInnerObserver(null); |
| 73 | + |
| 74 | + volatile boolean done; |
| 75 | + |
| 76 | + Subscription upstream; |
| 77 | + |
| 78 | + SwitchMapCompletableObserver(CompletableObserver downstream, |
| 79 | + Function<? super T, ? extends CompletableSource> mapper, boolean delayErrors) { |
| 80 | + this.downstream = downstream; |
| 81 | + this.mapper = mapper; |
| 82 | + this.delayErrors = delayErrors; |
| 83 | + this.errors = new AtomicThrowable(); |
| 84 | + this.inner = new AtomicReference<SwitchMapInnerObserver>(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void onSubscribe(Subscription s) { |
| 89 | + if (SubscriptionHelper.validate(upstream, s)) { |
| 90 | + this.upstream = s; |
| 91 | + downstream.onSubscribe(this); |
| 92 | + s.request(Long.MAX_VALUE); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onNext(T t) { |
| 98 | + CompletableSource c; |
| 99 | + |
| 100 | + try { |
| 101 | + c = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null CompletableSource"); |
| 102 | + } catch (Throwable ex) { |
| 103 | + Exceptions.throwIfFatal(ex); |
| 104 | + upstream.cancel(); |
| 105 | + onError(ex); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + SwitchMapInnerObserver o = new SwitchMapInnerObserver(this); |
| 110 | + |
| 111 | + for (;;) { |
| 112 | + SwitchMapInnerObserver current = inner.get(); |
| 113 | + if (current == INNER_DISPOSED) { |
| 114 | + break; |
| 115 | + } |
| 116 | + if (inner.compareAndSet(current, o)) { |
| 117 | + if (current != null) { |
| 118 | + current.dispose(); |
| 119 | + } |
| 120 | + c.subscribe(o); |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void onError(Throwable t) { |
| 128 | + if (errors.addThrowable(t)) { |
| 129 | + if (delayErrors) { |
| 130 | + onComplete(); |
| 131 | + } else { |
| 132 | + disposeInner(); |
| 133 | + Throwable ex = errors.terminate(); |
| 134 | + if (ex != ExceptionHelper.TERMINATED) { |
| 135 | + downstream.onError(ex); |
| 136 | + } |
| 137 | + } |
| 138 | + } else { |
| 139 | + RxJavaPlugins.onError(t); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void onComplete() { |
| 145 | + done = true; |
| 146 | + if (inner.get() == null) { |
| 147 | + Throwable ex = errors.terminate(); |
| 148 | + if (ex == null) { |
| 149 | + downstream.onComplete(); |
| 150 | + } else { |
| 151 | + downstream.onError(ex); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + void disposeInner() { |
| 157 | + SwitchMapInnerObserver o = inner.getAndSet(INNER_DISPOSED); |
| 158 | + if (o != null && o != INNER_DISPOSED) { |
| 159 | + o.dispose(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void dispose() { |
| 165 | + upstream.cancel(); |
| 166 | + disposeInner(); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public boolean isDisposed() { |
| 171 | + return inner.get() == INNER_DISPOSED; |
| 172 | + } |
| 173 | + |
| 174 | + void innerError(SwitchMapInnerObserver sender, Throwable error) { |
| 175 | + if (inner.compareAndSet(sender, null)) { |
| 176 | + if (errors.addThrowable(error)) { |
| 177 | + if (delayErrors) { |
| 178 | + if (done) { |
| 179 | + Throwable ex = errors.terminate(); |
| 180 | + downstream.onError(ex); |
| 181 | + } |
| 182 | + } else { |
| 183 | + dispose(); |
| 184 | + Throwable ex = errors.terminate(); |
| 185 | + if (ex != ExceptionHelper.TERMINATED) { |
| 186 | + downstream.onError(ex); |
| 187 | + } |
| 188 | + } |
| 189 | + return; |
| 190 | + } |
| 191 | + } |
| 192 | + RxJavaPlugins.onError(error); |
| 193 | + } |
| 194 | + |
| 195 | + void innerComplete(SwitchMapInnerObserver sender) { |
| 196 | + if (inner.compareAndSet(sender, null)) { |
| 197 | + if (done) { |
| 198 | + Throwable ex = errors.terminate(); |
| 199 | + if (ex == null) { |
| 200 | + downstream.onComplete(); |
| 201 | + } else { |
| 202 | + downstream.onError(ex); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + static final class SwitchMapInnerObserver extends AtomicReference<Disposable> |
| 209 | + implements CompletableObserver { |
| 210 | + |
| 211 | + private static final long serialVersionUID = -8003404460084760287L; |
| 212 | + |
| 213 | + final SwitchMapCompletableObserver<?> parent; |
| 214 | + |
| 215 | + SwitchMapInnerObserver(SwitchMapCompletableObserver<?> parent) { |
| 216 | + this.parent = parent; |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public void onSubscribe(Disposable d) { |
| 221 | + DisposableHelper.setOnce(this, d); |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public void onError(Throwable e) { |
| 226 | + parent.innerError(this, e); |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public void onComplete() { |
| 231 | + parent.innerComplete(this); |
| 232 | + } |
| 233 | + |
| 234 | + void dispose() { |
| 235 | + DisposableHelper.dispose(this); |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments