classy-pay-core
Version:
Shared tools used in ClassyPay-related projects
57 lines • 1.93 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataSourceManager = void 0;
require('source-map-support').install();
const Lock_1 = __importDefault(require("./utils/Lock"));
class DataSourceManager {
constructor(dataSource, config) {
this.lock = new Lock_1.default();
this.initialized = false;
this.querying = false;
this.cache = { values: {}, missing: {} };
if (!dataSource) {
throw new Error('Cannot construct DataSourceManager with null data source');
}
if (!config) {
throw new Error('Cannot construct DataSource with null config');
}
this.dataSource = dataSource;
this.config = config;
}
getQuerying() {
return this.querying;
}
async get(key) {
return await this.lock.lockForPath(async () => {
this.querying = true;
try {
await this._init();
if (!(key in this.cache.values || key in this.cache.missing)) {
const value = await this.dataSource.get(key);
if (value) {
this.cache.values[key] = value;
}
else {
this.cache.missing[key] = true;
}
}
return this.cache.values[key];
}
finally {
this.querying = false;
}
});
}
async _init() {
if (!this.initialized) {
await this.dataSource.initialize(this.config);
this.initialized = true;
}
}
}
exports.DataSourceManager = DataSourceManager;
exports.default = DataSourceManager;
//# sourceMappingURL=DataSourceManager.js.map