| 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.concurrent.atomic.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 21 |
import io.reactivex.rxjava3.internal.util.*; |
| 22 |
|
| 23 |
public final class ObservableTakeUntil<T, U> extends AbstractObservableWithUpstream<T, T> { |
| 24 |
|
| 25 |
final ObservableSource<? extends U> other; |
| 26 |
|
| 27 |
public ObservableTakeUntil(ObservableSource<T> source, ObservableSource<? extends U> other) { |
| 28 |
super(source); |
| 29 |
this.other = other; |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
public void subscribeActual(Observer<? super T> child) { |
| 34 |
TakeUntilMainObserver<T, U> parent = new TakeUntilMainObserver<>(child); |
| 35 |
child.onSubscribe(parent); |
| 36 |
|
| 37 |
other.subscribe(parent.otherObserver); |
| 38 |
source.subscribe(parent); |
| 39 |
} |
| 40 |
|
| 41 |
static final class TakeUntilMainObserver<T, U> extends AtomicInteger |
| 42 |
implements Observer<T>, Disposable { |
| 43 |
|
| 44 |
private static final long serialVersionUID = 1418547743690811973L; |
| 45 |
|
| 46 |
final Observer<? super T> downstream; |
| 47 |
|
| 48 |
final AtomicReference<Disposable> upstream; |
| 49 |
|
| 50 |
final OtherObserver otherObserver; |
| 51 |
|
| 52 |
final AtomicThrowable error; |
| 53 |
|
| 54 |
TakeUntilMainObserver(Observer<? super T> downstream) { |
| 55 |
this.downstream = downstream; |
| 56 |
this.upstream = new AtomicReference<>(); |
| 57 |
this.otherObserver = new OtherObserver(); |
| 58 |
this.error = new AtomicThrowable(); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void dispose() { |
| 63 |
DisposableHelper.dispose(upstream); |
| 64 |
DisposableHelper.dispose(otherObserver); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public boolean isDisposed() { |
| 69 |
return DisposableHelper.isDisposed(upstream.get()); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void onSubscribe(Disposable d) { |
| 74 |
DisposableHelper.setOnce(upstream, d); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void onNext(T t) { |
| 79 |
HalfSerializer.onNext(downstream, t, this, error); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void onError(Throwable e) { |
| 84 |
DisposableHelper.dispose(otherObserver); |
| 85 |
HalfSerializer.onError(downstream, e, this, error); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void onComplete() { |
| 90 |
DisposableHelper.dispose(otherObserver); |
| 91 |
HalfSerializer.onComplete(downstream, this, error); |
| 92 |
} |
| 93 |
|
| 94 |
void otherError(Throwable e) { |
| 95 |
DisposableHelper.dispose(upstream); |
| 96 |
HalfSerializer.onError(downstream, e, this, error); |
| 97 |
} |
| 98 |
|
| 99 |
void otherComplete() { |
| 100 |
DisposableHelper.dispose(upstream); |
| 101 |
HalfSerializer.onComplete(downstream, this, error); |
| 102 |
} |
| 103 |
|
| 104 |
final class OtherObserver extends AtomicReference<Disposable> |
| 105 |
implements Observer<U> { |
| 106 |
|
| 107 |
private static final long serialVersionUID = -8693423678067375039L; |
| 108 |
|
| 109 |
@Override |
| 110 |
public void onSubscribe(Disposable d) { |
| 111 |
DisposableHelper.setOnce(this, d); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public void onNext(U t) { |
| 116 |
DisposableHelper.dispose(this); |
| 117 |
otherComplete(); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public void onError(Throwable e) { |
| 122 |
otherError(e); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public void onComplete() { |
| 127 |
otherComplete(); |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
} |