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