| 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.mixed; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 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.subscriptions.SubscriptionHelper; |
| 24 |
|
| 25 |
/** |
| 26 |
* After Completable completes, it relays the signals |
| 27 |
* of the Publisher to the downstream subscriber. |
| 28 |
* |
| 29 |
* @param <R> the result type of the Publisher and this operator |
| 30 |
* @since 2.1.15 |
| 31 |
*/ |
| 32 |
public final class CompletableAndThenPublisher<R> extends Flowable<R> { |
| 33 |
|
| 34 |
final CompletableSource source; |
| 35 |
|
| 36 |
final Publisher<? extends R> other; |
| 37 |
|
| 38 |
public CompletableAndThenPublisher(CompletableSource source, |
| 39 |
Publisher<? extends R> other) { |
| 40 |
this.source = source; |
| 41 |
this.other = other; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
protected void subscribeActual(Subscriber<? super R> s) { |
| 46 |
source.subscribe(new AndThenPublisherSubscriber<R>(s, other)); |
| 47 |
} |
| 48 |
|
| 49 |
static final class AndThenPublisherSubscriber<R> |
| 50 |
extends AtomicReference<Subscription> |
| 51 |
implements FlowableSubscriber<R>, CompletableObserver, Subscription { |
| 52 |
|
| 53 |
private static final long serialVersionUID = -8948264376121066672L; |
| 54 |
|
| 55 |
final Subscriber<? super R> downstream; |
| 56 |
|
| 57 |
Publisher<? extends R> other; |
| 58 |
|
| 59 |
Disposable upstream; |
| 60 |
|
| 61 |
final AtomicLong requested; |
| 62 |
|
| 63 |
AndThenPublisherSubscriber(Subscriber<? super R> downstream, Publisher<? extends R> other) { |
| 64 |
this.downstream = downstream; |
| 65 |
this.other = other; |
| 66 |
this.requested = new AtomicLong(); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void onNext(R 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 |
Publisher<? extends R> p = other; |
| 82 |
if (p == null) { |
| 83 |
downstream.onComplete(); |
| 84 |
} else { |
| 85 |
other = null; |
| 86 |
p.subscribe(this); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public void request(long n) { |
| 92 |
SubscriptionHelper.deferredRequest(this, requested, n); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void cancel() { |
| 97 |
upstream.dispose(); |
| 98 |
SubscriptionHelper.cancel(this); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void onSubscribe(Disposable d) { |
| 103 |
if (DisposableHelper.validate(upstream, d)) { |
| 104 |
this.upstream = d; |
| 105 |
downstream.onSubscribe(this); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void onSubscribe(Subscription s) { |
| 111 |
SubscriptionHelper.deferredSetOnce(this, requested, s); |
| 112 |
} |
| 113 |
} |
| 114 |
} |