UNPKG

yoni-mcscripts-lib

Version:

为 Minecraft Script API 中的部分接口创建了 wrapper,并提供简单的事件管理器和任务管理器,另附有一些便于代码编写的一些小工具。

50 lines (49 loc) 1.59 kB
import { CommandQueueExecuteStatus } from "./CommandQueueExecuteStatus.js"; export class AsyncCommandQueue { sender; command; #status = CommandQueueExecuteStatus.executing; get status() { return this.#status; } #result; get result() { if (this.status === CommandQueueExecuteStatus.executing) throw new Error("command executing have not been done"); return this.#result; } async resolveResult(commandResult) { if (this.#status !== CommandQueueExecuteStatus.executing) { return; } try { this.#result = await commandResult; this.#status = CommandQueueExecuteStatus.done; } catch (err) { this.reject(err); this.#status = CommandQueueExecuteStatus.failed; } if (this.#status === CommandQueueExecuteStatus.done) this.#resolveCallback(this.result); else this.#rejectCallback(this.result); } reject(err) { if (this.status === CommandQueueExecuteStatus.executing) { this.#status = CommandQueueExecuteStatus.failed; this.#result = err; } } #resolveCallback; #rejectCallback; constructor(sender, command, resolve, reject) { if (typeof sender?.runCommandAsync !== "function") { throw new TypeError("sender cannot runCommandAsync()"); } this.sender = sender; this.command = String(command); this.#resolveCallback = resolve; this.#rejectCallback = reject; } }