| 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.disposables.Disposable; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 23 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 24 |
import io.reactivex.rxjava3.internal.util.*; |
| 25 |
|
| 26 |
/** |
| 27 |
* Merges a Flowable and a Completable by emitting the items of the Flowable and waiting until |
| 28 |
* both the Flowable and Completable complete normally. |
| 29 |
* <p>History: 2.1.10 - experimental |
| 30 |
* @param <T> the element type of the Flowable |
| 31 |
* @since 2.2 |
| 32 |
*/ |
| 33 |
public final class FlowableMergeWithCompletable<T> extends AbstractFlowableWithUpstream<T, T> { |
| 34 |
|
| 35 |
final CompletableSource other; |
| 36 |
|
| 37 |
public FlowableMergeWithCompletable(Flowable<T> source, CompletableSource other) { |
| 38 |
super(source); |
| 39 |
this.other = other; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected void subscribeActual(Subscriber<? super T> subscriber) { |
| 44 |
MergeWithSubscriber<T> parent = new MergeWithSubscriber<>(subscriber); |
| 45 |
subscriber.onSubscribe(parent); |
| 46 |
source.subscribe(parent); |
| 47 |
other.subscribe(parent.otherObserver); |
| 48 |
} |
| 49 |
|
| 50 |
static final class MergeWithSubscriber<T> extends AtomicInteger |
| 51 |
implements FlowableSubscriber<T>, Subscription { |
| 52 |
|
| 53 |
private static final long serialVersionUID = -4592979584110982903L; |
| 54 |
|
| 55 |
final Subscriber<? super T> downstream; |
| 56 |
|
| 57 |
final AtomicReference<Subscription> mainSubscription; |
| 58 |
|
| 59 |
final OtherObserver otherObserver; |
| 60 |
|
| 61 |
final AtomicThrowable errors; |
| 62 |
|
| 63 |
final AtomicLong requested; |
| 64 |
|
| 65 |
volatile boolean mainDone; |
| 66 |
|
| 67 |
volatile boolean otherDone; |
| 68 |
|
| 69 |
MergeWithSubscriber(Subscriber<? super T> downstream) { |
| 70 |
this.downstream = downstream; |
| 71 |
this.mainSubscription = new AtomicReference<>(); |
| 72 |
this.otherObserver = new OtherObserver(this); |
| 73 |
this.errors = new AtomicThrowable(); |
| 74 |
this.requested = new AtomicLong(); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void onSubscribe(Subscription s) { |
| 79 |
SubscriptionHelper.deferredSetOnce(mainSubscription, requested, s); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void onNext(T t) { |
| 84 |
HalfSerializer.onNext(downstream, t, this, errors); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void onError(Throwable ex) { |
| 89 |
DisposableHelper.dispose(otherObserver); |
| 90 |
HalfSerializer.onError(downstream, ex, this, errors); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void onComplete() { |
| 95 |
mainDone = true; |
| 96 |
if (otherDone) { |
| 97 |
HalfSerializer.onComplete(downstream, this, errors); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void request(long n) { |
| 103 |
SubscriptionHelper.deferredRequest(mainSubscription, requested, n); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void cancel() { |
| 108 |
SubscriptionHelper.cancel(mainSubscription); |
| 109 |
DisposableHelper.dispose(otherObserver); |
| 110 |
errors.tryTerminateAndReport(); |
| 111 |
} |
| 112 |
|
| 113 |
void otherError(Throwable ex) { |
| 114 |
SubscriptionHelper.cancel(mainSubscription); |
| 115 |
HalfSerializer.onError(downstream, ex, this, errors); |
| 116 |
} |
| 117 |
|
| 118 |
void otherComplete() { |
| 119 |
otherDone = true; |
| 120 |
if (mainDone) { |
| 121 |
HalfSerializer.onComplete(downstream, this, errors); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
static final class OtherObserver extends AtomicReference<Disposable> |
| 126 |
implements CompletableObserver { |
| 127 |
|
| 128 |
private static final long serialVersionUID = -2935427570954647017L; |
| 129 |
|
| 130 |
final MergeWithSubscriber<?> parent; |
| 131 |
|
| 132 |
OtherObserver(MergeWithSubscriber<?> parent) { |
| 133 |
this.parent = parent; |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public void onSubscribe(Disposable d) { |
| 138 |
DisposableHelper.setOnce(this, d); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void onError(Throwable e) { |
| 143 |
parent.otherError(e); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void onComplete() { |
| 148 |
parent.otherComplete(); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |