Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDebounce.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.Objects;
17 import java.util.concurrent.atomic.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.Disposable;
23 import io.reactivex.rxjava3.exceptions.*;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
26 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
27 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.subscribers.*;
30
31 public final class FlowableDebounce<T, U> extends AbstractFlowableWithUpstream<T, T> {
32 final Function<? super T, ? extends Publisher<U>> debounceSelector;
33
34 public FlowableDebounce(Flowable<T> source, Function<? super T, ? extends Publisher<U>> debounceSelector) {
35 super(source);
36 this.debounceSelector = debounceSelector;
37 }
38
39 @Override
40 protected void subscribeActual(Subscriber<? super T> s) {
41 source.subscribe(new DebounceSubscriber<>(new SerializedSubscriber<>(s), debounceSelector));
42 }
43
44 static final class DebounceSubscriber<T, U> extends AtomicLong
45 implements FlowableSubscriber<T>, Subscription {
46
47 private static final long serialVersionUID = 6725975399620862591L;
48 final Subscriber<? super T> downstream;
49 final Function<? super T, ? extends Publisher<U>> debounceSelector;
50
51 Subscription upstream;
52
53 final AtomicReference<Disposable> debouncer = new AtomicReference<>();
54
55 volatile long index;
56
57 boolean done;
58
59 DebounceSubscriber(Subscriber<? super T> actual,
60 Function<? super T, ? extends Publisher<U>> debounceSelector) {
61 this.downstream = actual;
62 this.debounceSelector = debounceSelector;
63 }
64
65 @Override
66 public void onSubscribe(Subscription s) {
67 if (SubscriptionHelper.validate(this.upstream, s)) {
68 this.upstream = s;
69 downstream.onSubscribe(this);
70 s.request(Long.MAX_VALUE);
71 }
72 }
73
74 @Override
75 public void onNext(T t) {
76 if (done) {
77 return;
78 }
79
80 long idx = index + 1;
81 index = idx;
82
83 Disposable d = debouncer.get();
84 if (d != null) {
85 d.dispose();
86 }
87
88 Publisher<U> p;
89
90 try {
91 p = Objects.requireNonNull(debounceSelector.apply(t), "The publisher supplied is null");
92 } catch (Throwable e) {
93 Exceptions.throwIfFatal(e);
94 cancel();
95 downstream.onError(e);
96 return;
97 }
98
99 DebounceInnerSubscriber<T, U> dis = new DebounceInnerSubscriber<>(this, idx, t);
100
101 if (debouncer.compareAndSet(d, dis)) {
102 p.subscribe(dis);
103 }
104 }
105
106 @Override
107 public void onError(Throwable t) {
108 DisposableHelper.dispose(debouncer);
109 downstream.onError(t);
110 }
111
112 @Override
113 public void onComplete() {
114 if (done) {
115 return;
116 }
117 done = true;
118 Disposable d = debouncer.get();
119 if (!DisposableHelper.isDisposed(d)) {
120 @SuppressWarnings("unchecked")
121 DebounceInnerSubscriber<T, U> dis = (DebounceInnerSubscriber<T, U>)d;
122 if (dis != null) {
123 dis.emit();
124 }
125 DisposableHelper.dispose(debouncer);
126 downstream.onComplete();
127 }
128 }
129
130 @Override
131 public void request(long n) {
132 if (SubscriptionHelper.validate(n)) {
133 BackpressureHelper.add(this, n);
134 }
135 }
136
137 @Override
138 public void cancel() {
139 upstream.cancel();
140 DisposableHelper.dispose(debouncer);
141 }
142
143 void emit(long idx, T value) {
144 if (idx == index) {
145 long r = get();
146 if (r != 0L) {
147 downstream.onNext(value);
148 BackpressureHelper.produced(this, 1);
149 } else {
150 cancel();
151 downstream.onError(new MissingBackpressureException("Could not deliver value due to lack of requests"));
152 }
153 }
154 }
155
156 static final class DebounceInnerSubscriber<T, U> extends DisposableSubscriber<U> {
157 final DebounceSubscriber<T, U> parent;
158 final long index;
159 final T value;
160
161 boolean done;
162
163 final AtomicBoolean once = new AtomicBoolean();
164
165 DebounceInnerSubscriber(DebounceSubscriber<T, U> parent, long index, T value) {
166 this.parent = parent;
167 this.index = index;
168 this.value = value;
169 }
170
171 @Override
172 public void onNext(U t) {
173 if (done) {
174 return;
175 }
176 done = true;
177 cancel();
178 emit();
179 }
180
181 void emit() {
182 if (once.compareAndSet(false, true)) {
183 parent.emit(index, value);
184 }
185 }
186
187 @Override
188 public void onError(Throwable t) {
189 if (done) {
190 RxJavaPlugins.onError(t);
191 return;
192 }
193 done = true;
194 parent.onError(t);
195 }
196
197 @Override
198 public void onComplete() {
199 if (done) {
200 return;
201 }
202 done = true;
203 emit();
204 }
205 }
206 }
207 }