feat: optimize task impl

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-09-22 18:42:39 +08:00
parent e7077b1315
commit b87fb08d1c
9 changed files with 167 additions and 90 deletions

View File

@ -8,7 +8,7 @@ const Callable = Java.type('java.util.concurrent.Callable')
const TimeUnit = Java.type('java.util.concurrent.TimeUnit')
@provideSingleton(task.TaskManager)
export class SpongeTaskManager implements task.TaskManager {
export class SpongeTaskManager extends task.TaskManager {
@inject(plugin.PluginInstance)
private pluginInstance: any
private syncExecutor: any
@ -18,21 +18,20 @@ export class SpongeTaskManager implements task.TaskManager {
this.syncExecutor = Sponge.getScheduler().createSyncExecutor(this.pluginInstance)
}
create(func: Function): task.Task {
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !') }
create0(func: Function): task.Task {
return new SpongeTask(this.pluginInstance, func)
}
callSyncMethod(func: Function): any {
// @ts-ignore
return this.syncExecutor.schedule(new Callable({ call: () => func() }), java.lang.Long.valueOf(0), TimeUnit.NANOSECONDS).get()
}
disable() {
disable0() {
Sponge.getScheduler().getScheduledTasks(this.pluginInstance).forEach((task: task.Cancelable) => task.cancel())
}
}
export class SpongeTask extends task.Task {
submit(...args: any[]): task.Cancelable {
submit0(...args: any[]): task.Cancelable {
let run = Task.builder().execute(new Consumer({ accept: () => this.run(...args) }))
if (this.isAsync) { run.async() }
if (this.laterTime) { run.delayTicks(this.laterTime) }