From 1c98c19e37d3856a4a71c0158ea18de797bf60e8 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 3 Apr 2020 11:36:06 +0800 Subject: [PATCH] feat: cancel all task when disable engine Signed-off-by: MiaoWoo --- packages/api/src/task.ts | 4 ++++ packages/bukkit/src/task.ts | 3 +++ packages/bungee/src/task.ts | 3 +++ packages/core/src/index.ts | 1 + packages/nukkit/src/task.ts | 3 +++ packages/sponge/src/task.ts | 17 ++++++++++++----- 6 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/api/src/task.ts b/packages/api/src/task.ts index 09e6ca7b..786defb3 100644 --- a/packages/api/src/task.ts +++ b/packages/api/src/task.ts @@ -11,6 +11,10 @@ export namespace task { * @param func 执行内容 */ callSyncMethod(func: Function): any; + /** + * 关闭任务管理器 + */ + disable(); } /** * 任务抽象 diff --git a/packages/bukkit/src/task.ts b/packages/bukkit/src/task.ts index 0f329fdb..7319dd5b 100644 --- a/packages/bukkit/src/task.ts +++ b/packages/bukkit/src/task.ts @@ -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 { diff --git a/packages/bungee/src/task.ts b/packages/bungee/src/task.ts index c87921bb..656ecdd8 100644 --- a/packages/bungee/src/task.ts +++ b/packages/bungee/src/task.ts @@ -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 { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index a9bd2eea..876481b2 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -48,6 +48,7 @@ class MiaoScriptCore { disable() { console.i18n("ms.core.engine.disable") this.pluginManager.disable(); + this.taskManager.disable(); } } diff --git a/packages/nukkit/src/task.ts b/packages/nukkit/src/task.ts index 3de76d80..267448aa 100644 --- a/packages/nukkit/src/task.ts +++ b/packages/nukkit/src/task.ts @@ -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 { diff --git a/packages/sponge/src/task.ts b/packages/sponge/src/task.ts index d0cba0f5..f0d1f18d 100644 --- a/packages/sponge/src/task.ts +++ b/packages/sponge/src/task.ts @@ -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()) } }