Switch to using Mono.create()

Switched from using Mono.just(T) and Mono.empty() for creating Mono<Void> objects to Mono.create(sink -> {});
This commit is contained in:
Paldiu 2022-12-15 13:32:28 -06:00
parent 43dee08554
commit c9fa0c1ca0
4 changed files with 30 additions and 22 deletions

View File

@ -26,7 +26,6 @@ package io.github.simplexdevelopment.impl;
import io.github.simplexdevelopment.scheduler.ExecutableService; import io.github.simplexdevelopment.scheduler.ExecutableService;
import io.github.simplexdevelopment.scheduler.ServicePool; import io.github.simplexdevelopment.scheduler.ServicePool;
import org.bukkit.plugin.java.JavaPlugin;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
public class ServiceImpl extends ExecutableService { public class ServiceImpl extends ExecutableService {
@ -39,18 +38,18 @@ public class ServiceImpl extends ExecutableService {
@Override @Override
public Mono<Void> start() { public Mono<Void> start() {
return Mono.just(plugin) return Mono.create(sink -> {
.map(JavaPlugin::getLogger) plugin.getLogger().info("The service has started successfully!");
.doOnNext(l -> l.info("The service has executed successfully!")) sink.success();
.then(); });
} }
@Override @Override
public Mono<Void> stop() { public Mono<Void> stop() {
return Mono.just(plugin) return Mono.create(sink -> {
.map(JavaPlugin::getLogger) plugin.getLogger().info("The service has stopped successfully!");
.doOnNext(l -> l.info("The service has stopped")) sink.success();
.then(); });
} }
@Override @Override

View File

@ -181,7 +181,15 @@ public abstract class ExecutableService implements IService {
this.delay = Objects.requireNonNullElse(delay, 0L); this.delay = Objects.requireNonNullElse(delay, 0L);
this.period = Objects.requireNonNullElse(period, (20L * 60L) * 20L); this.period = Objects.requireNonNullElse(period, (20L * 60L) * 20L);
this.mayInterruptWhenRunning = mayInterruptWhenRunning; this.mayInterruptWhenRunning = mayInterruptWhenRunning;
this.parentPool = parentPool;
if (parentPool == null) {
this.parentPool = new ServicePool("defaultPool" + SchedulingSystem.denom, false);
SchedulingSystem.denom++;
} else {
this.parentPool = parentPool;
}
this.parentPool.getAssociatedServices().add(this);
} }
@Override @Override
@ -245,6 +253,9 @@ public abstract class ExecutableService implements IService {
@Override @Override
public Mono<Void> setParentPool(ServicePool servicePool) { public Mono<Void> setParentPool(ServicePool servicePool) {
return Mono.just(servicePool).doOnNext(pool -> this.parentPool = pool).then(); return Mono.create(sink -> {
this.parentPool = servicePool;
sink.success();
});
} }
} }

View File

@ -102,11 +102,7 @@ public final class SchedulingSystem<T extends JavaPlugin> implements ISchedule {
@Override @Override
public @NotNull Mono<Void> runOnce(IService service) { public @NotNull Mono<Void> runOnce(IService service) {
return Mono.just(service) return Mono.create(sink -> service.start().then(service.stop()).subscribe(sink::success));
.doOnNext(s -> s.start()
.then(s.stop())
.subscribe())
.then();
} }
@Override @Override

View File

@ -186,11 +186,13 @@ public final class ServicePool implements Identifier {
* @return A {@link Mono<Void>} object which can be used to stop the service. * @return A {@link Mono<Void>} object which can be used to stop the service.
*/ */
public @NotNull Mono<Void> stopService(@NotNull String service_name, @Nullable Mono<Disposable> disposable) { public @NotNull Mono<Void> stopService(@NotNull String service_name, @Nullable Mono<Disposable> disposable) {
getService(service_name).doOnNext(IService::stop).subscribe(); return Mono.create(sink -> {
if (disposable != null) { getService(service_name).doOnNext(IService::stop).subscribe();
disposable.doOnNext(Disposable::dispose).subscribe(); if (disposable != null) {
} disposable.doOnNext(Disposable::dispose).subscribe();
return Mono.empty(); }
sink.success();
});
} }
/** /**
@ -223,7 +225,7 @@ public final class ServicePool implements Identifier {
*/ */
public @NotNull Mono<ServicePool> recycle() { public @NotNull Mono<ServicePool> recycle() {
this.getAssociatedServices().clear(); this.getAssociatedServices().clear();
return Mono.just(this); return Mono.create(sink -> sink.success(this));
} }
/** /**