| 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.concurrent.atomic.AtomicLong; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 22 |
import io.reactivex.rxjava3.functions.Consumer; |
| 23 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 24 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
|
| 27 |
public final class FlowableOnBackpressureDrop<T> extends AbstractFlowableWithUpstream<T, T> implements Consumer<T> { |
| 28 |
|
| 29 |
final Consumer<? super T> onDrop; |
| 30 |
|
| 31 |
public FlowableOnBackpressureDrop(Flowable<T> source) { |
| 32 |
super(source); |
| 33 |
this.onDrop = this; |
| 34 |
} |
| 35 |
|
| 36 |
public FlowableOnBackpressureDrop(Flowable<T> source, Consumer<? super T> onDrop) { |
| 37 |
super(source); |
| 38 |
this.onDrop = onDrop; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
public void accept(T t) { |
| 43 |
// deliberately ignoring |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 48 |
this.source.subscribe(new BackpressureDropSubscriber<>(s, onDrop)); |
| 49 |
} |
| 50 |
|
| 51 |
static final class BackpressureDropSubscriber<T> |
| 52 |
extends AtomicLong implements FlowableSubscriber<T>, Subscription { |
| 53 |
|
| 54 |
private static final long serialVersionUID = -6246093802440953054L; |
| 55 |
|
| 56 |
final Subscriber<? super T> downstream; |
| 57 |
final Consumer<? super T> onDrop; |
| 58 |
|
| 59 |
Subscription upstream; |
| 60 |
|
| 61 |
boolean done; |
| 62 |
|
| 63 |
BackpressureDropSubscriber(Subscriber<? super T> actual, Consumer<? super T> onDrop) { |
| 64 |
this.downstream = actual; |
| 65 |
this.onDrop = onDrop; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onSubscribe(Subscription s) { |
| 70 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 71 |
this.upstream = s; |
| 72 |
downstream.onSubscribe(this); |
| 73 |
s.request(Long.MAX_VALUE); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void onNext(T t) { |
| 79 |
if (done) { |
| 80 |
return; |
| 81 |
} |
| 82 |
long r = get(); |
| 83 |
if (r != 0L) { |
| 84 |
downstream.onNext(t); |
| 85 |
BackpressureHelper.produced(this, 1); |
| 86 |
} else { |
| 87 |
try { |
| 88 |
onDrop.accept(t); |
| 89 |
} catch (Throwable e) { |
| 90 |
Exceptions.throwIfFatal(e); |
| 91 |
cancel(); |
| 92 |
onError(e); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onError(Throwable t) { |
| 99 |
if (done) { |
| 100 |
RxJavaPlugins.onError(t); |
| 101 |
return; |
| 102 |
} |
| 103 |
done = true; |
| 104 |
downstream.onError(t); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void onComplete() { |
| 109 |
if (done) { |
| 110 |
return; |
| 111 |
} |
| 112 |
done = true; |
| 113 |
downstream.onComplete(); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void request(long n) { |
| 118 |
if (SubscriptionHelper.validate(n)) { |
| 119 |
BackpressureHelper.add(this, n); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public void cancel() { |
| 125 |
upstream.cancel(); |
| 126 |
} |
| 127 |
} |
| 128 |
} |