@browser-storage/indexeddb-driver
Version:
IndexedDb driver for BrowserStorage
188 lines (141 loc) • 4.23 kB
JavaScript
import { Defer } from '@browser-storage/core';
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _asyncToGenerator(fn) {
return function () {
var self = this,
args = arguments;
return new Promise(function (resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
class IndexeddbDriver {
constructor() {
this._ready = new Defer();
}
get isSupported() {
return typeof window.indexedDB !== 'undefined';
}
clear() {
const objectStore = this._getObjectStore('readwrite');
return this._getRequestResult(objectStore.clear());
} // tslint:disable-next-line
destroy() {
return _asyncToGenerator(function* () {})();
}
getItem(key) {
var _this = this;
return _asyncToGenerator(function* () {
const objectStore = _this._getObjectStore('readonly');
return _this._getRequestResult(objectStore.get(key));
})();
}
hasItem(key) {
var _this2 = this;
return _asyncToGenerator(function* () {
const store = _this2._getObjectStore('readonly');
return (yield _this2._getRequestResult(store.count(key))) > 0;
})();
}
init(dbOptions) {
var _this3 = this;
return _asyncToGenerator(function* () {
_this3._options = dbOptions;
_this3._db = yield new Promise((resolve, reject) => {
const openRequest = window.indexedDB.open(_this3._options.name, _this3._options.version);
openRequest.onerror = reject;
openRequest.onsuccess = () => resolve(openRequest.result);
openRequest.onupgradeneeded = e => {
openRequest.result.createObjectStore(_this3._options.storeName);
};
});
_this3._ready.resolve(true);
return undefined;
})();
}
iterate(iterator) {
var _this4 = this;
return _asyncToGenerator(function* () {
const keys = yield _this4.keys();
return keys.reduceRight(
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(function* (prev, key, index) {
yield prev;
const value = yield _this4.getItem(key);
yield iterator(key, value, index);
});
return function (_x, _x2, _x3) {
return _ref.apply(this, arguments);
};
}(), Promise.resolve());
})();
}
key(index) {
var _this5 = this;
return _asyncToGenerator(function* () {
return (yield _this5.keys())[index];
})();
}
keys() {
var _this6 = this;
return _asyncToGenerator(function* () {
const objectStore = _this6._getObjectStore('readonly');
return _this6._getRequestResult(objectStore.getAllKeys());
})();
}
length() {
var _this7 = this;
return _asyncToGenerator(function* () {
const objectStore = _this7._getObjectStore('readwrite');
return _this7._getRequestResult(objectStore.count());
})();
}
ready() {
return this._ready.promise;
}
removeItem(key) {
var _this8 = this;
return _asyncToGenerator(function* () {
yield _this8._getRequestResult(_this8._getObjectStore('readwrite').delete(key));
})();
}
setItem(key, item) {
var _this9 = this;
return _asyncToGenerator(function* () {
const objectStore = _this9._getObjectStore('readwrite');
yield _this9._getRequestResult(objectStore.put(item, key));
return item;
})();
}
_getObjectStore(mode) {
return this._db.transaction([this._options.storeName], mode).objectStore(this._options.storeName);
}
_getRequestResult(request) {
return new Promise((resolve, reject) => {
request.onerror = reject;
request.onsuccess = () => resolve(request.result);
});
}
}
export { IndexeddbDriver };