Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableOnBackpressureError.java

raw
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.MissingBackpressureException;
22 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
23 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25
26 public final class FlowableOnBackpressureError<T> extends AbstractFlowableWithUpstream<T, T> {
27
28 public FlowableOnBackpressureError(Flowable<T> source) {
29 super(source);
30 }
31
32 @Override
33 protected void subscribeActual(Subscriber<? super T> s) {
34 this.source.subscribe(new BackpressureErrorSubscriber<>(s));
35 }
36
37 static final class BackpressureErrorSubscriber<T>
38 extends AtomicLong implements FlowableSubscriber<T>, Subscription {
39 private static final long serialVersionUID = -3176480756392482682L;
40
41 final Subscriber<? super T> downstream;
42 Subscription upstream;
43 boolean done;
44
45 BackpressureErrorSubscriber(Subscriber<? super T> downstream) {
46 this.downstream = downstream;
47 }
48
49 @Override
50 public void onSubscribe(Subscription s) {
51 if (SubscriptionHelper.validate(this.upstream, s)) {
52 this.upstream = s;
53 downstream.onSubscribe(this);
54 s.request(Long.MAX_VALUE);
55 }
56 }
57
58 @Override
59 public void onNext(T t) {
60 if (done) {
61 return;
62 }
63 long r = get();
64 if (r != 0L) {
65 downstream.onNext(t);
66 BackpressureHelper.produced(this, 1);
67 } else {
68 upstream.cancel();
69 onError(new MissingBackpressureException("could not emit value due to lack of requests"));
70 }
71 }
72
73 @Override
74 public void onError(Throwable t) {
75 if (done) {
76 RxJavaPlugins.onError(t);
77 return;
78 }
79 done = true;
80 downstream.onError(t);
81 }
82
83 @Override
84 public void onComplete() {
85 if (done) {
86 return;
87 }
88 done = true;
89 downstream.onComplete();
90 }
91
92 @Override
93 public void request(long n) {
94 if (SubscriptionHelper.validate(n)) {
95 BackpressureHelper.add(this, n);
96 }
97 }
98
99 @Override
100 public void cancel() {
101 upstream.cancel();
102 }
103 }
104 }