| 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.Objects; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 23 |
import io.reactivex.rxjava3.functions.BiFunction; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 25 |
import io.reactivex.rxjava3.operators.ConditionalSubscriber; |
| 26 |
import io.reactivex.rxjava3.subscribers.SerializedSubscriber; |
| 27 |
|
| 28 |
public final class FlowableWithLatestFrom<T, U, R> extends AbstractFlowableWithUpstream<T, R> { |
| 29 |
final BiFunction<? super T, ? super U, ? extends R> combiner; |
| 30 |
final Publisher<? extends U> other; |
| 31 |
public FlowableWithLatestFrom(Flowable<T> source, BiFunction<? super T, ? super U, ? extends R> combiner, Publisher<? extends U> other) { |
| 32 |
super(source); |
| 33 |
this.combiner = combiner; |
| 34 |
this.other = other; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
protected void subscribeActual(Subscriber<? super R> s) { |
| 39 |
final SerializedSubscriber<R> serial = new SerializedSubscriber<>(s); |
| 40 |
final WithLatestFromSubscriber<T, U, R> wlf = new WithLatestFromSubscriber<>(serial, combiner); |
| 41 |
|
| 42 |
serial.onSubscribe(wlf); |
| 43 |
|
| 44 |
other.subscribe(new FlowableWithLatestSubscriber(wlf)); |
| 45 |
|
| 46 |
source.subscribe(wlf); |
| 47 |
} |
| 48 |
|
| 49 |
static final class WithLatestFromSubscriber<T, U, R> extends AtomicReference<U> |
| 50 |
implements ConditionalSubscriber<T>, Subscription { |
| 51 |
|
| 52 |
private static final long serialVersionUID = -312246233408980075L; |
| 53 |
|
| 54 |
final Subscriber<? super R> downstream; |
| 55 |
|
| 56 |
final BiFunction<? super T, ? super U, ? extends R> combiner; |
| 57 |
|
| 58 |
final AtomicReference<Subscription> upstream = new AtomicReference<>(); |
| 59 |
|
| 60 |
final AtomicLong requested = new AtomicLong(); |
| 61 |
|
| 62 |
final AtomicReference<Subscription> other = new AtomicReference<>(); |
| 63 |
|
| 64 |
WithLatestFromSubscriber(Subscriber<? super R> actual, BiFunction<? super T, ? super U, ? extends R> combiner) { |
| 65 |
this.downstream = actual; |
| 66 |
this.combiner = combiner; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void onSubscribe(Subscription s) { |
| 71 |
SubscriptionHelper.deferredSetOnce(this.upstream, requested, s); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void onNext(T t) { |
| 76 |
if (!tryOnNext(t)) { |
| 77 |
upstream.get().request(1); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public boolean tryOnNext(T t) { |
| 83 |
U u = get(); |
| 84 |
if (u != null) { |
| 85 |
R r; |
| 86 |
try { |
| 87 |
r = Objects.requireNonNull(combiner.apply(t, u), "The combiner returned a null value"); |
| 88 |
} catch (Throwable e) { |
| 89 |
Exceptions.throwIfFatal(e); |
| 90 |
cancel(); |
| 91 |
downstream.onError(e); |
| 92 |
return false; |
| 93 |
} |
| 94 |
downstream.onNext(r); |
| 95 |
return true; |
| 96 |
} else { |
| 97 |
return false; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void onError(Throwable t) { |
| 103 |
SubscriptionHelper.cancel(other); |
| 104 |
downstream.onError(t); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void onComplete() { |
| 109 |
SubscriptionHelper.cancel(other); |
| 110 |
downstream.onComplete(); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void request(long n) { |
| 115 |
SubscriptionHelper.deferredRequest(upstream, requested, n); |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void cancel() { |
| 120 |
SubscriptionHelper.cancel(upstream); |
| 121 |
SubscriptionHelper.cancel(other); |
| 122 |
} |
| 123 |
|
| 124 |
public boolean setOther(Subscription o) { |
| 125 |
return SubscriptionHelper.setOnce(other, o); |
| 126 |
} |
| 127 |
|
| 128 |
public void otherError(Throwable e) { |
| 129 |
SubscriptionHelper.cancel(upstream); |
| 130 |
downstream.onError(e); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
final class FlowableWithLatestSubscriber implements FlowableSubscriber<U> { |
| 135 |
private final WithLatestFromSubscriber<T, U, R> wlf; |
| 136 |
|
| 137 |
FlowableWithLatestSubscriber(WithLatestFromSubscriber<T, U, R> wlf) { |
| 138 |
this.wlf = wlf; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void onSubscribe(Subscription s) { |
| 143 |
if (wlf.setOther(s)) { |
| 144 |
s.request(Long.MAX_VALUE); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public void onNext(U t) { |
| 150 |
wlf.lazySet(t); |
| 151 |
} |
| 152 |
|
| 153 |
@Override |
| 154 |
public void onError(Throwable t) { |
| 155 |
wlf.otherError(t); |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public void onComplete() { |
| 160 |
// nothing to do, the wlf will complete on its own pace |
| 161 |
} |
| 162 |
} |
| 163 |
} |