From 8f5bae006aee2b926888d2e3f9e8ea1c11c1043b Mon Sep 17 00:00:00 2001 From: coding Date: Sat, 19 May 2018 17:08:48 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4Task=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/api/task.js | 11 +++++- src/main/resources/internal/bukkit/task.js | 30 ++++++++++---- src/main/resources/internal/sponge/task.js | 46 ++++++++++++++-------- 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/src/main/resources/api/task.js b/src/main/resources/api/task.js index d38a523..32ac645 100644 --- a/src/main/resources/api/task.js +++ b/src/main/resources/api/task.js @@ -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'); \ No newline at end of file +function TaskHandlerDefault() { +} +var TaskHandler = Object.assign(new TaskHandlerDefault(), requireInternal('task')); + +exports = module.exports = TaskHandler; diff --git a/src/main/resources/internal/bukkit/task.js b/src/main/resources/internal/bukkit/task.js index 461b788..b60db05 100644 --- a/src/main/resources/internal/bukkit/task.js +++ b/src/main/resources/internal/bukkit/task.js @@ -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)'); } -}; \ No newline at end of file +}; + +exports = module.exports = { + run: run, + later: later, + timer: timer, + async: _async, + laterAsync: laterAsync, + timerAsync: timerAsync +} \ No newline at end of file diff --git a/src/main/resources/internal/sponge/task.js b/src/main/resources/internal/sponge/task.js index 977fa67..521946a 100644 --- a/src/main/resources/internal/sponge/task.js +++ b/src/main/resources/internal/sponge/task.js @@ -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)'); } -}; \ No newline at end of file +}; + +exports = module.exports = { + run: run, + later: later, + timer: timer, + async: _async, + laterAsync: laterAsync, + timerAsync: timerAsync +} \ No newline at end of file