feat: cancel all task when disable engine
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user