| 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.observable; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 21 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 22 |
import io.reactivex.rxjava3.functions.BiFunction; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 |
import io.reactivex.rxjava3.observers.SerializedObserver; |
| 25 |
|
| 26 |
public final class ObservableWithLatestFrom<T, U, R> extends AbstractObservableWithUpstream<T, R> { |
| 27 |
final BiFunction<? super T, ? super U, ? extends R> combiner; |
| 28 |
final ObservableSource<? extends U> other; |
| 29 |
public ObservableWithLatestFrom(ObservableSource<T> source, |
| 30 |
BiFunction<? super T, ? super U, ? extends R> combiner, ObservableSource<? extends U> other) { |
| 31 |
super(source); |
| 32 |
this.combiner = combiner; |
| 33 |
this.other = other; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public void subscribeActual(Observer<? super R> t) { |
| 38 |
final SerializedObserver<R> serial = new SerializedObserver<>(t); |
| 39 |
final WithLatestFromObserver<T, U, R> wlf = new WithLatestFromObserver<>(serial, combiner); |
| 40 |
|
| 41 |
serial.onSubscribe(wlf); |
| 42 |
|
| 43 |
other.subscribe(new WithLatestFromOtherObserver(wlf)); |
| 44 |
|
| 45 |
source.subscribe(wlf); |
| 46 |
} |
| 47 |
|
| 48 |
static final class WithLatestFromObserver<T, U, R> extends AtomicReference<U> implements Observer<T>, Disposable { |
| 49 |
|
| 50 |
private static final long serialVersionUID = -312246233408980075L; |
| 51 |
|
| 52 |
final Observer<? super R> downstream; |
| 53 |
|
| 54 |
final BiFunction<? super T, ? super U, ? extends R> combiner; |
| 55 |
|
| 56 |
final AtomicReference<Disposable> upstream = new AtomicReference<>(); |
| 57 |
|
| 58 |
final AtomicReference<Disposable> other = new AtomicReference<>(); |
| 59 |
|
| 60 |
WithLatestFromObserver(Observer<? super R> actual, BiFunction<? super T, ? super U, ? extends R> combiner) { |
| 61 |
this.downstream = actual; |
| 62 |
this.combiner = combiner; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void onSubscribe(Disposable d) { |
| 67 |
DisposableHelper.setOnce(this.upstream, d); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void onNext(T t) { |
| 72 |
U u = get(); |
| 73 |
if (u != null) { |
| 74 |
R r; |
| 75 |
try { |
| 76 |
r = Objects.requireNonNull(combiner.apply(t, u), "The combiner returned a null value"); |
| 77 |
} catch (Throwable e) { |
| 78 |
Exceptions.throwIfFatal(e); |
| 79 |
dispose(); |
| 80 |
downstream.onError(e); |
| 81 |
return; |
| 82 |
} |
| 83 |
downstream.onNext(r); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void onError(Throwable t) { |
| 89 |
DisposableHelper.dispose(other); |
| 90 |
downstream.onError(t); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void onComplete() { |
| 95 |
DisposableHelper.dispose(other); |
| 96 |
downstream.onComplete(); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void dispose() { |
| 101 |
DisposableHelper.dispose(upstream); |
| 102 |
DisposableHelper.dispose(other); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public boolean isDisposed() { |
| 107 |
return DisposableHelper.isDisposed(upstream.get()); |
| 108 |
} |
| 109 |
|
| 110 |
public boolean setOther(Disposable o) { |
| 111 |
return DisposableHelper.setOnce(other, o); |
| 112 |
} |
| 113 |
|
| 114 |
public void otherError(Throwable e) { |
| 115 |
DisposableHelper.dispose(upstream); |
| 116 |
downstream.onError(e); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
final class WithLatestFromOtherObserver implements Observer<U> { |
| 121 |
private final WithLatestFromObserver<T, U, R> parent; |
| 122 |
|
| 123 |
WithLatestFromOtherObserver(WithLatestFromObserver<T, U, R> parent) { |
| 124 |
this.parent = parent; |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public void onSubscribe(Disposable d) { |
| 129 |
parent.setOther(d); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public void onNext(U t) { |
| 134 |
parent.lazySet(t); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public void onError(Throwable t) { |
| 139 |
parent.otherError(t); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public void onComplete() { |
| 144 |
// nothing to do, the wlf will complete on its own pace |
| 145 |
} |
| 146 |
} |
| 147 |
} |