| 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 org.reactivestreams.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.Flowable; |
| 19 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 20 |
import io.reactivex.rxjava3.functions.Function; |
| 21 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableRepeatWhen.*; |
| 22 |
import io.reactivex.rxjava3.internal.subscriptions.EmptySubscription; |
| 23 |
import io.reactivex.rxjava3.processors.*; |
| 24 |
import io.reactivex.rxjava3.subscribers.SerializedSubscriber; |
| 25 |
|
| 26 |
import java.util.Objects; |
| 27 |
|
| 28 |
public final class FlowableRetryWhen<T> extends AbstractFlowableWithUpstream<T, T> { |
| 29 |
final Function<? super Flowable<Throwable>, ? extends Publisher<?>> handler; |
| 30 |
|
| 31 |
public FlowableRetryWhen(Flowable<T> source, |
| 32 |
Function<? super Flowable<Throwable>, ? extends Publisher<?>> handler) { |
| 33 |
super(source); |
| 34 |
this.handler = handler; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public void subscribeActual(Subscriber<? super T> s) { |
| 39 |
SerializedSubscriber<T> z = new SerializedSubscriber<>(s); |
| 40 |
|
| 41 |
FlowableProcessor<Throwable> processor = UnicastProcessor.<Throwable>create(8).toSerialized(); |
| 42 |
|
| 43 |
Publisher<?> when; |
| 44 |
|
| 45 |
try { |
| 46 |
when = Objects.requireNonNull(handler.apply(processor), "handler returned a null Publisher"); |
| 47 |
} catch (Throwable ex) { |
| 48 |
Exceptions.throwIfFatal(ex); |
| 49 |
EmptySubscription.error(ex, s); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
WhenReceiver<T, Throwable> receiver = new WhenReceiver<>(source); |
| 54 |
|
| 55 |
RetryWhenSubscriber<T> subscriber = new RetryWhenSubscriber<>(z, processor, receiver); |
| 56 |
|
| 57 |
receiver.subscriber = subscriber; |
| 58 |
|
| 59 |
s.onSubscribe(subscriber); |
| 60 |
|
| 61 |
when.subscribe(receiver); |
| 62 |
|
| 63 |
receiver.onNext(0); |
| 64 |
} |
| 65 |
|
| 66 |
static final class RetryWhenSubscriber<T> extends WhenSourceSubscriber<T, Throwable> { |
| 67 |
|
| 68 |
private static final long serialVersionUID = -2680129890138081029L; |
| 69 |
|
| 70 |
RetryWhenSubscriber(Subscriber<? super T> actual, FlowableProcessor<Throwable> processor, |
| 71 |
Subscription receiver) { |
| 72 |
super(actual, processor, receiver); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void onError(Throwable t) { |
| 77 |
again(t); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void onComplete() { |
| 82 |
receiver.cancel(); |
| 83 |
downstream.onComplete(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
} |