Update: 更新socket工作模式

This commit is contained in:
王立帮
2024-11-29 21:21:58 +08:00
parent 546912edd7
commit d8ceafadbf
9 changed files with 428 additions and 0 deletions

60
src/common/registry.js Normal file
View File

@@ -0,0 +1,60 @@
export default class Registry {
#registry_ = new Map();
constructor() {
this.reset();
}
reset() {
this.#registry_.clear();
}
validate(keys) {
if (!(keys instanceof Array)) {
keys = [keys];
}
return keys;
}
register(keys, value) {
keys = this.validate(keys);
for (let key of keys) {
if (this.#registry_.has(key)) {
Debug.warn(`${key}已存在,不可重复注册`);
continue;
}
this.#registry_.set(key, value);
}
}
unregister(keys) {
keys = this.validate(keys);
for (let key of keys) {
if (!this.#registry_.has(key)) {
Debug.warn(`${key}不存在,无需取消注册`);
continue;
}
this.#registry_.delete(key);
}
}
length() {
return this.#registry_.size;
}
hasKey(key) {
return this.#registry_.has(key);
}
keys() {
return [...this.#registry_.keys()];
}
getItem(key) {
return this.#registry_.get(key) ?? null;
}
getAllItems() {
return this.#registry_;
}
}