UNPKG

ajsfw

Version:
296 lines (295 loc) 13.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var logger = require("ajsfw/dbg/logger"); var utils = require("ajsfw/utils"); var di = require("ajsfw/di"); var exceptions = require("./exceptions"); var logmsg = require("./Strings_Log"); "use strict"; var AjsIndexedDb = (function (_super) { __extends(AjsIndexedDb, _super); function AjsIndexedDb(dbName) { var _this = _super.call(this) || this; _this.__dbName = dbName; _this.__db = null; return _this; } Object.defineProperty(AjsIndexedDb.prototype, "isOldIDbImplementation", { get: function () { if (this.__db && this.__db !== null) { return typeof this.__db.setVersion === "function"; } else { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, this, logmsg.LOG_NOT_INITIALIZED); throw new exceptions.IndexedDbNotInitializedException(); } }, enumerable: true, configurable: true }); AjsIndexedDb.prototype._onInitializeAsync = function () { return __awaiter(this, void 0, void 0, function () { var e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!window.indexedDB) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, this, logmsg.LOG_NOT_SUPPORTED); throw new exceptions.IndexedDBNotSupportedException(); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4, this.__openDb()]; case 2: _a.sent(); return [3, 4]; case 3: e_1 = _a.sent(); throw new exceptions.InitializationFailedException(e_1); case 4: return [2]; } }); }); }; AjsIndexedDb.prototype.doStoreRequest = function (name, mode, requestCb) { var _this = this; if (mode === void 0) { mode = "readwrite"; } return new Promise(function (resolve, reject) { _this.__doStoreRequest(name, mode, requestCb, resolve, reject); }); }; AjsIndexedDb.prototype.__doStoreRequest = function (name, mode, requestCb, resolve, reject) { var _this = this; if (mode === void 0) { mode = "readwrite"; } logger.log(logger.LogType.Info, 0, logmsg.LOG_IDB, this, logmsg.LOG_GETTING_STORE + name + ", ", mode); if (!this.initialized || this.__db === null) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, this, logmsg.LOG_NOT_INITIALIZED); reject(new exceptions.IndexedDbNotInitializedException()); return; } if (!this.__db.objectStoreNames.contains(name)) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, this, logmsg.LOG_STORE_NOT_EXIST + name); reject(new exceptions.StoreNotExitsException()); return; } try { if (typeof this.__db.setVersion === "function") { mode = (mode === "readwrite" ? 0 : 1); } var tx = this.__db.transaction(name, mode); tx.onerror = function (e) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, _this, logmsg.LOG_FAILED_TO_PERFORM_STORE_REQ, e); reject(new exceptions.IndexedDbStoreRequestFailedException()); }; var dbr_1 = requestCb(tx.objectStore(name)); dbr_1.onsuccess = function (e) { resolve(dbr_1.result); }; dbr_1.onerror = function (e) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, _this, logmsg.LOG_FAILED_TO_PERFORM_STORE_REQ, e); reject(new exceptions.IndexedDbStoreRequestFailedException()); }; } catch (e) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, this, logmsg.LOG_FAILED_TO_PERFORM_STORE_REQ, e); reject(new exceptions.IndexedDbStoreRequestFailedException(e)); } }; AjsIndexedDb.prototype.countItemsUsingCursor = function (storeName) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2, new Promise(function (resolve, reject) { _this.__countItemsUsingCursor(storeName, resolve, reject); })]; }); }); }; AjsIndexedDb.prototype.__countItemsUsingCursor = function (storeName, resolve, reject) { var _this = this; if (!this.initialized || this.__db === null) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, this, logmsg.LOG_NOT_INITIALIZED); reject(new exceptions.IndexedDbNotInitializedException()); return; } if (!this.__db.objectStoreNames.contains(storeName)) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, this, logmsg.LOG_STORE_NOT_EXIST + storeName); reject(new exceptions.StoreNotExitsException()); return; } var count = 0; try { var tx_1 = this.__db.transaction(storeName, "readonly"); tx_1.onerror = function (e) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, _this, logmsg.LOG_FAILED_TO_COUNT_ITEMS_USING_CURSOR, e); reject(new exceptions.IndexedDbStoreRequestFailedException(tx_1.error.toString())); }; var dbr_2 = tx_1.objectStore(storeName).openCursor(); dbr_2.onsuccess = function (e) { if (!e.target.result) { resolve(count); return; } count++; e.target.result.continue(); }; dbr_2.onerror = function (e) { console.error("Failed to open cursor! " + dbr_2.error); logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, _this, logmsg.LOG_FAILED_TO_COUNT_ITEMS_USING_CURSOR, e); reject(new exceptions.IndexedDbStoreRequestFailedException(dbr_2.error.toString())); }; } catch (e) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, this, logmsg.LOG_FAILED_TO_COUNT_ITEMS_USING_CURSOR, e); reject(new exceptions.IndexedDbStoreRequestFailedException(e)); } }; AjsIndexedDb.prototype.createStore = function (name, params, configureStore) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2, new Promise(function (resolve, reject) { _this.__createStore(name, params, configureStore, resolve, reject); })]; }); }); }; AjsIndexedDb.prototype.__createStore = function (name, params, configureStore, resolve, reject) { return __awaiter(this, void 0, void 0, function () { var _this = this; var version; return __generator(this, function (_a) { switch (_a.label) { case 0: logger.log(logger.LogType.Info, 0, logmsg.LOG_IDB, this, logmsg.LOG_CREATING_STORE + name); if (this.__db.objectStoreNames.contains(name)) { resolve(); return [2]; } version = this.__db.version; if (version === "") { version = 1; } version++; this.__db.close(); this.__db = null; return [4, utils.nextTickAsync()]; case 1: _a.sent(); logger.log(logger.LogType.Info, 0, logmsg.LOG_IDB, this, logmsg.LOG_NEW_DB_VER + version); return [4, this.__openDb(version, function () { _this.__dbUpgradeCreateStore(name, params, configureStore); })]; case 2: _a.sent(); this.__db.close(); this.__db = null; return [4, utils.nextTickAsync()]; case 3: _a.sent(); return [4, this.__openDb()]; case 4: _a.sent(); resolve(); return [2]; } }); }); }; AjsIndexedDb.prototype.__dbUpgradeCreateStore = function (name, params, configureStore) { var store = this.__db.createObjectStore(name, params); if (configureStore) { logger.log(logger.LogType.Info, 0, logmsg.LOG_IDB, this, logmsg.LOG_CONF_STORE); configureStore(store); logger.log(logger.LogType.Info, 0, logmsg.LOG_IDB, this, logmsg.LOG_CONF_STORE_DONE); } }; AjsIndexedDb.prototype.__openDb = function (version, upgradeCb) { var _this = this; return new Promise(function (resolve, reject) { _this.__openDbExecutor(version, upgradeCb, resolve, reject); }); }; AjsIndexedDb.prototype.__openDbExecutor = function (version, upgradeCb, resolve, reject) { var _this = this; try { logger.log(logger.LogType.Enter, 0, logmsg.LOG_IDB, this); var dbOpen = void 0; if (version) { dbOpen = window.indexedDB.open(this.__dbName, version); } else { dbOpen = window.indexedDB.open(this.__dbName); } dbOpen.onerror = function (e) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, _this, logmsg.LOG_FAILED_OPEN_DB, e); reject(new exceptions.IndexedDBFailedToOpenException()); }; dbOpen.onsuccess = function (e) { _this.__dbOpenSuccess(e, upgradeCb, version, resolve, reject); }; dbOpen.onupgradeneeded = function (e) { _this.__upgradeDb(e, upgradeCb, resolve); }; } catch (e) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, this, logmsg.LOG_FAILED_OPEN_DB, e); reject(new exceptions.IndexedDBFailedToOpenException(e)); } }; AjsIndexedDb.prototype.__upgradeDb = function (e, upgradeCb, resolve) { this.__db = e.target.result; e.target.onsuccess = null; e.target.onupgradeneeded = null; e.target.onerror = null; logger.log(logger.LogType.Info, 0, logmsg.LOG_IDB, this, logmsg.LOG_UPGRADING_DB + this.__db.version); if (upgradeCb) { upgradeCb(); } resolve(); }; AjsIndexedDb.prototype.__dbOpenSuccess = function (e, upgradeCb, newVersion, resolve, reject) { var _this = this; this.__db = e.target.result; e.target.onsuccess = null; e.target.onupgradeneeded = null; e.target.onerror = null; if (typeof this.__db.setVersion === "function") { logger.log(logger.LogType.Warning, 0, logmsg.LOG_IDB, this, logmsg.LOG_OLD_INDEXEDDB_IMPLEMENTATION); if (newVersion) { var oldVersion = this.__db.version; if (oldVersion === "") { oldVersion = 1; } if (newVersion > oldVersion && upgradeCb !== undefined) { logger.log(logger.LogType.Info, 0, logmsg.LOG_IDB, this, logmsg.LOG_UPGRADING_VERSION + newVersion); var vch = this.__db.setVersion(newVersion); vch.onsuccess = function (e) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: logger.log(logger.LogType.Info, 0, logmsg.LOG_IDB, this, logmsg.LOG_VERSION_UPGRADED); return [4, upgradeCb()]; case 1: _a.sent(); resolve(); return [2]; } }); }); }; vch.onerror = function (e) { logger.log(logger.LogType.Error, 0, logmsg.LOG_IDB, _this, logmsg.LOG_UPGRADE_FAILED); reject(new exceptions.IndexedDbOldFailedToSetVersionException()); }; return; } } logger.log(logger.LogType.Exit, 0, logmsg.LOG_IDB, this); resolve(); return; } logger.log(logger.LogType.Info, 0, logmsg.LOG_IDB, this, logmsg.LOG_DB_OPENED + this.__db.version); resolve(); }; return AjsIndexedDb; }(di.ServiceAsyncInit)); exports.AjsIndexedDb = AjsIndexedDb;