Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableThrottleLatest.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.concurrent.TimeUnit;
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.internal.disposables.DisposableHelper;
22
23 /**
24 * Emits the next or latest item when the given time elapses.
25 * <p>
26 * The operator emits the next item, then starts a timer. When the timer fires,
27 * it tries to emit the latest item from upstream. If there was no upstream item,
28 * in the meantime, the next upstream item is emitted immediately and the
29 * timed process repeats.
30 * <p>History: 2.1.14 - experimental
31 * @param <T> the upstream and downstream value type
32 * @since 2.2
33 */
34 public final class ObservableThrottleLatest<T> extends AbstractObservableWithUpstream<T, T> {
35
36 final long timeout;
37
38 final TimeUnit unit;
39
40 final Scheduler scheduler;
41
42 final boolean emitLast;
43
44 public ObservableThrottleLatest(Observable<T> source,
45 long timeout, TimeUnit unit, Scheduler scheduler,
46 boolean emitLast) {
47 super(source);
48 this.timeout = timeout;
49 this.unit = unit;
50 this.scheduler = scheduler;
51 this.emitLast = emitLast;
52 }
53
54 @Override
55 protected void subscribeActual(Observer<? super T> observer) {
56 source.subscribe(new ThrottleLatestObserver<>(observer, timeout, unit, scheduler.createWorker(), emitLast));
57 }
58
59 static final class ThrottleLatestObserver<T>
60 extends AtomicInteger
61 implements Observer<T>, Disposable, Runnable {
62
63 private static final long serialVersionUID = -8296689127439125014L;
64
65 final Observer<? super T> downstream;
66
67 final long timeout;
68
69 final TimeUnit unit;
70
71 final Scheduler.Worker worker;
72
73 final boolean emitLast;
74
75 final AtomicReference<T> latest;
76
77 Disposable upstream;
78
79 volatile boolean done;
80 Throwable error;
81
82 volatile boolean cancelled;
83
84 volatile boolean timerFired;
85
86 boolean timerRunning;
87
88 ThrottleLatestObserver(Observer<? super T> downstream,
89 long timeout, TimeUnit unit, Scheduler.Worker worker,
90 boolean emitLast) {
91 this.downstream = downstream;
92 this.timeout = timeout;
93 this.unit = unit;
94 this.worker = worker;
95 this.emitLast = emitLast;
96 this.latest = new AtomicReference<>();
97 }
98
99 @Override
100 public void onSubscribe(Disposable d) {
101 if (DisposableHelper.validate(upstream, d)) {
102 upstream = d;
103 downstream.onSubscribe(this);
104 }
105 }
106
107 @Override
108 public void onNext(T t) {
109 latest.set(t);
110 drain();
111 }
112
113 @Override
114 public void onError(Throwable t) {
115 error = t;
116 done = true;
117 drain();
118 }
119
120 @Override
121 public void onComplete() {
122 done = true;
123 drain();
124 }
125
126 @Override
127 public void dispose() {
128 cancelled = true;
129 upstream.dispose();
130 worker.dispose();
131 if (getAndIncrement() == 0) {
132 latest.lazySet(null);
133 }
134 }
135
136 @Override
137 public boolean isDisposed() {
138 return cancelled;
139 }
140
141 @Override
142 public void run() {
143 timerFired = true;
144 drain();
145 }
146
147 void drain() {
148 if (getAndIncrement() != 0) {
149 return;
150 }
151
152 int missed = 1;
153
154 AtomicReference<T> latest = this.latest;
155 Observer<? super T> downstream = this.downstream;
156
157 for (;;) {
158
159 for (;;) {
160 if (cancelled) {
161 latest.lazySet(null);
162 return;
163 }
164
165 boolean d = done;
166
167 if (d && error != null) {
168 latest.lazySet(null);
169 downstream.onError(error);
170 worker.dispose();
171 return;
172 }
173
174 T v = latest.get();
175 boolean empty = v == null;
176
177 if (d) {
178 v = latest.getAndSet(null);
179 if (!empty && emitLast) {
180 downstream.onNext(v);
181 }
182 downstream.onComplete();
183 worker.dispose();
184 return;
185 }
186
187 if (empty) {
188 if (timerFired) {
189 timerRunning = false;
190 timerFired = false;
191 }
192 break;
193 }
194
195 if (!timerRunning || timerFired) {
196 v = latest.getAndSet(null);
197 downstream.onNext(v);
198
199 timerFired = false;
200 timerRunning = true;
201 worker.schedule(this, timeout, unit);
202 } else {
203 break;
204 }
205 }
206
207 missed = addAndGet(-missed);
208 if (missed == 0) {
209 break;
210 }
211 }
212 }
213 }
214 }