| 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.single; |
| 15 |
|
| 16 |
import java.util.concurrent.CancellationException; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
|
| 27 |
/** |
| 28 |
* Signals the events of the source Single or signals a CancellationException if the |
| 29 |
* other Publisher signalled first. |
| 30 |
* @param <T> the main value type |
| 31 |
* @param <U> the other value type |
| 32 |
*/ |
| 33 |
public final class SingleTakeUntil<T, U> extends Single<T> { |
| 34 |
|
| 35 |
final SingleSource<T> source; |
| 36 |
|
| 37 |
final Publisher<U> other; |
| 38 |
|
| 39 |
public SingleTakeUntil(SingleSource<T> source, Publisher<U> other) { |
| 40 |
this.source = source; |
| 41 |
this.other = other; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
protected void subscribeActual(SingleObserver<? super T> observer) { |
| 46 |
TakeUntilMainObserver<T> parent = new TakeUntilMainObserver<>(observer); |
| 47 |
observer.onSubscribe(parent); |
| 48 |
|
| 49 |
other.subscribe(parent.other); |
| 50 |
|
| 51 |
source.subscribe(parent); |
| 52 |
} |
| 53 |
|
| 54 |
static final class TakeUntilMainObserver<T> |
| 55 |
extends AtomicReference<Disposable> |
| 56 |
implements SingleObserver<T>, Disposable { |
| 57 |
|
| 58 |
private static final long serialVersionUID = -622603812305745221L; |
| 59 |
|
| 60 |
final SingleObserver<? super T> downstream; |
| 61 |
|
| 62 |
final TakeUntilOtherSubscriber other; |
| 63 |
|
| 64 |
TakeUntilMainObserver(SingleObserver<? super T> downstream) { |
| 65 |
this.downstream = downstream; |
| 66 |
this.other = new TakeUntilOtherSubscriber(this); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void dispose() { |
| 71 |
DisposableHelper.dispose(this); |
| 72 |
other.dispose(); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public boolean isDisposed() { |
| 77 |
return DisposableHelper.isDisposed(get()); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void onSubscribe(Disposable d) { |
| 82 |
DisposableHelper.setOnce(this, d); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void onSuccess(T value) { |
| 87 |
other.dispose(); |
| 88 |
|
| 89 |
Disposable a = getAndSet(DisposableHelper.DISPOSED); |
| 90 |
if (a != DisposableHelper.DISPOSED) { |
| 91 |
downstream.onSuccess(value); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void onError(Throwable e) { |
| 97 |
other.dispose(); |
| 98 |
|
| 99 |
Disposable a = get(); |
| 100 |
if (a != DisposableHelper.DISPOSED) { |
| 101 |
a = getAndSet(DisposableHelper.DISPOSED); |
| 102 |
if (a != DisposableHelper.DISPOSED) { |
| 103 |
downstream.onError(e); |
| 104 |
return; |
| 105 |
} |
| 106 |
} |
| 107 |
RxJavaPlugins.onError(e); |
| 108 |
} |
| 109 |
|
| 110 |
void otherError(Throwable e) { |
| 111 |
Disposable a = get(); |
| 112 |
if (a != DisposableHelper.DISPOSED) { |
| 113 |
a = getAndSet(DisposableHelper.DISPOSED); |
| 114 |
if (a != DisposableHelper.DISPOSED) { |
| 115 |
if (a != null) { |
| 116 |
a.dispose(); |
| 117 |
} |
| 118 |
downstream.onError(e); |
| 119 |
return; |
| 120 |
} |
| 121 |
} |
| 122 |
RxJavaPlugins.onError(e); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
static final class TakeUntilOtherSubscriber |
| 127 |
extends AtomicReference<Subscription> |
| 128 |
implements FlowableSubscriber<Object> { |
| 129 |
|
| 130 |
private static final long serialVersionUID = 5170026210238877381L; |
| 131 |
|
| 132 |
final TakeUntilMainObserver<?> parent; |
| 133 |
|
| 134 |
TakeUntilOtherSubscriber(TakeUntilMainObserver<?> parent) { |
| 135 |
this.parent = parent; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void onSubscribe(Subscription s) { |
| 140 |
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE); |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public void onNext(Object t) { |
| 145 |
if (SubscriptionHelper.cancel(this)) { |
| 146 |
parent.otherError(new CancellationException()); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public void onError(Throwable t) { |
| 152 |
parent.otherError(t); |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void onComplete() { |
| 157 |
if (get() != SubscriptionHelper.CANCELLED) { |
| 158 |
lazySet(SubscriptionHelper.CANCELLED); |
| 159 |
parent.otherError(new CancellationException()); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public void dispose() { |
| 164 |
SubscriptionHelper.cancel(this); |
| 165 |
} |
| 166 |
} |
| 167 |
} |