| 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.TimeUnit; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.exceptions.MissingBackpressureException; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 25 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 26 |
import io.reactivex.rxjava3.subscribers.SerializedSubscriber; |
| 27 |
|
| 28 |
public final class FlowableSampleTimed<T> extends AbstractFlowableWithUpstream<T, T> { |
| 29 |
final long period; |
| 30 |
final TimeUnit unit; |
| 31 |
final Scheduler scheduler; |
| 32 |
|
| 33 |
final boolean emitLast; |
| 34 |
|
| 35 |
public FlowableSampleTimed(Flowable<T> source, long period, TimeUnit unit, Scheduler scheduler, boolean emitLast) { |
| 36 |
super(source); |
| 37 |
this.period = period; |
| 38 |
this.unit = unit; |
| 39 |
this.scheduler = scheduler; |
| 40 |
this.emitLast = emitLast; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 45 |
SerializedSubscriber<T> serial = new SerializedSubscriber<>(s); |
| 46 |
if (emitLast) { |
| 47 |
source.subscribe(new SampleTimedEmitLast<>(serial, period, unit, scheduler)); |
| 48 |
} else { |
| 49 |
source.subscribe(new SampleTimedNoLast<>(serial, period, unit, scheduler)); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
abstract static class SampleTimedSubscriber<T> extends AtomicReference<T> implements FlowableSubscriber<T>, Subscription, Runnable { |
| 54 |
|
| 55 |
private static final long serialVersionUID = -3517602651313910099L; |
| 56 |
|
| 57 |
final Subscriber<? super T> downstream; |
| 58 |
final long period; |
| 59 |
final TimeUnit unit; |
| 60 |
final Scheduler scheduler; |
| 61 |
|
| 62 |
final AtomicLong requested = new AtomicLong(); |
| 63 |
|
| 64 |
final SequentialDisposable timer = new SequentialDisposable(); |
| 65 |
|
| 66 |
Subscription upstream; |
| 67 |
|
| 68 |
SampleTimedSubscriber(Subscriber<? super T> actual, long period, TimeUnit unit, Scheduler scheduler) { |
| 69 |
this.downstream = actual; |
| 70 |
this.period = period; |
| 71 |
this.unit = unit; |
| 72 |
this.scheduler = scheduler; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void onSubscribe(Subscription s) { |
| 77 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 78 |
this.upstream = s; |
| 79 |
downstream.onSubscribe(this); |
| 80 |
timer.replace(scheduler.schedulePeriodicallyDirect(this, period, period, unit)); |
| 81 |
s.request(Long.MAX_VALUE); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void onNext(T t) { |
| 87 |
lazySet(t); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public void onError(Throwable t) { |
| 92 |
cancelTimer(); |
| 93 |
downstream.onError(t); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void onComplete() { |
| 98 |
cancelTimer(); |
| 99 |
complete(); |
| 100 |
} |
| 101 |
|
| 102 |
void cancelTimer() { |
| 103 |
DisposableHelper.dispose(timer); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void request(long n) { |
| 108 |
if (SubscriptionHelper.validate(n)) { |
| 109 |
BackpressureHelper.add(requested, n); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void cancel() { |
| 115 |
cancelTimer(); |
| 116 |
upstream.cancel(); |
| 117 |
} |
| 118 |
|
| 119 |
void emit() { |
| 120 |
T value = getAndSet(null); |
| 121 |
if (value != null) { |
| 122 |
long r = requested.get(); |
| 123 |
if (r != 0L) { |
| 124 |
downstream.onNext(value); |
| 125 |
BackpressureHelper.produced(requested, 1); |
| 126 |
} else { |
| 127 |
cancel(); |
| 128 |
downstream.onError(new MissingBackpressureException("Couldn't emit value due to lack of requests!")); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
abstract void complete(); |
| 134 |
} |
| 135 |
|
| 136 |
static final class SampleTimedNoLast<T> extends SampleTimedSubscriber<T> { |
| 137 |
|
| 138 |
private static final long serialVersionUID = -7139995637533111443L; |
| 139 |
|
| 140 |
SampleTimedNoLast(Subscriber<? super T> actual, long period, TimeUnit unit, Scheduler scheduler) { |
| 141 |
super(actual, period, unit, scheduler); |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
void complete() { |
| 146 |
downstream.onComplete(); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void run() { |
| 151 |
emit(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
static final class SampleTimedEmitLast<T> extends SampleTimedSubscriber<T> { |
| 156 |
|
| 157 |
private static final long serialVersionUID = -7139995637533111443L; |
| 158 |
|
| 159 |
final AtomicInteger wip; |
| 160 |
|
| 161 |
SampleTimedEmitLast(Subscriber<? super T> actual, long period, TimeUnit unit, Scheduler scheduler) { |
| 162 |
super(actual, period, unit, scheduler); |
| 163 |
this.wip = new AtomicInteger(1); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
void complete() { |
| 168 |
emit(); |
| 169 |
if (wip.decrementAndGet() == 0) { |
| 170 |
downstream.onComplete(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public void run() { |
| 176 |
if (wip.incrementAndGet() == 2) { |
| 177 |
emit(); |
| 178 |
if (wip.decrementAndGet() == 0) { |
| 179 |
downstream.onComplete(); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |