| 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.Function; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 24 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 25 |
|
| 26 |
/** |
| 27 |
* Shares a source Observable for the duration of a selector function. |
| 28 |
* @param <T> the input value type |
| 29 |
* @param <R> the output value type |
| 30 |
*/ |
| 31 |
public final class ObservablePublishSelector<T, R> extends AbstractObservableWithUpstream<T, R> { |
| 32 |
|
| 33 |
final Function<? super Observable<T>, ? extends ObservableSource<R>> selector; |
| 34 |
|
| 35 |
public ObservablePublishSelector(final ObservableSource<T> source, |
| 36 |
final Function<? super Observable<T>, ? extends ObservableSource<R>> selector) { |
| 37 |
super(source); |
| 38 |
this.selector = selector; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected void subscribeActual(Observer<? super R> observer) { |
| 43 |
PublishSubject<T> subject = PublishSubject.create(); |
| 44 |
|
| 45 |
ObservableSource<? extends R> target; |
| 46 |
|
| 47 |
try { |
| 48 |
target = Objects.requireNonNull(selector.apply(subject), "The selector returned a null ObservableSource"); |
| 49 |
} catch (Throwable ex) { |
| 50 |
Exceptions.throwIfFatal(ex); |
| 51 |
EmptyDisposable.error(ex, observer); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
TargetObserver<R> o = new TargetObserver<>(observer); |
| 56 |
|
| 57 |
target.subscribe(o); |
| 58 |
|
| 59 |
source.subscribe(new SourceObserver<>(subject, o)); |
| 60 |
} |
| 61 |
|
| 62 |
static final class SourceObserver<T> implements Observer<T> { |
| 63 |
|
| 64 |
final PublishSubject<T> subject; |
| 65 |
|
| 66 |
final AtomicReference<Disposable> target; |
| 67 |
|
| 68 |
SourceObserver(PublishSubject<T> subject, AtomicReference<Disposable> target) { |
| 69 |
this.subject = subject; |
| 70 |
this.target = target; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void onSubscribe(Disposable d) { |
| 75 |
DisposableHelper.setOnce(target, d); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void onNext(T value) { |
| 80 |
subject.onNext(value); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void onError(Throwable e) { |
| 85 |
subject.onError(e); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void onComplete() { |
| 90 |
subject.onComplete(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static final class TargetObserver<R> |
| 95 |
extends AtomicReference<Disposable> implements Observer<R>, Disposable { |
| 96 |
private static final long serialVersionUID = 854110278590336484L; |
| 97 |
|
| 98 |
final Observer<? super R> downstream; |
| 99 |
|
| 100 |
Disposable upstream; |
| 101 |
|
| 102 |
TargetObserver(Observer<? super R> downstream) { |
| 103 |
this.downstream = downstream; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onSubscribe(Disposable d) { |
| 108 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 109 |
this.upstream = d; |
| 110 |
|
| 111 |
downstream.onSubscribe(this); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public void onNext(R value) { |
| 117 |
downstream.onNext(value); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public void onError(Throwable e) { |
| 122 |
DisposableHelper.dispose(this); |
| 123 |
downstream.onError(e); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public void onComplete() { |
| 128 |
DisposableHelper.dispose(this); |
| 129 |
downstream.onComplete(); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public void dispose() { |
| 134 |
upstream.dispose(); |
| 135 |
DisposableHelper.dispose(this); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public boolean isDisposed() { |
| 140 |
return upstream.isDisposed(); |
| 141 |
} |
| 142 |
} |
| 143 |
} |