feat: 调整Task任务模块

merge/3/MERGE
coding 2018-05-19 17:08:48 +00:00
parent f94fa71f36
commit 8f5bae006a
3 changed files with 62 additions and 25 deletions

View File

@ -1,2 +1,11 @@
'use strict';
/**
* MiaoScript Task处理类
* @namespace plugin.configFile.parentFile, command.enable, permission.enable
*/
/*global Java, base, module, exports, require*/
module.exports = requireInternal('task');
function TaskHandlerDefault() {
}
var TaskHandler = Object.assign(new TaskHandlerDefault(), requireInternal('task'));
exports = module.exports = TaskHandler;

View File

@ -10,14 +10,15 @@ var BukkitRunnable = Java.type("org.bukkit.scheduler.BukkitRunnable");
* 创建任务对象
* @param func 任务
*/
exports.create = function (func) {
function create(func) {
if (toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
return new BukkitRunnable(func);
};
/**
* 运行任务
* @param func 任务
*/
exports.run = function (func) {
function run(func) {
return exports.create(func).runTask(plugin);
};
/**
@ -25,7 +26,7 @@ exports.run = function (func) {
* @param func 任务
* @param time 延时时间
*/
exports.later = function (func, time) {
function later(func, time) {
return exports.create(func).runTaskLater(plugin, time);
};
/**
@ -33,19 +34,21 @@ exports.later = function (func, time) {
* @constructor (任务,执行间隔).
* @constructor (任务,首次延时,执行间隔)
*/
exports.timer = function () {
function timer() {
switch (arguments.length) {
case 2:
return exports.create(arguments[0]).runTaskTimer(plugin, 0, arguments[1]);
case 3:
return exports.create(arguments[0]).runTaskTimer(plugin, arguments[1], arguments[2]);
default:
throw TypeError('参数错误 task.timer(func, [delay], interval)');
}
};
/**
* 运行异步任务
* @param func function 任务
*/
exports.async = function (func) {
function _async(func) {
return exports.create(func).runTaskAsynchronously(plugin);
};
/**
@ -53,7 +56,7 @@ exports.async = function (func) {
* @param func 任务
* @param time 延时时间
*/
exports.laterAsync = function (func, time) {
function laterAsync(func, time) {
return exports.create(func).runTaskLaterAsynchronously(plugin, time);
};
/**
@ -61,11 +64,22 @@ exports.laterAsync = function (func, time) {
* @constructor (任务,执行间隔).
* @constructor (任务,首次延时,执行间隔)
*/
exports.timerAsync = function () {
function timerAsync() {
switch (arguments.length) {
case 2:
return exports.create(arguments[0]).runTaskTimerAsynchronously(plugin, 0, arguments[1]);
case 3:
return exports.create(arguments[0]).runTaskTimerAsynchronously(plugin, arguments[1], arguments[2]);
default:
throw TypeError('参数错误 task.timerAsync(func, [delay], interval)');
}
};
};
exports = module.exports = {
run: run,
later: later,
timer: timer,
async: _async,
laterAsync: laterAsync,
timerAsync: timerAsync
}

View File

@ -11,7 +11,8 @@ var Task = Java.type("org.spongepowered.api.scheduler.Task");
* 创建任务对象
* @param func 任务
*/
exports.create = function (func) {
function create(func) {
if (toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
return Task.builder().execute(new Consumer(function () {
try {
func();
@ -24,55 +25,68 @@ exports.create = function (func) {
* 运行任务
* @param func 任务
*/
exports.run = function (func) {
return exports.create(func).submit(plugin);
function run(func) {
return create(func).submit(plugin);
};
/**
* 延时运行任务
* @param func 任务
* @param time 延时时间
*/
exports.later = function (func, time) {
return exports.create(func).delayTicks(time).submit(plugin);
function later(func, time) {
return create(func).delayTicks(time).submit(plugin);
};
/**
* 运行循环任务
* @constructor (任务,执行间隔).
* @constructor (任务,首次延时,执行间隔)
*/
exports.timer = function () {
function timer() {
switch (arguments.length) {
case 2:
return exports.create(arguments[0]).intervalTicks(arguments[1]).submit(plugin);
return create(arguments[0]).intervalTicks(arguments[1]).submit(plugin);
case 3:
return exports.create(arguments[0]).delayTicks(arguments[1]).intervalTicks(arguments[2]).submit(plugin);
return create(arguments[0]).delayTicks(arguments[1]).intervalTicks(arguments[2]).submit(plugin);
default:
throw TypeError('参数错误 task.timer(func, [delay], interval)');
}
};
/**
* 运行异步任务
* @param func function 任务
*/
exports.async = function (func) {
return exports.create(func).async().submit(plugin);
function _async(func) {
return create(func).async().submit(plugin);
};
/**
* 延时运行异步任务
* @param func 任务
* @param time 延时时间
*/
exports.laterAsync = function (func, time) {
return exports.create(func).async().delayTicks(time).submit(plugin);
function laterAsync(func, time) {
return create(func).async().delayTicks(time).submit(plugin);
};
/**
* 运行异步循环任务
* @constructor (任务,执行间隔).
* @constructor (任务,首次延时,执行间隔)
*/
exports.timerAsync = function () {
function timerAsync() {
switch (arguments.length) {
case 2:
return exports.create(arguments[0]).async().intervalTicks(arguments[1]).submit(plugin);
return create(arguments[0]).async().intervalTicks(arguments[1]).submit(plugin);
case 3:
return exports.create(arguments[0]).async().delayTicks(arguments[1]).intervalTicks(arguments[2]).submit(plugin);
return create(arguments[0]).async().delayTicks(arguments[1]).intervalTicks(arguments[2]).submit(plugin);
default:
throw TypeError('参数错误 task.timerAsync(func, [delay], interval)');
}
};
};
exports = module.exports = {
run: run,
later: later,
timer: timer,
async: _async,
laterAsync: laterAsync,
timerAsync: timerAsync
}