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

View File

@ -181,7 +181,15 @@ public abstract class ExecutableService implements IService {
this.delay = Objects.requireNonNullElse(delay, 0L);
this.period = Objects.requireNonNullElse(period, (20L * 60L) * 20L);
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
@ -245,6 +253,9 @@ public abstract class ExecutableService implements IService {
@Override
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
public @NotNull Mono<Void> runOnce(IService service) {
return Mono.just(service)
.doOnNext(s -> s.start()
.then(s.stop())
.subscribe())
.then();
return Mono.create(sink -> service.start().then(service.stop()).subscribe(sink::success));
}
@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.
*/
public @NotNull Mono<Void> stopService(@NotNull String service_name, @Nullable Mono<Disposable> disposable) {
getService(service_name).doOnNext(IService::stop).subscribe();
if (disposable != null) {
disposable.doOnNext(Disposable::dispose).subscribe();
}
return Mono.empty();
return Mono.create(sink -> {
getService(service_name).doOnNext(IService::stop).subscribe();
if (disposable != null) {
disposable.doOnNext(Disposable::dispose).subscribe();
}
sink.success();
});
}
/**
@ -223,7 +225,7 @@ public final class ServicePool implements Identifier {
*/
public @NotNull Mono<ServicePool> recycle() {
this.getAssociatedServices().clear();
return Mono.just(this);
return Mono.create(sink -> sink.success(this));
}
/**