Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableDebounce.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.observable;
15
16 import java.util.Objects;
17 import java.util.concurrent.atomic.*;
18
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.exceptions.Exceptions;
22 import io.reactivex.rxjava3.functions.Function;
23 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
24 import io.reactivex.rxjava3.observers.*;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26
27 public final class ObservableDebounce<T, U> extends AbstractObservableWithUpstream<T, T> {
28 final Function<? super T, ? extends ObservableSource<U>> debounceSelector;
29
30 public ObservableDebounce(ObservableSource<T> source, Function<? super T, ? extends ObservableSource<U>> debounceSelector) {
31 super(source);
32 this.debounceSelector = debounceSelector;
33 }
34
35 @Override
36 public void subscribeActual(Observer<? super T> t) {
37 source.subscribe(new DebounceObserver<>(new SerializedObserver<>(t), debounceSelector));
38 }
39
40 static final class DebounceObserver<T, U>
41 implements Observer<T>, Disposable {
42 final Observer<? super T> downstream;
43 final Function<? super T, ? extends ObservableSource<U>> debounceSelector;
44
45 Disposable upstream;
46
47 final AtomicReference<Disposable> debouncer = new AtomicReference<>();
48
49 volatile long index;
50
51 boolean done;
52
53 DebounceObserver(Observer<? super T> actual,
54 Function<? super T, ? extends ObservableSource<U>> debounceSelector) {
55 this.downstream = actual;
56 this.debounceSelector = debounceSelector;
57 }
58
59 @Override
60 public void onSubscribe(Disposable d) {
61 if (DisposableHelper.validate(this.upstream, d)) {
62 this.upstream = d;
63 downstream.onSubscribe(this);
64 }
65 }
66
67 @Override
68 public void onNext(T t) {
69 if (done) {
70 return;
71 }
72
73 long idx = index + 1;
74 index = idx;
75
76 Disposable d = debouncer.get();
77 if (d != null) {
78 d.dispose();
79 }
80
81 ObservableSource<U> p;
82
83 try {
84 p = Objects.requireNonNull(debounceSelector.apply(t), "The ObservableSource supplied is null");
85 } catch (Throwable e) {
86 Exceptions.throwIfFatal(e);
87 dispose();
88 downstream.onError(e);
89 return;
90 }
91
92 DebounceInnerObserver<T, U> dis = new DebounceInnerObserver<>(this, idx, t);
93
94 if (debouncer.compareAndSet(d, dis)) {
95 p.subscribe(dis);
96 }
97 }
98
99 @Override
100 public void onError(Throwable t) {
101 DisposableHelper.dispose(debouncer);
102 downstream.onError(t);
103 }
104
105 @Override
106 public void onComplete() {
107 if (done) {
108 return;
109 }
110 done = true;
111 Disposable d = debouncer.get();
112 if (d != DisposableHelper.DISPOSED) {
113 @SuppressWarnings("unchecked")
114 DebounceInnerObserver<T, U> dis = (DebounceInnerObserver<T, U>)d;
115 if (dis != null) {
116 dis.emit();
117 }
118 DisposableHelper.dispose(debouncer);
119 downstream.onComplete();
120 }
121 }
122
123 @Override
124 public void dispose() {
125 upstream.dispose();
126 DisposableHelper.dispose(debouncer);
127 }
128
129 @Override
130 public boolean isDisposed() {
131 return upstream.isDisposed();
132 }
133
134 void emit(long idx, T value) {
135 if (idx == index) {
136 downstream.onNext(value);
137 }
138 }
139
140 static final class DebounceInnerObserver<T, U> extends DisposableObserver<U> {
141 final DebounceObserver<T, U> parent;
142 final long index;
143 final T value;
144
145 boolean done;
146
147 final AtomicBoolean once = new AtomicBoolean();
148
149 DebounceInnerObserver(DebounceObserver<T, U> parent, long index, T value) {
150 this.parent = parent;
151 this.index = index;
152 this.value = value;
153 }
154
155 @Override
156 public void onNext(U t) {
157 if (done) {
158 return;
159 }
160 done = true;
161 dispose();
162 emit();
163 }
164
165 void emit() {
166 if (once.compareAndSet(false, true)) {
167 parent.emit(index, value);
168 }
169 }
170
171 @Override
172 public void onError(Throwable t) {
173 if (done) {
174 RxJavaPlugins.onError(t);
175 return;
176 }
177 done = true;
178 parent.onError(t);
179 }
180
181 @Override
182 public void onComplete() {
183 if (done) {
184 return;
185 }
186 done = true;
187 emit();
188 }
189 }
190 }
191 }