UNPKG

js-uploader

Version:
331 lines 15.1 kB
import { __awaiter, __generator } from "tslib"; import { from } from 'rxjs'; import { concatMap, publishReplay } from 'rxjs/operators'; var Constants; (function (Constants) { Constants["Key"] = "key"; Constants["Value"] = "value"; Constants["Readonly"] = "readonly"; Constants["Readwrite"] = "readwrite"; Constants["DefaultStoreName"] = "key-value"; })(Constants || (Constants = {})); var IDB = /** @class */ (function () { function IDB(dbName, tableName) { if (tableName === void 0) { tableName = Constants.DefaultStoreName; } this.dbName = dbName; this.tableName = tableName; if (!this.dbName || typeof dbName !== 'string' || !this.tableName || typeof tableName !== 'string') { throw new Error(); } this.conn$ = from(this.initConn()).pipe(publishReplay(1)); this.conn$.connect(); } IDB.createInstance = function (dbName, tableName) { return new IDB(dbName, tableName); }; IDB.prototype.initConn = function () { return __awaiter(this, void 0, void 0, function () { var loop, maxRetryTimes, db, error_1; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: loop = function () { return new Promise(function (resolve, reject) { var request = indexedDB.open(_this.dbName, 1); request.onsuccess = function (e) { var db = e.target.result; if (!db.objectStoreNames.contains(_this.tableName)) { console.warn('no such store', _this.tableName, db); } resolve(db); }; request.onupgradeneeded = function (e) { var db = e.target.result; if (!db.objectStoreNames.contains(_this.tableName)) { db.createObjectStore(_this.tableName, { autoIncrement: true }); } db.close(); resolve(null); }; request.onerror = function () { reject(new Error()); }; }); }; maxRetryTimes = 10; db = null; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); maxRetryTimes--; return [4 /*yield*/, loop()]; case 2: db = _a.sent(); return [3 /*break*/, 4]; case 3: error_1 = _a.sent(); db = null; console.error(error_1); return [3 /*break*/, 4]; case 4: if (db === null && maxRetryTimes > 0) return [3 /*break*/, 1]; _a.label = 5; case 5: return [2 /*return*/, db]; } }); }); }; IDB.prototype.setItem = function (key, value) { var _this = this; var setItem = function (db) { return new Promise(function (resolve, reject) { var table = db.transaction(_this.tableName, Constants.Readwrite).objectStore(_this.tableName); var request = table.put(value, String(key)); request.onsuccess = function () { return resolve(value); }; request.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(setItem)); }; IDB.prototype.getItem = function (key) { var _this = this; var getItem = function (db) { return new Promise(function (resolve, reject) { var table = db.transaction(_this.tableName, Constants.Readonly).objectStore(_this.tableName); var request = table.get(String(key)); request.onsuccess = function () { return resolve(request.result); }; request.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(getItem)); }; IDB.prototype.list = function () { var _this = this; var list = function (db) { return new Promise(function (resolve, reject) { var transaction = db.transaction(_this.tableName, Constants.Readonly); var request = transaction.objectStore(_this.tableName).openCursor(); var record = {}; request.onsuccess = function (_e) { var cursor = request.result; if (cursor) { var key = cursor.key, value = cursor.value; record[key] = value; cursor.continue(); } else { resolve(record); } }; request.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(list)); }; IDB.prototype.removeItem = function (key) { var _this = this; var removeItem = function (db) { return new Promise(function (resolve, reject) { var table = db.transaction(_this.tableName, Constants.Readwrite).objectStore(_this.tableName); var request = table.delete(String(key)); request.onsuccess = function () { return resolve(); }; request.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(removeItem)); }; IDB.prototype.clear = function () { var _this = this; var clear = function (db) { return new Promise(function (resolve, reject) { var table = db.transaction(_this.tableName, Constants.Readwrite).objectStore(_this.tableName); var request = table.clear(); request.onsuccess = function () { return resolve(); }; request.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(clear)); }; IDB.prototype.size = function () { var _this = this; var size = function (db) { return new Promise(function (resolve, reject) { var table = db.transaction(_this.tableName, Constants.Readwrite).objectStore(_this.tableName); var request = table.count(); request.onsuccess = function () { return resolve(request.result || 0); }; request.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(size)); }; IDB.prototype.keys = function () { var _this = this; var keys = function (db) { return new Promise(function (resolve, reject) { var table = db.transaction(_this.tableName, Constants.Readwrite).objectStore(_this.tableName); var request = table.getAllKeys(); request.onsuccess = function () { return resolve(request.result); }; request.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(keys)); }; IDB.prototype.values = function () { var _this = this; var values = function (db) { return new Promise(function (resolve, reject) { var table = db.transaction(_this.tableName, Constants.Readwrite).objectStore(_this.tableName); var request = table.getAll(); request.onsuccess = function () { return resolve(request.result); }; request.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(values)); }; IDB.prototype.getItems = function (keys) { var _this = this; var getItems = function (db) { return new Promise(function (resolve, reject) { var record = {}; if (!(keys === null || keys === void 0 ? void 0 : keys.length)) { resolve(record); return; } keys = keys.slice().sort(function (a, b) { return (a < b ? -1 : a > b ? 1 : 0); }); var table = db.transaction(_this.tableName, Constants.Readwrite).objectStore(_this.tableName); var keyRangeValue = IDBKeyRange.bound(keys[0], keys[keys.length - 1], false, false); var request = table.openCursor(keyRangeValue); request.onsuccess = function () { var cursor = request.result; if (!cursor) { resolve(record); return; } var key = cursor.key; var i = 0; while (key > keys[i]) { i++; if (i === keys.length) { resolve(record); return; } } if (key === keys[i]) { var value = cursor.value; record[key] = value; cursor.continue(); } else { cursor.continue(keys[i]); } }; request.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(getItems)); }; IDB.prototype.setItems = function (items) { var _this = this; var setItems = function (db) { return new Promise(function (resolve, reject) { var table = db.transaction(_this.tableName, Constants.Readwrite).objectStore(_this.tableName); var promises = []; if (Array.isArray(items)) { promises = items.map(function (item) { return new Promise(function (resolve, reject) { var req = table.put(item[Constants.Value], String(item[Constants.Key])); req.onsuccess = function () { return resolve(); }; req.onerror = function () { return reject(new Error()); }; }); }); } else { promises = Object.keys(items).map(function (key) { var value = items[key]; return new Promise(function (resolve, reject) { var req = table.put(value, String(key)); req.onsuccess = function () { return resolve(); }; req.onerror = function () { return reject(new Error()); }; }); }); } Promise.all(promises) .then(function () { return resolve(); }) .catch(function (e) { return reject(e); }); }); }; return this.conn$.pipe(concatMap(setItems)); }; IDB.prototype.removeItems = function (keys) { var _this = this; var removeItems = function (db) { return new Promise(function (resolve, reject) { if (!(keys === null || keys === void 0 ? void 0 : keys.length)) { resolve(); return; } var table = db.transaction(_this.tableName, Constants.Readwrite).objectStore(_this.tableName); var requests = keys.map(function (key) { return new Promise(function (resolve, reject) { var req = table.delete(key); req.onerror = function () { return reject(new Error()); }; req.onsuccess = function () { return resolve(); }; }); }); Promise.all(requests) .then(function () { return resolve(); }) .catch(function (e) { return reject(e); }); }); }; return this.conn$.pipe(concatMap(removeItems)); }; IDB.prototype.getItemsWhenKeyStartsWith = function (prefix) { var _this = this; var getItemsWhenKeyStartsWith = function (db) { return new Promise(function (resolve, reject) { var record = {}; if (!prefix) { resolve(record); return; } var table = db.transaction(_this.tableName, Constants.Readonly).objectStore(_this.tableName); var keyRangeValue = IDBKeyRange.bound(String(prefix), prefix + 'uffff', false, false); var req = table.openCursor(keyRangeValue); req.onsuccess = function () { var cursor = req.result; if (cursor) { var key = cursor.key, value = cursor.value; record[key] = value; cursor.continue(); } else { resolve(record); } }; req.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(getItemsWhenKeyStartsWith)); }; IDB.prototype.getKeysWhenKeyStartsWith = function (prefix) { var _this = this; var getValuesWhenKeyStartsWith = function (db) { return new Promise(function (resolve, reject) { if (!prefix) { resolve([]); return; } var table = db.transaction(_this.tableName, Constants.Readonly).objectStore(_this.tableName); var keyRangeValue = IDBKeyRange.bound(String(prefix), prefix + 'uffff', false, false); var req = table.getAllKeys(keyRangeValue); req.onsuccess = function () { return resolve(req.result); }; req.onerror = function () { return reject(new Error()); }; }); }; return this.conn$.pipe(concatMap(getValuesWhenKeyStartsWith)); }; return IDB; }()); export { IDB }; //# sourceMappingURL=IDB.js.map