UNPKG

ajsfw

Version:
453 lines (452 loc) 23.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var logger = require("ajsfw/dbg/logger"); var exceptions = require("./exceptions"); var logmsg = require("./Strings_Log.Debug"); var consts = require("./constants"); var enums_1 = require("../enums"); var AjsStorage = (function () { function AjsStorage(cacheSize) { logger.log(logger.LogType.Constructor, 0, logmsg.LOG_AJSRESSTOR, this); this._maxCacheSize = cacheSize; logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); } Object.defineProperty(AjsStorage.prototype, "supported", { get: function () { return this._supported; }, enumerable: true, configurable: true }); Object.defineProperty(AjsStorage.prototype, "cacheSize", { get: function () { return this._maxCacheSize; }, enumerable: true, configurable: true }); ; Object.defineProperty(AjsStorage.prototype, "usedSpace", { get: function () { return this._usedSpace; }, enumerable: true, configurable: true }); Object.defineProperty(AjsStorage.prototype, "resources", { get: function () { return this._resources; }, enumerable: true, configurable: true }); Object.defineProperty(AjsStorage.prototype, "storageProvider", { get: function () { return this._storageProvider; }, enumerable: true, configurable: true }); AjsStorage.prototype.clear = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: logger.log(logger.LogType.Enter, 0, logmsg.LOG_AJSRESSTOR, this); logger.log(logger.LogType.Info, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_CLEARING_STORAGE); return [4, this._storageProvider.clear()]; case 1: _a.sent(); this._usedSpace = 0; this._resources = []; this._storageProvider.setItem(consts.STORAGE_INFO_KEY, JSON.stringify(this._resources)); logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); return [2]; } }); }); }; AjsStorage.prototype.addResource = function (resource) { return __awaiter(this, void 0, void 0, function () { var data, dataSize, str, i, oldInfoSize, e_1, e_2, resourceInfo, resourcesInfoStr, newInfoSize, e_3; return __generator(this, function (_a) { switch (_a.label) { case 0: logger.log(logger.LogType.Enter, 0, logmsg.LOG_AJSRESSTOR, this); logger.log(logger.LogType.Info, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_ADDING_RESOSURCE + resource.url, resource); return [4, this.getResource(resource.url)]; case 1: if (!((_a.sent()) !== null)) return [3, 3]; return [4, this.updateResource(resource)]; case 2: _a.sent(); logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); return [2]; case 3: if (resource.data instanceof Uint8Array) { str = ""; for (i = 0; i < resource.data.length; i++) { str += String.fromCharCode(resource.data[i]); } data = JSON.stringify(btoa(str)); } else { data = JSON.stringify(resource.data); } return [4, this._storageProvider.getItem(consts.STORAGE_INFO_KEY)]; case 4: oldInfoSize = (_a.sent()).length; dataSize = data.length; _a.label = 5; case 5: _a.trys.push([5, 7, , 13]); return [4, this._storageProvider.setItem(consts.STORAGE_RESOURCE_KEY_PREFIX + resource.url, data)]; case 6: _a.sent(); return [3, 13]; case 7: e_1 = _a.sent(); return [4, this.__cleanCache(dataSize)]; case 8: _a.sent(); _a.label = 9; case 9: _a.trys.push([9, 11, , 12]); return [4, this._storageProvider.setItem(consts.STORAGE_RESOURCE_KEY_PREFIX + resource.url, data)]; case 10: _a.sent(); return [3, 12]; case 11: e_2 = _a.sent(); logger.log(logger.LogType.Error, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_NOT_ENOUGH_SPACE, e_2); throw new exceptions.NotEnoughSpaceInStorageException(e_2); case 12: return [3, 13]; case 13: resourceInfo = { url: resource.url, data: null, cachePolicy: resource.cachePolicy, lastModified: resource.lastModified, lastUsedTimestamp: new Date() }; this._resources.push(resourceInfo); resourcesInfoStr = JSON.stringify(this._resources); newInfoSize = resourcesInfoStr.length; _a.label = 14; case 14: _a.trys.push([14, 16, , 18]); return [4, this._storageProvider.setItem(consts.STORAGE_INFO_KEY, resourcesInfoStr)]; case 15: _a.sent(); return [3, 18]; case 16: e_3 = _a.sent(); return [4, this._storageProvider.removeItem(consts.STORAGE_RESOURCE_KEY_PREFIX + resource.url)]; case 17: _a.sent(); logger.log(logger.LogType.Error, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_NOT_ENOUGH_SPACE_META, e_3); throw new exceptions.NotEnoughSpaceInStorageException(e_3); case 18: this._usedSpace += (newInfoSize - oldInfoSize) + dataSize; logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); return [2]; } }); }); }; AjsStorage.prototype.getResource = function (url) { return __awaiter(this, void 0, void 0, function () { var i, info, dataStr, _a, _b, data, resource; return __generator(this, function (_c) { switch (_c.label) { case 0: logger.log(logger.LogType.Enter, 0, logmsg.LOG_AJSRESSTOR, this); logger.log(logger.LogType.Info, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_GETTING_RESOURCE + url); i = 0; _c.label = 1; case 1: if (!(i < this._resources.length)) return [3, 5]; if (!(this._resources[i].url === url)) return [3, 4]; this._resources[i].lastUsedTimestamp = new Date(); info = JSON.stringify(this._resources); return [4, this._storageProvider.setItem(consts.STORAGE_INFO_KEY, info)]; case 2: _c.sent(); _b = (_a = JSON).parse; return [4, this._storageProvider.getItem(consts.STORAGE_RESOURCE_KEY_PREFIX + url)]; case 3: dataStr = _b.apply(_a, [_c.sent()]); data = dataStr; resource = { url: this._resources[i].url, data: data, cachePolicy: this._resources[i].cachePolicy, lastModified: this._resources[i].lastModified, lastUsedTimestamp: this._resources[i].lastUsedTimestamp, size: dataStr.length }; logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); return [2, resource]; case 4: i++; return [3, 1]; case 5: logger.log(logger.LogType.Info, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_RES_NOT_FOUND + url); logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); return [2, null]; } }); }); }; AjsStorage.prototype.updateResource = function (resource) { return __awaiter(this, void 0, void 0, function () { var data, dataSize, str, i, oldInfoSize, resourceKey, oldItem, oldDataSize, e_4, e_5, resourceInfo, resourcesInfoStr, newInfoSize, e_6; return __generator(this, function (_a) { switch (_a.label) { case 0: logger.log(logger.LogType.Enter, 0, logmsg.LOG_AJSRESSTOR, this); logger.log(logger.LogType.Info, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_UPDATE_RESOURCE + resource.url, resource); return [4, this.getResource(resource.url)]; case 1: if ((_a.sent()) === null) { logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); return [2, this.addResource(resource)]; } if (resource.data instanceof Uint8Array) { str = ""; for (i = 0; i < resource.data.length; i++) { str += String.fromCharCode(resource.data[i]); } data = JSON.stringify(btoa(str)); } else { data = JSON.stringify(resource.data); } dataSize = data.length; return [4, this._storageProvider.getItem(consts.STORAGE_INFO_KEY)]; case 2: oldInfoSize = (_a.sent()).length; resourceKey = consts.STORAGE_RESOURCE_KEY_PREFIX + resource.url; return [4, this._storageProvider.getItem(resourceKey)]; case 3: oldItem = _a.sent(); if (oldItem !== null) { oldDataSize = oldItem.length; } else { oldDataSize = 0; } _a.label = 4; case 4: _a.trys.push([4, 6, , 12]); return [4, this._storageProvider.setItem(consts.STORAGE_RESOURCE_KEY_PREFIX + resource.url, data)]; case 5: _a.sent(); return [3, 12]; case 6: e_4 = _a.sent(); return [4, this.__cleanCache(Math.abs(dataSize - oldDataSize))]; case 7: _a.sent(); _a.label = 8; case 8: _a.trys.push([8, 10, , 11]); return [4, this._storageProvider.setItem(consts.STORAGE_RESOURCE_KEY_PREFIX + resource.url, data)]; case 9: _a.sent(); return [3, 11]; case 10: e_5 = _a.sent(); logger.log(logger.LogType.Error, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_NOT_ENOUGH_SPACE, e_5); throw new exceptions.NotEnoughSpaceInStorageException(); case 11: return [3, 12]; case 12: resourceInfo = { url: resource.url, data: null, cachePolicy: resource.cachePolicy, lastModified: resource.lastModified, lastUsedTimestamp: new Date() }; this._resources[this.__getResourceIndex(resource.url)] = resourceInfo; resourcesInfoStr = JSON.stringify(this._resources); newInfoSize = resourcesInfoStr.length; _a.label = 13; case 13: _a.trys.push([13, 15, , 16]); return [4, this._storageProvider.setItem(consts.STORAGE_INFO_KEY, resourcesInfoStr)]; case 14: _a.sent(); return [3, 16]; case 15: e_6 = _a.sent(); logger.log(logger.LogType.Error, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_NOT_ENOUGH_SPACE_META, e_6); throw new exceptions.NotEnoughSpaceInStorageException(); case 16: this._usedSpace += (newInfoSize - oldInfoSize) + (dataSize - oldDataSize); logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); return [2]; } }); }); }; AjsStorage.prototype.removeResource = function (url) { return __awaiter(this, void 0, void 0, function () { var resource, oldInfoSize, info, newInfoSize; return __generator(this, function (_a) { switch (_a.label) { case 0: logger.log(logger.LogType.Enter, 0, logmsg.LOG_AJSRESSTOR, this); logger.log(logger.LogType.Info, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_REMOVING_RESOURCE + url); return [4, this.getResource(url)]; case 1: resource = _a.sent(); if (resource === null) { return [2]; } return [4, this._storageProvider.removeItem(consts.STORAGE_RESOURCE_KEY_PREFIX + url)]; case 2: _a.sent(); this._usedSpace -= resource.size; return [4, this._storageProvider.getItem(consts.STORAGE_INFO_KEY)]; case 3: oldInfoSize = (_a.sent()).length; this._resources.splice(this._resources.indexOf(resource), 1); info = JSON.stringify(this._resources); newInfoSize = info.length; return [4, this._storageProvider.setItem(consts.STORAGE_INFO_KEY, info)]; case 4: _a.sent(); this._usedSpace -= oldInfoSize - newInfoSize; logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); return [2]; } }); }); }; AjsStorage.prototype._getResourcesInfo = function () { return __awaiter(this, void 0, void 0, function () { var resources, cachedResourcesInfoStr, i, resourceKey, item; return __generator(this, function (_a) { switch (_a.label) { case 0: logger.log(logger.LogType.Enter, 0, logmsg.LOG_AJSRESSTOR, this); logger.log(logger.LogType.Info, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_GETTING_INFO); resources = []; return [4, this._storageProvider.getItem(consts.STORAGE_INFO_KEY)]; case 1: cachedResourcesInfoStr = _a.sent(); if (!(cachedResourcesInfoStr !== null)) return [3, 6]; this._usedSpace = cachedResourcesInfoStr.length; resources = JSON.parse(cachedResourcesInfoStr, this.__resourceInfoJSONReviver); i = 0; _a.label = 2; case 2: if (!(i < resources.length)) return [3, 5]; resourceKey = consts.STORAGE_RESOURCE_KEY_PREFIX + resources[i].url; return [4, this._storageProvider.getItem(resourceKey)]; case 3: item = _a.sent(); if (item !== null) { this._usedSpace += item.length; } _a.label = 4; case 4: i++; return [3, 2]; case 5: return [3, 8]; case 6: return [4, this._storageProvider.setItem(consts.STORAGE_INFO_KEY, JSON.stringify([]))]; case 7: _a.sent(); _a.label = 8; case 8: logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); return [2, resources]; } }); }); }; AjsStorage.prototype.__cleanCache = function (requiredSpace) { return __awaiter(this, void 0, void 0, function () { var testString, i_1, orderedResources, enoughSpace, i, e_7, i; return __generator(this, function (_a) { switch (_a.label) { case 0: logger.log(logger.LogType.Enter, 0, logmsg.LOG_AJSRESSTOR, this); logger.log(logger.LogType.Info, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_CACHE_CLEAN + (requiredSpace === undefined ? logmsg.LOG_CLEAN_ALL : requiredSpace)); if (!(requiredSpace !== undefined)) return [3, 12]; testString = ""; for (i_1 = 0; i_1 < requiredSpace; i_1++) { testString += " "; } orderedResources = this._resources.slice(0).sort(function (a, b) { return a.lastUsedTimestamp < b.lastUsedTimestamp ? -1 : a.lastUsedTimestamp > b.lastUsedTimestamp ? 1 : 0; }); enoughSpace = true; i = 0; _a.label = 1; case 1: if (!(i < orderedResources.length && !enoughSpace)) return [3, 11]; if (!(orderedResources[i].cachePolicy === enums_1.CachePolicy.LastRecentlyUsed)) return [3, 9]; return [4, this.removeResource(orderedResources[i].url)]; case 2: _a.sent(); _a.label = 3; case 3: _a.trys.push([3, 5, , 6]); enoughSpace = true; return [4, this._storageProvider.setItem(consts.STORAGE_ADDTEST_KEY, testString)]; case 4: _a.sent(); return [3, 6]; case 5: e_7 = _a.sent(); enoughSpace = false; return [3, 6]; case 6: if (!enoughSpace) return [3, 8]; return [4, this._storageProvider.removeItem(consts.STORAGE_ADDTEST_KEY)]; case 7: _a.sent(); _a.label = 8; case 8: return [3, 10]; case 9: i++; _a.label = 10; case 10: return [3, 1]; case 11: if (!enoughSpace) { logger.log(logger.LogType.Error, 0, logmsg.LOG_AJSRESSTOR, this, logmsg.LOG_NOT_ENOUGH_SPACE); throw new exceptions.NotEnoughSpaceInStorageException(); } return [3, 17]; case 12: i = 0; _a.label = 13; case 13: if (!(i < this._resources.length)) return [3, 17]; if (!(this._resources[i].cachePolicy === enums_1.CachePolicy.LastRecentlyUsed)) return [3, 15]; return [4, this.removeResource(this._resources[i].url)]; case 14: _a.sent(); return [3, 16]; case 15: i++; _a.label = 16; case 16: return [3, 13]; case 17: logger.log(logger.LogType.Exit, 0, logmsg.LOG_AJSRESSTOR, this); return [2]; } }); }); }; AjsStorage.prototype.__resourceInfoJSONReviver = function (key, value) { if (key === "lastModified" || key === "lastUsedTimestamp") { return new Date(value); } return value; }; AjsStorage.prototype.__getResourceIndex = function (url) { for (var i = 0; i < this._resources.length; i++) { if (this._resources[i].url === url) { return i; } } return -1; }; return AjsStorage; }()); exports.AjsStorage = AjsStorage;