| 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.AtomicReference; |
| 17 |
|
| 18 |
import org.reactivestreams.Subscriber; |
| 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.subscribers.SinglePostCompleteSubscriber; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 25 |
|
| 26 |
/** |
| 27 |
* Subscribe to a main Flowable first, then when it completes normally, subscribe to a Single, |
| 28 |
* signal its success value followed by a completion or signal its error as is. |
| 29 |
* <p>History: 2.1.10 - experimental |
| 30 |
* @param <T> the element type of the main source and output type |
| 31 |
* @since 2.2 |
| 32 |
*/ |
| 33 |
public final class FlowableConcatWithSingle<T> extends AbstractFlowableWithUpstream<T, T> { |
| 34 |
|
| 35 |
final SingleSource<? extends T> other; |
| 36 |
|
| 37 |
public FlowableConcatWithSingle(Flowable<T> source, SingleSource<? extends T> other) { |
| 38 |
super(source); |
| 39 |
this.other = other; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 44 |
source.subscribe(new ConcatWithSubscriber<>(s, other)); |
| 45 |
} |
| 46 |
|
| 47 |
static final class ConcatWithSubscriber<T> |
| 48 |
extends SinglePostCompleteSubscriber<T, T> |
| 49 |
implements SingleObserver<T> { |
| 50 |
|
| 51 |
private static final long serialVersionUID = -7346385463600070225L; |
| 52 |
|
| 53 |
final AtomicReference<Disposable> otherDisposable; |
| 54 |
|
| 55 |
SingleSource<? extends T> other; |
| 56 |
|
| 57 |
ConcatWithSubscriber(Subscriber<? super T> actual, SingleSource<? extends T> other) { |
| 58 |
super(actual); |
| 59 |
this.other = other; |
| 60 |
this.otherDisposable = new AtomicReference<>(); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void onSubscribe(Disposable d) { |
| 65 |
DisposableHelper.setOnce(otherDisposable, d); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onNext(T t) { |
| 70 |
produced++; |
| 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 onSuccess(T t) { |
| 81 |
complete(t); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void onComplete() { |
| 86 |
upstream = SubscriptionHelper.CANCELLED; |
| 87 |
SingleSource<? extends T> ss = other; |
| 88 |
other = null; |
| 89 |
ss.subscribe(this); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void cancel() { |
| 94 |
super.cancel(); |
| 95 |
DisposableHelper.dispose(otherDisposable); |
| 96 |
} |
| 97 |
} |
| 98 |
} |