"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Application = void 0; var tslib_1 = require("tslib"); var Feature_1 = require("oak-frontend-base/lib/types/Feature"); var concurrent_1 = require("oak-domain/lib/utils/concurrent"); var assert_1 = require("oak-domain/lib/utils/assert"); var projection = { id: 1, name: 1, config: 1, type: 1, systemId: 1, system: { id: 1, name: 1, config: 1, } }; var Application = /** @class */ (function (_super) { tslib_1.__extends(Application, _super); function Application(aspectWrapper, type, cache, storage, callback) { var _this = _super.call(this, aspectWrapper) || this; _this.rwLock = new concurrent_1.RWLock(); _this.cache = cache; _this.storage = storage; var applicationId = storage.load('application:applicationId'); _this.rwLock.acquire('X'); if (applicationId) { _this.applicationId = applicationId; _this.getApplicationFromCache(callback); } else { _this.refresh(type, callback); } _this.rwLock.release(); return _this; } Application.prototype.getApplicationFromCache = function (callback) { return tslib_1.__awaiter(this, void 0, void 0, function () { var data; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.cache.refresh('application', { data: projection, filter: { id: this.applicationId, } })]; case 1: data = (_a.sent()).data; (0, assert_1.assert)(data.length === 1); this.application = data[0]; callback(this.application); return [2 /*return*/]; } }); }); }; Application.prototype.refresh = function (type, callback) { return tslib_1.__awaiter(this, void 0, void 0, function () { var applicationId; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getAspectWrapper().exec('getApplication', { type: type, })]; case 1: applicationId = (_a.sent()).result; this.applicationId = applicationId; this.storage.save('application:applicationId', applicationId); this.getApplicationFromCache(callback); return [2 /*return*/]; } }); }); }; Application.prototype.getApplication = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var result; return tslib_1.__generator(this, function (_a) { this.rwLock.acquire('S'); result = this.application; this.rwLock.release(); return [2 /*return*/, result]; }); }); }; Application.prototype.getApplicationId = function () { this.rwLock.acquire('S'); var result = this.applicationId; this.rwLock.release(); return result; }; return Application; }(Feature_1.Feature)); exports.Application = Application;