| 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 static io.reactivex.rxjava3.internal.util.ExceptionHelper.timeoutMessage; |
| 17 |
|
| 18 |
import java.util.concurrent.*; |
| 19 |
import java.util.concurrent.atomic.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 24 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 25 |
|
| 26 |
public final class ObservableTimeoutTimed<T> extends AbstractObservableWithUpstream<T, T> { |
| 27 |
final long timeout; |
| 28 |
final TimeUnit unit; |
| 29 |
final Scheduler scheduler; |
| 30 |
final ObservableSource<? extends T> other; |
| 31 |
|
| 32 |
public ObservableTimeoutTimed(Observable<T> source, |
| 33 |
long timeout, TimeUnit unit, Scheduler scheduler, ObservableSource<? extends T> other) { |
| 34 |
super(source); |
| 35 |
this.timeout = timeout; |
| 36 |
this.unit = unit; |
| 37 |
this.scheduler = scheduler; |
| 38 |
this.other = other; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected void subscribeActual(Observer<? super T> observer) { |
| 43 |
if (other == null) { |
| 44 |
TimeoutObserver<T> parent = new TimeoutObserver<>(observer, timeout, unit, scheduler.createWorker()); |
| 45 |
observer.onSubscribe(parent); |
| 46 |
parent.startTimeout(0L); |
| 47 |
source.subscribe(parent); |
| 48 |
} else { |
| 49 |
TimeoutFallbackObserver<T> parent = new TimeoutFallbackObserver<>(observer, timeout, unit, scheduler.createWorker(), other); |
| 50 |
observer.onSubscribe(parent); |
| 51 |
parent.startTimeout(0L); |
| 52 |
source.subscribe(parent); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
static final class TimeoutObserver<T> extends AtomicLong |
| 57 |
implements Observer<T>, Disposable, TimeoutSupport { |
| 58 |
|
| 59 |
private static final long serialVersionUID = 3764492702657003550L; |
| 60 |
|
| 61 |
final Observer<? super T> downstream; |
| 62 |
|
| 63 |
final long timeout; |
| 64 |
|
| 65 |
final TimeUnit unit; |
| 66 |
|
| 67 |
final Scheduler.Worker worker; |
| 68 |
|
| 69 |
final SequentialDisposable task; |
| 70 |
|
| 71 |
final AtomicReference<Disposable> upstream; |
| 72 |
|
| 73 |
TimeoutObserver(Observer<? super T> actual, long timeout, TimeUnit unit, Scheduler.Worker worker) { |
| 74 |
this.downstream = actual; |
| 75 |
this.timeout = timeout; |
| 76 |
this.unit = unit; |
| 77 |
this.worker = worker; |
| 78 |
this.task = new SequentialDisposable(); |
| 79 |
this.upstream = new AtomicReference<>(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void onSubscribe(Disposable d) { |
| 84 |
DisposableHelper.setOnce(upstream, d); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void onNext(T t) { |
| 89 |
long idx = get(); |
| 90 |
if (idx == Long.MAX_VALUE || !compareAndSet(idx, idx + 1)) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
task.get().dispose(); |
| 95 |
|
| 96 |
downstream.onNext(t); |
| 97 |
|
| 98 |
startTimeout(idx + 1); |
| 99 |
} |
| 100 |
|
| 101 |
void startTimeout(long nextIndex) { |
| 102 |
task.replace(worker.schedule(new TimeoutTask(nextIndex, this), timeout, unit)); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void onError(Throwable t) { |
| 107 |
if (getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) { |
| 108 |
task.dispose(); |
| 109 |
|
| 110 |
downstream.onError(t); |
| 111 |
|
| 112 |
worker.dispose(); |
| 113 |
} else { |
| 114 |
RxJavaPlugins.onError(t); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void onComplete() { |
| 120 |
if (getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) { |
| 121 |
task.dispose(); |
| 122 |
|
| 123 |
downstream.onComplete(); |
| 124 |
|
| 125 |
worker.dispose(); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void onTimeout(long idx) { |
| 131 |
if (compareAndSet(idx, Long.MAX_VALUE)) { |
| 132 |
DisposableHelper.dispose(upstream); |
| 133 |
|
| 134 |
downstream.onError(new TimeoutException(timeoutMessage(timeout, unit))); |
| 135 |
|
| 136 |
worker.dispose(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public void dispose() { |
| 142 |
DisposableHelper.dispose(upstream); |
| 143 |
worker.dispose(); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public boolean isDisposed() { |
| 148 |
return DisposableHelper.isDisposed(upstream.get()); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
static final class TimeoutTask implements Runnable { |
| 153 |
|
| 154 |
final TimeoutSupport parent; |
| 155 |
|
| 156 |
final long idx; |
| 157 |
|
| 158 |
TimeoutTask(long idx, TimeoutSupport parent) { |
| 159 |
this.idx = idx; |
| 160 |
this.parent = parent; |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
public void run() { |
| 165 |
parent.onTimeout(idx); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
static final class TimeoutFallbackObserver<T> extends AtomicReference<Disposable> |
| 170 |
implements Observer<T>, Disposable, TimeoutSupport { |
| 171 |
|
| 172 |
private static final long serialVersionUID = 3764492702657003550L; |
| 173 |
|
| 174 |
final Observer<? super T> downstream; |
| 175 |
|
| 176 |
final long timeout; |
| 177 |
|
| 178 |
final TimeUnit unit; |
| 179 |
|
| 180 |
final Scheduler.Worker worker; |
| 181 |
|
| 182 |
final SequentialDisposable task; |
| 183 |
|
| 184 |
final AtomicLong index; |
| 185 |
|
| 186 |
final AtomicReference<Disposable> upstream; |
| 187 |
|
| 188 |
ObservableSource<? extends T> fallback; |
| 189 |
|
| 190 |
TimeoutFallbackObserver(Observer<? super T> actual, long timeout, TimeUnit unit, |
| 191 |
Scheduler.Worker worker, ObservableSource<? extends T> fallback) { |
| 192 |
this.downstream = actual; |
| 193 |
this.timeout = timeout; |
| 194 |
this.unit = unit; |
| 195 |
this.worker = worker; |
| 196 |
this.fallback = fallback; |
| 197 |
this.task = new SequentialDisposable(); |
| 198 |
this.index = new AtomicLong(); |
| 199 |
this.upstream = new AtomicReference<>(); |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public void onSubscribe(Disposable d) { |
| 204 |
DisposableHelper.setOnce(upstream, d); |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public void onNext(T t) { |
| 209 |
long idx = index.get(); |
| 210 |
if (idx == Long.MAX_VALUE || !index.compareAndSet(idx, idx + 1)) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
task.get().dispose(); |
| 215 |
|
| 216 |
downstream.onNext(t); |
| 217 |
|
| 218 |
startTimeout(idx + 1); |
| 219 |
} |
| 220 |
|
| 221 |
void startTimeout(long nextIndex) { |
| 222 |
task.replace(worker.schedule(new TimeoutTask(nextIndex, this), timeout, unit)); |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public void onError(Throwable t) { |
| 227 |
if (index.getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) { |
| 228 |
task.dispose(); |
| 229 |
|
| 230 |
downstream.onError(t); |
| 231 |
|
| 232 |
worker.dispose(); |
| 233 |
} else { |
| 234 |
RxJavaPlugins.onError(t); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public void onComplete() { |
| 240 |
if (index.getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) { |
| 241 |
task.dispose(); |
| 242 |
|
| 243 |
downstream.onComplete(); |
| 244 |
|
| 245 |
worker.dispose(); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public void onTimeout(long idx) { |
| 251 |
if (index.compareAndSet(idx, Long.MAX_VALUE)) { |
| 252 |
DisposableHelper.dispose(upstream); |
| 253 |
|
| 254 |
ObservableSource<? extends T> f = fallback; |
| 255 |
fallback = null; |
| 256 |
|
| 257 |
f.subscribe(new FallbackObserver<T>(downstream, this)); |
| 258 |
|
| 259 |
worker.dispose(); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
@Override |
| 264 |
public void dispose() { |
| 265 |
DisposableHelper.dispose(upstream); |
| 266 |
DisposableHelper.dispose(this); |
| 267 |
worker.dispose(); |
| 268 |
} |
| 269 |
|
| 270 |
@Override |
| 271 |
public boolean isDisposed() { |
| 272 |
return DisposableHelper.isDisposed(get()); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static final class FallbackObserver<T> implements Observer<T> { |
| 277 |
|
| 278 |
final Observer<? super T> downstream; |
| 279 |
|
| 280 |
final AtomicReference<Disposable> arbiter; |
| 281 |
|
| 282 |
FallbackObserver(Observer<? super T> actual, AtomicReference<Disposable> arbiter) { |
| 283 |
this.downstream = actual; |
| 284 |
this.arbiter = arbiter; |
| 285 |
} |
| 286 |
|
| 287 |
@Override |
| 288 |
public void onSubscribe(Disposable d) { |
| 289 |
DisposableHelper.replace(arbiter, d); |
| 290 |
} |
| 291 |
|
| 292 |
@Override |
| 293 |
public void onNext(T t) { |
| 294 |
downstream.onNext(t); |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public void onError(Throwable t) { |
| 299 |
downstream.onError(t); |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public void onComplete() { |
| 304 |
downstream.onComplete(); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
interface TimeoutSupport { |
| 309 |
|
| 310 |
void onTimeout(long idx); |
| 311 |
|
| 312 |
} |
| 313 |
} |