package reactor.core.publisher;
import java.util.Objects;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.Disposable;
import reactor.core.scheduler.Scheduler;
import reactor.util.annotation.Nullable;
final class MonoDelayElement<T> extends MonoOperator<T, T> {
final Scheduler timedScheduler;
final long delay;
final TimeUnit unit;
MonoDelayElement(Mono<? extends T> source, long delay, TimeUnit unit, Scheduler timedScheduler) {
super(source);
this.delay = delay;
this.unit = Objects.requireNonNull(unit, "unit");
this.timedScheduler = Objects.requireNonNull(timedScheduler, "timedScheduler");
}
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
source.subscribe(new DelayElementSubscriber<>(actual, timedScheduler, delay, unit));
}
@Override
public Object scanUnsafe(Attr key) {
if (key == Attr.RUN_ON) return timedScheduler;
return super.scanUnsafe(key);
}
static final class DelayElementSubscriber<T> extends Operators.MonoSubscriber<T,T> {
final long delay;
final Scheduler scheduler;
final TimeUnit unit;
Subscription s;
volatile Disposable task;
boolean done;
DelayElementSubscriber(CoreSubscriber<? super T> actual, Scheduler scheduler,
long delay, TimeUnit unit) {
super(actual);
this.scheduler = scheduler;
this.delay = delay;
this.unit = unit;
}
@Override
@Nullable
public Object scanUnsafe(Attr key) {
if (key == Attr.TERMINATED) return done;
if (key == Attr.PARENT) return s;
if (key == Attr.RUN_ON) return scheduler;
return super.scanUnsafe(key);
}
@Override
public void cancel() {
super.cancel();
if (task != null) {
task.dispose();
}
if (s != Operators.cancelledSubscription()) {
s.cancel();
}
}
@Override
public void onSubscribe(Subscription s) {
if (Operators.validate(this.s, s)) {
this.s = s;
actual.onSubscribe(this);
s.request(Long.MAX_VALUE);
}
}
@Override
public void onNext(T t) {
if (done) {
Operators.onNextDropped(t, actual.currentContext());
return;
}
this.done = true;
try {
this.task = scheduler.schedule(() -> complete(t), delay, unit);
}
catch (RejectedExecutionException ree) {
throw Operators.onRejectedExecution(ree, this, null, t,
actual.currentContext());
}
}
@Override
public void onComplete() {
if (done) {
return;
}
this.done = true;
actual.onComplete();
}
@Override
public void onError(Throwable t) {
if (done) {
Operators.onErrorDropped(t, actual.currentContext());
return;
}
this.done = true;
actual.onError(t);
}
}
}