| 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 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 22 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 23 |
import io.reactivex.rxjava3.subscribers.SerializedSubscriber; |
| 24 |
|
| 25 |
public final class FlowableDelay<T> extends AbstractFlowableWithUpstream<T, T> { |
| 26 |
final long delay; |
| 27 |
final TimeUnit unit; |
| 28 |
final Scheduler scheduler; |
| 29 |
final boolean delayError; |
| 30 |
|
| 31 |
public FlowableDelay(Flowable<T> source, long delay, TimeUnit unit, Scheduler scheduler, boolean delayError) { |
| 32 |
super(source); |
| 33 |
this.delay = delay; |
| 34 |
this.unit = unit; |
| 35 |
this.scheduler = scheduler; |
| 36 |
this.delayError = delayError; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
protected void subscribeActual(Subscriber<? super T> t) { |
| 41 |
Subscriber<? super T> downstream; |
| 42 |
if (delayError) { |
| 43 |
downstream = t; |
| 44 |
} else { |
| 45 |
downstream = new SerializedSubscriber<>(t); |
| 46 |
} |
| 47 |
|
| 48 |
Scheduler.Worker w = scheduler.createWorker(); |
| 49 |
|
| 50 |
source.subscribe(new DelaySubscriber<>(downstream, delay, unit, w, delayError)); |
| 51 |
} |
| 52 |
|
| 53 |
static final class DelaySubscriber<T> implements FlowableSubscriber<T>, Subscription { |
| 54 |
final Subscriber<? super T> downstream; |
| 55 |
final long delay; |
| 56 |
final TimeUnit unit; |
| 57 |
final Scheduler.Worker w; |
| 58 |
final boolean delayError; |
| 59 |
|
| 60 |
Subscription upstream; |
| 61 |
|
| 62 |
DelaySubscriber(Subscriber<? super T> actual, long delay, TimeUnit unit, Worker w, boolean delayError) { |
| 63 |
super(); |
| 64 |
this.downstream = actual; |
| 65 |
this.delay = delay; |
| 66 |
this.unit = unit; |
| 67 |
this.w = w; |
| 68 |
this.delayError = delayError; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onSubscribe(Subscription s) { |
| 73 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 74 |
this.upstream = s; |
| 75 |
downstream.onSubscribe(this); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void onNext(final T t) { |
| 81 |
w.schedule(new OnNext(t), delay, unit); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void onError(final Throwable t) { |
| 86 |
w.schedule(new OnError(t), delayError ? delay : 0, unit); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void onComplete() { |
| 91 |
w.schedule(new OnComplete(), delay, unit); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void request(long n) { |
| 96 |
upstream.request(n); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void cancel() { |
| 101 |
upstream.cancel(); |
| 102 |
w.dispose(); |
| 103 |
} |
| 104 |
|
| 105 |
final class OnNext implements Runnable { |
| 106 |
private final T t; |
| 107 |
|
| 108 |
OnNext(T t) { |
| 109 |
this.t = t; |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void run() { |
| 114 |
downstream.onNext(t); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
final class OnError implements Runnable { |
| 119 |
private final Throwable t; |
| 120 |
|
| 121 |
OnError(Throwable t) { |
| 122 |
this.t = t; |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public void run() { |
| 127 |
try { |
| 128 |
downstream.onError(t); |
| 129 |
} finally { |
| 130 |
w.dispose(); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
final class OnComplete implements Runnable { |
| 136 |
@Override |
| 137 |
public void run() { |
| 138 |
try { |
| 139 |
downstream.onComplete(); |
| 140 |
} finally { |
| 141 |
w.dispose(); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |