io.github.sinri.keel.facade.async.FutureRepeat Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Keel Show documentation
Show all versions of Keel Show documentation
A website framework with VERT.X for ex-PHP-ers, exactly Ark Framework Users.
package io.github.sinri.keel.facade.async;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import javax.annotation.Nonnull;
import java.util.function.Function;
import static io.github.sinri.keel.facade.KeelInstance.Keel;
/**
* @since 2.9.3
*/
public class FutureRepeat {
private final Function> routineFunction;
private FutureRepeat(@Nonnull Function> routineFunction) {
this.routineFunction = routineFunction;
}
static Future call(@Nonnull Function> routineFunction) {
Promise promise = Promise.promise();
RoutineResult routineResult = new RoutineResult(false);
new FutureRepeat(routineFunction).routine(routineResult, promise);
return promise.future();
}
private void routine(@Nonnull RoutineResult routineResult, @Nonnull Promise finalPromise) {
Future.succeededFuture()
.compose(v -> routineFunction.apply(routineResult))
.andThen(shouldStopAR -> {
if (shouldStopAR.succeeded()) {
if (routineResult.isToStop()) {
finalPromise.complete();
} else {
Keel.getVertx().setTimer(1L, x -> routine(routineResult, finalPromise));
}
} else {
finalPromise.fail(shouldStopAR.cause());
}
});
}
public static class RoutineResult {
private boolean toStop;
public RoutineResult(boolean toStop) {
this.toStop = toStop;
}
public boolean isToStop() {
return toStop;
}
public RoutineResult stop() {
this.toStop = true;
return this;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy