pref: divide channel.

This commit is contained in:
Ansolve 2023-12-30 23:35:29 +08:00
parent 7025291198
commit 519abfacbd
No known key found for this signature in database
GPG Key ID: 2F2873AC9538CC2D
2 changed files with 89 additions and 82 deletions

View File

@ -0,0 +1,87 @@
/**
* @template T
*/
export class Channel {
/**
* @template U
* @typedef {(value?: U | PromiseLike<U>) => void} PromiseResolve
*/
constructor() {
/**
* @type {"active" | "receiving" | "sending"}
*/
this.status = "active";
/**
* @type {PromiseResolve<T> | [T, PromiseResolve<void>] | null}
*/
this._buffer = null;
}
/**
* 向该频道发送消息在消息未被接受前将等待
*
* @param {T} value - 要发送的消息
* @returns {Promise<void>}
*/
send(value) {
return new Promise((resolve, reject) => {
switch (this.status) {
case "sending":
// TODO: handle the error.
reject(new Error());
break;
case "receiving": {
/**
* @type {PromiseResolve<T>}
*/
// @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<T>} 接收到的消息
*/
receive() {
return new Promise((resolve, reject) => {
switch (this.status) {
case "receiving":
// TODO: handle the error.
reject(new Error());
break;
case "sending": {
/**
* @type {[T, PromiseResolve<void>]}
*/
// @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;
}
});
}
}

View File

@ -20,6 +20,7 @@ import { GNC as gnc } from '../gnc/index.js';
import { LibInit } from "./init/index.js"; import { LibInit } from "./init/index.js";
import { Announce } from "./announce/index.js"; import { Announce } from "./announce/index.js";
import { Channel } from "./channel/index.js";
import * as Element from "./element/index.js"; import * as Element from "./element/index.js";
@ -320,88 +321,7 @@ export class Library extends Uninstantable {
* // 从某个角落向channel发消息若无消息接收则等待 * // 从某个角落向channel发消息若无消息接收则等待
* await channel.send(item); * await channel.send(item);
*/ */
static channel = class { static channel = Channel;
/**
* @template TValue
* @callback PromiseResolve
* @param {TValue} value
* @returns {void}
*/
constructor() {
/**
* @type {"active" | "receiving" | "sending"}
*/
this.status = "active";
/**
* @type {PromiseResolve<T> | [T, PromiseResolve<void>] | null}
*/
this._buffer = null;
}
/**
* 向该频道发送消息在消息未被接受前将等待
*
* @param {T} value - 要发送的消息
* @returns {Promise<void>}
*/
send(value) {
return new Promise((resolve, reject) => {
switch (this.status) {
case "sending":
// TODO: handle the error.
reject(new Error());
break;
case "receiving": {
/**
* @type {PromiseResolve<T>}
*/
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<T>} 接收到的消息
*/
receive() {
return new Promise((resolve, reject) => {
switch (this.status) {
case "receiving":
// TODO: handle the error.
reject(new Error());
break;
case "sending": {
/**
* @type {[T, PromiseResolve<void>]}
*/
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;
}
});
}
};
/** /**
* **无名杀消息推送库** * **无名杀消息推送库**
* *