diff --git a/noname/library/channel/index.js b/noname/library/channel/index.js new file mode 100644 index 000000000..2a76543fc --- /dev/null +++ b/noname/library/channel/index.js @@ -0,0 +1,87 @@ + +/** + * @template T + */ +export class Channel { + /** + * @template U + * @typedef {(value?: U | PromiseLike) => void} PromiseResolve + */ + constructor() { + /** + * @type {"active" | "receiving" | "sending"} + */ + this.status = "active"; + + /** + * @type {PromiseResolve | [T, PromiseResolve] | null} + */ + this._buffer = null; + } + + /** + * 向该频道发送消息,在消息未被接受前将等待 + * + * @param {T} value - 要发送的消息 + * @returns {Promise} + */ + send(value) { + return new Promise((resolve, reject) => { + switch (this.status) { + case "sending": + // TODO: handle the error. + reject(new Error()); + break; + case "receiving": { + /** + * @type {PromiseResolve} + */ + // @ts-ignore + const buffer = this._buffer; + this._buffer = null; + buffer(value); + this.status = "active"; + resolve(); + break; + } + case "active": + this.status = "sending"; + this._buffer = [value, resolve]; + break; + } + }); + } + + /** + * 接收频道所发送的消息,若无消息发送则等待 + * + * @returns {Promise} 接收到的消息 + */ + receive() { + return new Promise((resolve, reject) => { + switch (this.status) { + case "receiving": + // TODO: handle the error. + reject(new Error()); + break; + case "sending": { + /** + * @type {[T, PromiseResolve]} + */ + // @ts-ignore + const buffer = this._buffer; + this._buffer = null; + resolve(buffer[0]); + this.status = "active"; + buffer[1](); + break; + } + case "active": + this.status = "receiving"; + // @ts-ignore + this._buffer = resolve; + break; + } + }); + } +} diff --git a/noname/library/index.js b/noname/library/index.js index ce06cee35..cd410b079 100644 --- a/noname/library/index.js +++ b/noname/library/index.js @@ -20,6 +20,7 @@ import { GNC as gnc } from '../gnc/index.js'; import { LibInit } from "./init/index.js"; import { Announce } from "./announce/index.js"; +import { Channel } from "./channel/index.js"; import * as Element from "./element/index.js"; @@ -320,88 +321,7 @@ export class Library extends Uninstantable { * // 从某个角落向channel发消息,若无消息接收则等待 * await channel.send(item); */ - static channel = class { - /** - * @template TValue - * @callback PromiseResolve - * @param {TValue} value - * @returns {void} - */ - constructor() { - /** - * @type {"active" | "receiving" | "sending"} - */ - this.status = "active"; - - /** - * @type {PromiseResolve | [T, PromiseResolve] | null} - */ - this._buffer = null; - } - - /** - * 向该频道发送消息,在消息未被接受前将等待 - * - * @param {T} value - 要发送的消息 - * @returns {Promise} - */ - send(value) { - return new Promise((resolve, reject) => { - switch (this.status) { - case "sending": - // TODO: handle the error. - reject(new Error()); - break; - case "receiving": { - /** - * @type {PromiseResolve} - */ - const buffer = this._buffer; - this._buffer = null; - buffer(value); - this.status = "active"; - resolve(); - break; - } - case "active": - this.status = "sending"; - this._buffer = [value, resolve]; - break; - } - }); - } - - /** - * 接收频道所发送的消息,若无消息发送则等待 - * - * @returns {Promise} 接收到的消息 - */ - receive() { - return new Promise((resolve, reject) => { - switch (this.status) { - case "receiving": - // TODO: handle the error. - reject(new Error()); - break; - case "sending": { - /** - * @type {[T, PromiseResolve]} - */ - const buffer = this._buffer; - this._buffer = null; - resolve(buffer[0]); - this.status = "active"; - buffer[1](); - break; - } - case "active": - this.status = "receiving"; - this._buffer = resolve; - break; - } - }); - } - }; + static channel = Channel; /** * **无名杀消息推送库** *