feat: cancel all task when disable engine

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-04-03 11:36:06 +08:00
parent 24fe3b9026
commit e74cfda86b
6 changed files with 26 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { task, plugin } from '@ms/api'
import { inject, provideSingleton } from '@ms/container';
import { inject, provideSingleton, postConstruct } from '@ms/container';
const Sponge = Java.type("org.spongepowered.api.Sponge");
const Task = Java.type("org.spongepowered.api.scheduler.Task");
@ -11,16 +11,23 @@ const TimeUnit = Java.type('java.util.concurrent.TimeUnit');
export class SpongeTaskManager implements task.TaskManager {
@inject(plugin.PluginInstance)
private pluginInstance: any;
private syncExecutor: any;
@postConstruct()
initialize() {
this.syncExecutor = Sponge.getScheduler().createSyncExecutor(this.pluginInstance)
}
create(func: Function): task.Task {
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
return new SpongeTask(this.pluginInstance, func);
}
callSyncMethod(func: Function): any {
return Sponge.getScheduler()
.createSyncExecutor(this.pluginInstance)
// @ts-ignore
.schedule(new Callable({ call: () => func() }), java.lang.Long.valueOf(0), TimeUnit.NANOSECONDS).get()
// @ts-ignore
return this.syncExecutor.schedule(new Callable({ call: () => func() }), java.lang.Long.valueOf(0), TimeUnit.NANOSECONDS).get()
}
disable() {
Sponge.getScheduler().getScheduledTasks(this.pluginInstance).forEach((task: task.Cancelable) => task.cancel())
}
}