| 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.*; |
| 19 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 20 |
import io.reactivex.rxjava3.functions.*; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
public final class FlowableDoOnLifecycle<T> extends AbstractFlowableWithUpstream<T, T> { |
| 25 |
private final Consumer<? super Subscription> onSubscribe; |
| 26 |
private final LongConsumer onRequest; |
| 27 |
private final Action onCancel; |
| 28 |
|
| 29 |
public FlowableDoOnLifecycle(Flowable<T> source, Consumer<? super Subscription> onSubscribe, |
| 30 |
LongConsumer onRequest, Action onCancel) { |
| 31 |
super(source); |
| 32 |
this.onSubscribe = onSubscribe; |
| 33 |
this.onRequest = onRequest; |
| 34 |
this.onCancel = onCancel; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 39 |
source.subscribe(new SubscriptionLambdaSubscriber<>(s, onSubscribe, onRequest, onCancel)); |
| 40 |
} |
| 41 |
|
| 42 |
static final class SubscriptionLambdaSubscriber<T> implements FlowableSubscriber<T>, Subscription { |
| 43 |
final Subscriber<? super T> downstream; |
| 44 |
final Consumer<? super Subscription> onSubscribe; |
| 45 |
final LongConsumer onRequest; |
| 46 |
final Action onCancel; |
| 47 |
|
| 48 |
Subscription upstream; |
| 49 |
|
| 50 |
SubscriptionLambdaSubscriber(Subscriber<? super T> actual, |
| 51 |
Consumer<? super Subscription> onSubscribe, |
| 52 |
LongConsumer onRequest, |
| 53 |
Action onCancel) { |
| 54 |
this.downstream = actual; |
| 55 |
this.onSubscribe = onSubscribe; |
| 56 |
this.onCancel = onCancel; |
| 57 |
this.onRequest = onRequest; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void onSubscribe(Subscription s) { |
| 62 |
// this way, multiple calls to onSubscribe can show up in tests that use doOnSubscribe to validate behavior |
| 63 |
try { |
| 64 |
onSubscribe.accept(s); |
| 65 |
} catch (Throwable e) { |
| 66 |
Exceptions.throwIfFatal(e); |
| 67 |
s.cancel(); |
| 68 |
this.upstream = SubscriptionHelper.CANCELLED; |
| 69 |
EmptySubscription.error(e, downstream); |
| 70 |
return; |
| 71 |
} |
| 72 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 73 |
this.upstream = s; |
| 74 |
downstream.onSubscribe(this); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void onNext(T t) { |
| 80 |
downstream.onNext(t); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void onError(Throwable t) { |
| 85 |
if (upstream != SubscriptionHelper.CANCELLED) { |
| 86 |
downstream.onError(t); |
| 87 |
} else { |
| 88 |
RxJavaPlugins.onError(t); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onComplete() { |
| 94 |
if (upstream != SubscriptionHelper.CANCELLED) { |
| 95 |
downstream.onComplete(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void request(long n) { |
| 101 |
try { |
| 102 |
onRequest.accept(n); |
| 103 |
} catch (Throwable e) { |
| 104 |
Exceptions.throwIfFatal(e); |
| 105 |
RxJavaPlugins.onError(e); |
| 106 |
} |
| 107 |
upstream.request(n); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void cancel() { |
| 112 |
Subscription s = upstream; |
| 113 |
if (s != SubscriptionHelper.CANCELLED) { |
| 114 |
upstream = SubscriptionHelper.CANCELLED; |
| 115 |
try { |
| 116 |
onCancel.run(); |
| 117 |
} catch (Throwable e) { |
| 118 |
Exceptions.throwIfFatal(e); |
| 119 |
RxJavaPlugins.onError(e); |
| 120 |
} |
| 121 |
s.cancel(); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |