| 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.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.internal.disposables.DisposableHelper; |
| 23 |
import io.reactivex.rxjava3.internal.observers.ResumeSingleObserver; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
|
| 27 |
public final class SingleDelayWithPublisher<T, U> extends Single<T> { |
| 28 |
|
| 29 |
final SingleSource<T> source; |
| 30 |
|
| 31 |
final Publisher<U> other; |
| 32 |
|
| 33 |
public SingleDelayWithPublisher(SingleSource<T> source, Publisher<U> other) { |
| 34 |
this.source = source; |
| 35 |
this.other = other; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
protected void subscribeActual(SingleObserver<? super T> observer) { |
| 40 |
other.subscribe(new OtherSubscriber<>(observer, source)); |
| 41 |
} |
| 42 |
|
| 43 |
static final class OtherSubscriber<T, U> |
| 44 |
extends AtomicReference<Disposable> |
| 45 |
implements FlowableSubscriber<U>, Disposable { |
| 46 |
|
| 47 |
private static final long serialVersionUID = -8565274649390031272L; |
| 48 |
|
| 49 |
final SingleObserver<? super T> downstream; |
| 50 |
|
| 51 |
final SingleSource<T> source; |
| 52 |
|
| 53 |
boolean done; |
| 54 |
|
| 55 |
Subscription upstream; |
| 56 |
|
| 57 |
OtherSubscriber(SingleObserver<? super T> actual, SingleSource<T> source) { |
| 58 |
this.downstream = actual; |
| 59 |
this.source = source; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void onSubscribe(Subscription s) { |
| 64 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 65 |
this.upstream = s; |
| 66 |
|
| 67 |
downstream.onSubscribe(this); |
| 68 |
|
| 69 |
s.request(Long.MAX_VALUE); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void onNext(U value) { |
| 75 |
upstream.cancel(); |
| 76 |
onComplete(); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void onError(Throwable e) { |
| 81 |
if (done) { |
| 82 |
RxJavaPlugins.onError(e); |
| 83 |
return; |
| 84 |
} |
| 85 |
done = true; |
| 86 |
downstream.onError(e); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void onComplete() { |
| 91 |
if (done) { |
| 92 |
return; |
| 93 |
} |
| 94 |
done = true; |
| 95 |
source.subscribe(new ResumeSingleObserver<>(this, downstream)); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void dispose() { |
| 100 |
upstream.cancel(); |
| 101 |
DisposableHelper.dispose(this); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public boolean isDisposed() { |
| 106 |
return DisposableHelper.isDisposed(get()); |
| 107 |
} |
| 108 |
} |
| 109 |
} |