diff --git a/game/game.js b/game/game.js index 257a41015..e1baacfc3 100644 --- a/game/game.js +++ b/game/game.js @@ -357,11 +357,32 @@ /** * **无名杀频道推送机制** * - * 模拟`Golang`的`channel`,仅保留部分特征 + * 鉴于`Javascript`的特性及自身对所需功能的思考,这是一个参考`Golang`的`channel`设计的、完全和`go channel`不一样的异步消息传递对象 + * + * 当且仅当接收方和发送方均存在时进行消息传递,完全保证信息传递的单一性(发送方/接收方一旦确定则无法更改)和准确性(发送方必然将消息发送给接收方) + * + * 若存在发送方/接收方时调用`send`/`receive`,将报错 + * + * 若需要异步/不报错发送信息,请等待`lib.actor` * * @template T + * @example + * // 创建一个频道 + * const channel = new lib.channel(); + * + * // 从某个角落接收channel发出的消息,若无消息则等待 + * const message = await channel.receive(); + * + * // 从某个角落向channel发消息,若无消息接收则等待 + * await channel.send(item); */ channel: class { + /** + * @template TValue + * @callback PromiseResolve + * @param {TValue} value + * @returns {void} + */ constructor() { /** * @type {"active" | "receiving" | "sending"} @@ -369,13 +390,15 @@ this.status = "active"; /** - * @type {Promise | [T, Promise] | null} + * @type {PromiseResolve | [T, PromiseResolve] | null} */ this._buffer = null; } /** - * @param {T} value + * 向该频道发送消息,在消息未被接受前将等待 + * + * @param {T} value - 要发送的消息 * @returns {Promise} */ send(value) { @@ -387,7 +410,7 @@ break; case "receiving": /** - * @type {Promise} + * @type {PromiseResolve} */ const buffer = this._buffer; this._buffer = null; @@ -404,7 +427,9 @@ } /** - * @returns {Promise} + * 接收频道所发送的消息,若无消息发送则等待 + * + * @returns {Promise} 接收到的消息 */ receive() { return new Promise((resolve, reject) => { @@ -415,7 +440,7 @@ break; case "sending": /** - * @type {[T, Promise]} + * @type {[T, PromiseResolve]} */ const buffer = this._buffer; this._buffer = null;