feat: add callSyncMethod

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-01-17 11:08:51 +08:00
parent 13a0b62103
commit 363d0c164a
4 changed files with 18 additions and 3 deletions

View File

@ -4,6 +4,7 @@ export namespace task {
export const TaskManager = Symbol('TaskManager')
export interface TaskManager {
create(func: Function): task.Task;
callSyncMethod(func: Function): any;
}
/**
*

View File

@ -1,7 +1,9 @@
import { task, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container';
let BukkitRunnable = Java.type('org.bukkit.scheduler.BukkitRunnable');
const Bukkit = Java.type('org.bukkit.Bukkit');
const BukkitRunnable = Java.type('org.bukkit.scheduler.BukkitRunnable');
const Callable = Java.type('java.util.concurrent.Callable')
@injectable()
export class BukkitTaskManager implements task.TaskManager {
@ -12,6 +14,9 @@ export class BukkitTaskManager implements task.TaskManager {
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
return new BukkitTask(this.pluginInstance, func);
}
callSyncMethod(func: Function): any {
return Bukkit.getScheduler().callSyncMethod(this.pluginInstance, new Callable({ call: () => func() })).get()
}
}
export class BukkitTask extends task.Task {

View File

@ -13,6 +13,9 @@ export class BungeeTaskManager implements task.TaskManager {
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
return new BungeeTask(this.pluginInstance, func);
}
callSyncMethod(func: Function): any {
return func();
}
}
export class BungeeTask extends task.Task {

View File

@ -1,8 +1,11 @@
import { task, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container';
var Consumer = Java.type('java.util.function.Consumer');
var Task = Java.type("org.spongepowered.api.scheduler.Task");
const Sponge = Java.type("org.spongepowered.api.Sponge");
const Task = Java.type("org.spongepowered.api.scheduler.Task");
const Consumer = Java.type('java.util.function.Consumer');
const Callable = Java.type('java.util.concurrent.Callable');
const TimeUnit = Java.type('java.util.concurrent.TimeUnit');
@injectable()
export class SpongeTaskManager implements task.TaskManager {
@ -13,6 +16,9 @@ export class SpongeTaskManager implements task.TaskManager {
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).schedule(new Callable({ call: () => func() }), 0, TimeUnit.NANOSECONDS).get()
}
}
export class SpongeTask extends task.Task {