初始化提交
This commit is contained in:
71
common/modules/mixly-modules/common/registry.js
Normal file
71
common/modules/mixly-modules/common/registry.js
Normal file
@@ -0,0 +1,71 @@
|
||||
goog.loadJs('common', () => {
|
||||
|
||||
goog.require('Mixly.Debug');
|
||||
goog.provide('Mixly.Registry');
|
||||
|
||||
const { Debug } = Mixly;
|
||||
|
||||
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_;
|
||||
}
|
||||
}
|
||||
|
||||
Mixly.Registry = Registry;
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user