| 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.flowable; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
/** |
| 25 |
* Delays the subscription to the main source until the other |
| 26 |
* observable fires an event or completes. |
| 27 |
* @param <T> the main type |
| 28 |
* @param <U> the other value type, ignored |
| 29 |
*/ |
| 30 |
public final class FlowableDelaySubscriptionOther<T, U> extends Flowable<T> { |
| 31 |
final Publisher<? extends T> main; |
| 32 |
final Publisher<U> other; |
| 33 |
|
| 34 |
public FlowableDelaySubscriptionOther(Publisher<? extends T> main, Publisher<U> other) { |
| 35 |
this.main = main; |
| 36 |
this.other = other; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public void subscribeActual(final Subscriber<? super T> child) { |
| 41 |
MainSubscriber<T> parent = new MainSubscriber<>(child, main); |
| 42 |
child.onSubscribe(parent); |
| 43 |
other.subscribe(parent.other); |
| 44 |
} |
| 45 |
|
| 46 |
static final class MainSubscriber<T> extends AtomicLong implements FlowableSubscriber<T>, Subscription { |
| 47 |
|
| 48 |
private static final long serialVersionUID = 2259811067697317255L; |
| 49 |
|
| 50 |
final Subscriber<? super T> downstream; |
| 51 |
|
| 52 |
final Publisher<? extends T> main; |
| 53 |
|
| 54 |
final OtherSubscriber other; |
| 55 |
|
| 56 |
final AtomicReference<Subscription> upstream; |
| 57 |
|
| 58 |
MainSubscriber(Subscriber<? super T> downstream, Publisher<? extends T> main) { |
| 59 |
this.downstream = downstream; |
| 60 |
this.main = main; |
| 61 |
this.other = new OtherSubscriber(); |
| 62 |
this.upstream = new AtomicReference<>(); |
| 63 |
} |
| 64 |
|
| 65 |
void next() { |
| 66 |
main.subscribe(this); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void onNext(T t) { |
| 71 |
downstream.onNext(t); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void onError(Throwable t) { |
| 76 |
downstream.onError(t); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void onComplete() { |
| 81 |
downstream.onComplete(); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void request(long n) { |
| 86 |
if (SubscriptionHelper.validate(n)) { |
| 87 |
SubscriptionHelper.deferredRequest(upstream, this, n); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public void cancel() { |
| 93 |
SubscriptionHelper.cancel(other); |
| 94 |
SubscriptionHelper.cancel(upstream); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onSubscribe(Subscription s) { |
| 99 |
SubscriptionHelper.deferredSetOnce(upstream, this, s); |
| 100 |
} |
| 101 |
|
| 102 |
final class OtherSubscriber extends AtomicReference<Subscription> implements FlowableSubscriber<Object> { |
| 103 |
|
| 104 |
private static final long serialVersionUID = -3892798459447644106L; |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onSubscribe(Subscription s) { |
| 108 |
if (SubscriptionHelper.setOnce(this, s)) { |
| 109 |
s.request(Long.MAX_VALUE); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void onNext(Object t) { |
| 115 |
Subscription s = get(); |
| 116 |
if (s != SubscriptionHelper.CANCELLED) { |
| 117 |
lazySet(SubscriptionHelper.CANCELLED); |
| 118 |
s.cancel(); |
| 119 |
next(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public void onError(Throwable t) { |
| 125 |
Subscription s = get(); |
| 126 |
if (s != SubscriptionHelper.CANCELLED) { |
| 127 |
downstream.onError(t); |
| 128 |
} else { |
| 129 |
RxJavaPlugins.onError(t); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void onComplete() { |
| 135 |
Subscription s = get(); |
| 136 |
if (s != SubscriptionHelper.CANCELLED) { |
| 137 |
next(); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |