ts-api-core
Version:
Nodejs api framework core
85 lines • 3.46 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UtilsDB = void 0;
/** 数据信息, 用以缓存或持久化数据对象 */
class UtilsDB {
constructor(classType, className) {
/** 授权数据 */
this.accessData = {};
if (!classType) {
if (className) {
this.className = className;
this.storeKey = "data_" + className;
const data = this.getItem(className, this.storeKey);
if (data) {
try {
const items = JSON.parse(data);
if (items) {
Object.keys(items).forEach((key) => {
if (key === 'undefined') {
return;
}
const v = items[key];
if (!v) {
return;
}
this.accessData[key] = v;
});
}
}
catch (e) { }
}
}
return;
}
let _instance = UtilsDB._instance.get(classType);
if (!_instance) {
_instance = new UtilsDB(undefined, classType.name);
UtilsDB._instance.set(classType, _instance);
}
return _instance;
}
/**
* 注册数据储器类
* @param classTypeName
* @param storage
*/
registerDataStorage(classTypeName, storage) {
UtilsDB._dataStorage[classTypeName] = storage;
}
getItem(className, key) {
var _a, _b;
return (_b = (_a = UtilsDB._dataStorage[className]) === null || _a === void 0 ? void 0 : _a.getItem(key)) !== null && _b !== void 0 ? _b : '';
}
setItem(className, key, value) {
var _a;
(_a = UtilsDB._dataStorage[className]) === null || _a === void 0 ? void 0 : _a.setItem(key, value);
}
get(key) {
return __awaiter(this, void 0, void 0, function* () {
return this.accessData[key];
});
}
set(key, data) {
return __awaiter(this, void 0, void 0, function* () {
this.accessData[key] = data;
if (this.storeKey && UtilsDB._dataStorage[this.className]) {
this.setItem(this.className, this.storeKey, JSON.stringify(this.accessData));
}
return true;
});
}
}
exports.UtilsDB = UtilsDB;
UtilsDB._instance = new Map();
UtilsDB._dataStorage = {};
//# sourceMappingURL=utils.db.js.map