feat: cancel all task when disable engine

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

View File

@ -11,6 +11,10 @@ export namespace task {
* @param func
*/
callSyncMethod(func: Function): any;
/**
*
*/
disable();
}
/**
*

View File

@ -17,6 +17,9 @@ export class BukkitTaskManager implements task.TaskManager {
callSyncMethod(func: Function): any {
return Bukkit.getScheduler().callSyncMethod(this.pluginInstance, new Callable({ call: () => func() })).get()
}
disable() {
Bukkit.getScheduler().cancelTasks(this.pluginInstance);
}
}
export class BukkitTask extends task.Task {

View File

@ -16,6 +16,9 @@ export class BungeeTaskManager implements task.TaskManager {
callSyncMethod(func: Function): any {
return func();
}
disable() {
this.pluginInstance.getProxy().getScheduler().cancel(this.pluginInstance)
}
}
export class BungeeTask extends task.Task {

View File

@ -48,6 +48,7 @@ class MiaoScriptCore {
disable() {
console.i18n("ms.core.engine.disable")
this.pluginManager.disable();
this.taskManager.disable();
}
}

View File

@ -15,6 +15,9 @@ export class NukkitTaskManager implements task.TaskManager {
callSyncMethod(func: Function): any {
return func()
}
disable() {
base.getInstance().getServer().getScheduler().cancelTask(this.pluginInstance)
}
}
export class NukkitTask extends task.Task {

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())
}
}