From 559e468c2e5ebb2283705c4d82890ef175520439 Mon Sep 17 00:00:00 2001 From: Zarko Hristovski Date: Tue, 20 Aug 2024 10:36:46 +0200 Subject: [PATCH 01/10] wip --- src/index.ts | 19 ++++--------------- src/util.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 src/util.ts diff --git a/src/index.ts b/src/index.ts index 8467fbe..96fde84 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,21 +1,10 @@ -export function promisifyRequest( - request: IDBRequest | IDBTransaction, -): Promise { - return new Promise((resolve, reject) => { - // @ts-ignore - file size hacks - request.oncomplete = request.onsuccess = () => resolve(request.result); - // @ts-ignore - file size hacks - request.onabort = request.onerror = () => reject(request.error); - }); -} +import { openDatabase, promisifyDatabaseRequest } from './util'; -export function createStore(dbName: string, storeName: string): UseStore { - const request = indexedDB.open(dbName); - request.onupgradeneeded = () => request.result.createObjectStore(storeName); - const dbp = promisifyRequest(request); +export const promisifyRequest = promisifyDatabaseRequest; +export function createStore(dbName: string, storeName: string): UseStore { return (txMode, callback) => - dbp.then((db) => + openDatabase(dbName, storeName).then(db => callback(db.transaction(storeName, txMode).objectStore(storeName)), ); } diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 0000000..62af38a --- /dev/null +++ b/src/util.ts @@ -0,0 +1,49 @@ +export function promisifyDatabaseRequest( + request: IDBRequest | IDBTransaction, +): Promise { + return new Promise((resolve, reject) => { + // @ts-ignore - file size hacks + request.oncomplete = request.onsuccess = () => resolve(request.result); + // @ts-ignore - file size hacks + request.onabort = request.onerror = () => reject(request.error); + }); +} + +const qq: {[dbName: string]: {promise?: Promise; resolve: (db: IDBDatabase) => void;}} = {}; + +export async function openDatabase(dbName: string, storeName: string): Promise { + const q = qq[dbName] || {}; + if (!q.promise) { + q.promise = new Promise(resolve => q.resolve = resolve); + qq[dbName] = q; + _openDatabase(dbName, storeName, q.resolve); + } + + const db = await q.promise; + delete q.promise; + return db; +} + +const databases: {[dbName: string]: IDBDatabase} = {}; + +async function _openDatabase( + dbName: string, storeName: string, resolve: (db: IDBDatabase) => void +): Promise { + if (!databases[dbName]) { + const request = indexedDB.open(dbName); + request.onupgradeneeded = () => request.result.createObjectStore(storeName); + resolve(databases[dbName] = await promisifyDatabaseRequest(request)); + return; + } + + try { + databases[dbName].transaction(storeName); + resolve(databases[dbName]); + } catch(err: any) { + console.log(`Could not open an indexedDB transaction due to ${err.name} (${err.message}).`); + console.log('Trying to reopen the connection...'); + // Try re-open. + delete databases[dbName]; + _openDatabase(dbName, storeName, resolve); + } +} \ No newline at end of file From 33226354a3b64eb0e44c8e4ee6c1a5ec5957b07e Mon Sep 17 00:00:00 2001 From: Zarko Hristovski Date: Tue, 20 Aug 2024 10:48:52 +0200 Subject: [PATCH 02/10] wip --- .nvmrc | 2 +- package-lock.json | 13 ------------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/.nvmrc b/.nvmrc index 9a0c3d3..80a9956 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -v14.15.4 +v20.16.0 diff --git a/package-lock.json b/package-lock.json index eeb74c0..5d820fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,9 +8,6 @@ "name": "idb-keyval", "version": "6.2.1", "license": "Apache-2.0", - "dependencies": { - "safari-14-idb-fix": "^3.0.0" - }, "devDependencies": { "@babel/core": "^7.18.5", "@babel/plugin-external-helpers": "^7.17.12", @@ -5128,11 +5125,6 @@ "tslib": "^2.1.0" } }, - "node_modules/safari-14-idb-fix": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/safari-14-idb-fix/-/safari-14-idb-fix-3.0.0.tgz", - "integrity": "sha512-eBNFLob4PMq8JA1dGyFn6G97q3/WzNtFK4RnzT1fnLq+9RyrGknzYiM/9B12MnKAxuj1IXr7UKYtTNtjyKMBog==" - }, "node_modules/safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -9588,11 +9580,6 @@ "tslib": "^2.1.0" } }, - "safari-14-idb-fix": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/safari-14-idb-fix/-/safari-14-idb-fix-3.0.0.tgz", - "integrity": "sha512-eBNFLob4PMq8JA1dGyFn6G97q3/WzNtFK4RnzT1fnLq+9RyrGknzYiM/9B12MnKAxuj1IXr7UKYtTNtjyKMBog==" - }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", From 610c64e91600e4dcb88fdae0f49f248244f3f94b Mon Sep 17 00:00:00 2001 From: Zarko Hristovski Date: Tue, 20 Aug 2024 15:10:33 +0200 Subject: [PATCH 03/10] wip --- src/index.ts | 4 +--- src/util.ts | 37 ++++++++++++++++++++++++++++--------- test/index.ts | 38 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index 96fde84..177092f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,4 @@ -import { openDatabase, promisifyDatabaseRequest } from './util'; - -export const promisifyRequest = promisifyDatabaseRequest; +import { openDatabase, promisifyRequest } from './util'; export function createStore(dbName: string, storeName: string): UseStore { return (txMode, callback) => diff --git a/src/util.ts b/src/util.ts index 62af38a..f9d64ea 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,4 +1,4 @@ -export function promisifyDatabaseRequest( +export function promisifyRequest( request: IDBRequest | IDBTransaction, ): Promise { return new Promise((resolve, reject) => { @@ -11,12 +11,14 @@ export function promisifyDatabaseRequest( const qq: {[dbName: string]: {promise?: Promise; resolve: (db: IDBDatabase) => void;}} = {}; -export async function openDatabase(dbName: string, storeName: string): Promise { +export async function openDatabase( + dbName: string, storeName: string, retry = true +): Promise { const q = qq[dbName] || {}; if (!q.promise) { q.promise = new Promise(resolve => q.resolve = resolve); qq[dbName] = q; - _openDatabase(dbName, storeName, q.resolve); + _openDatabase(dbName, storeName, q.resolve, retry); } const db = await q.promise; @@ -24,26 +26,43 @@ export async function openDatabase(dbName: string, storeName: string): Promise void + dbName: string, storeName: string, resolve: (db: IDBDatabase) => void, retry: boolean ): Promise { if (!databases[dbName]) { const request = indexedDB.open(dbName); request.onupgradeneeded = () => request.result.createObjectStore(storeName); - resolve(databases[dbName] = await promisifyDatabaseRequest(request)); + resolve(databases[dbName] = await promisifyRequest(request)); + return; + } + + if (!retry) { + resolve(databases[dbName]); return; } try { databases[dbName].transaction(storeName); resolve(databases[dbName]); - } catch(err: any) { - console.log(`Could not open an indexedDB transaction due to ${err.name} (${err.message}).`); - console.log('Trying to reopen the connection...'); + } catch (err: any) { + console.debug( + `Could not open an indexedDB transaction due to ${err.name} (${err.message}).`, + 'Trying to reopen the connection...' + ); // Try re-open. delete databases[dbName]; - _openDatabase(dbName, storeName, resolve); + _openDatabase(dbName, storeName, resolve, retry); } } \ No newline at end of file diff --git a/test/index.ts b/test/index.ts index 19540b7..9cfddde 100644 --- a/test/index.ts +++ b/test/index.ts @@ -4,7 +4,6 @@ import { get, set, del, - promisifyRequest, clear, createStore, keys, @@ -14,7 +13,8 @@ import { update, getMany, delMany -} from '../src'; +} from '../src/index'; +import { closeDatabase, promisifyRequest } from '../src/util'; import { assert as typeAssert, IsExact } from 'conditional-type-checks'; const { assert } = chai; @@ -551,5 +551,39 @@ mocha.setup('tdd'); }); }); + suite('Resilience', () => { + setup(() => Promise.all([clear(), clear(customStore)])); + + test('Database connection recovery', async () => { + await set('foo', 'bar'); + // Close the database just before getting a value. This is supposed to simulate + // an unexpected/platform closure of the database for whatever reason. The problem + // is present on iOS Safari, but not on Android Chrome/WebView and it's difficult + // to trigger/reproduce since it appears random. + closeDatabase('keyval-store'); + + let value; + try { + value = await get('foo'); + } catch (_) { + assert.fail('A get(...) must not throw if the db connection has been closed.'); + } + + assert.strictEqual(value, 'bar', 'Could not get value'); + + closeDatabase('keyval-store'); + try { + await setMany([ + ['bar', 'baz'], + ['baz', 'cat'], + ]); + } catch (_) { + assert.fail('A setMany(...) must not throw if the db connection has been closed.'); + } + + assert.deepEqual(await keys(), ['bar', 'baz', 'foo'], 'Could not get all test keys'); + }); + }); + mocha.run(); })(); From 9eb7dd09d3db7572d82ee9ce372b23ca50729d89 Mon Sep 17 00:00:00 2001 From: Zarko Hristovski Date: Tue, 20 Aug 2024 21:00:07 +0200 Subject: [PATCH 04/10] wip --- src/util.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/util.ts b/src/util.ts index f9d64ea..7bac989 100644 --- a/src/util.ts +++ b/src/util.ts @@ -54,12 +54,14 @@ async function _openDatabase( } try { + // Basic way to check if the db is open. databases[dbName].transaction(storeName); resolve(databases[dbName]); } catch (err: any) { + // Log here on purpose. console.debug( - `Could not open an indexedDB transaction due to ${err.name} (${err.message}).`, - 'Trying to reopen the connection...' + `Could not open a transaction on "${dbName}" due to ${err.name} (${err.message}). ` + + 'Trying to reopen the connection...' ); // Try re-open. delete databases[dbName]; From bba026a3a9d01e77384b5324378524bcd040174a Mon Sep 17 00:00:00 2001 From: Zarko Hristovski Date: Tue, 20 Aug 2024 23:07:53 +0200 Subject: [PATCH 05/10] temp --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index 3247c51..597414e 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,6 @@ "./dist/*": "./dist/*", "./package.json": "./package.json" }, - "files": [ - "dist/**" - ], "type": "module", "types": "./dist/index.d.ts", "sideEffects": false, From 12916f1f2a77a7499c88b47f944f65ac1d914d77 Mon Sep 17 00:00:00 2001 From: Zarko Hristovski Date: Tue, 20 Aug 2024 23:23:38 +0200 Subject: [PATCH 06/10] Revert "temp" This reverts commit bba026a3a9d01e77384b5324378524bcd040174a. --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 597414e..3247c51 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,9 @@ "./dist/*": "./dist/*", "./package.json": "./package.json" }, + "files": [ + "dist/**" + ], "type": "module", "types": "./dist/index.d.ts", "sideEffects": false, From b7fd814e0c4aef51234aedb314513f1991e0510d Mon Sep 17 00:00:00 2001 From: Zarko Hristovski Date: Tue, 20 Aug 2024 23:24:08 +0200 Subject: [PATCH 07/10] temp --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 43157c6..f9a3dfa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ node_modules .ts-tmp -dist tmp From beade592fdc9276ec52c918e66d98b167f6334f2 Mon Sep 17 00:00:00 2001 From: Zarko Hristovski Date: Tue, 20 Aug 2024 23:25:05 +0200 Subject: [PATCH 08/10] dist --- dist/compat.cjs | 410 +++++++++++++++++++++++++++++++++++++++++++++++ dist/compat.d.ts | 1 + dist/compat.js | 393 +++++++++++++++++++++++++++++++++++++++++++++ dist/index.cjs | 235 +++++++++++++++++++++++++++ dist/index.d.ts | 78 +++++++++ dist/index.js | 220 +++++++++++++++++++++++++ dist/umd.cjs | 1 + dist/umd.d.ts | 1 + dist/umd.js | 1 + dist/util.d.ts | 3 + 10 files changed, 1343 insertions(+) create mode 100644 dist/compat.cjs create mode 100644 dist/compat.d.ts create mode 100644 dist/compat.js create mode 100644 dist/index.cjs create mode 100644 dist/index.d.ts create mode 100644 dist/index.js create mode 100644 dist/umd.cjs create mode 100644 dist/umd.d.ts create mode 100644 dist/umd.js create mode 100644 dist/util.d.ts diff --git a/dist/compat.cjs b/dist/compat.cjs new file mode 100644 index 0000000..cc25836 --- /dev/null +++ b/dist/compat.cjs @@ -0,0 +1,410 @@ +'use strict'; + +function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } + +function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; } + +function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } + +function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } + +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } + +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } + +function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } + +function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } + +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); }); }; } + +Object.defineProperty(exports, '__esModule', { + value: true +}); + +function promisifyRequest(request) { + return new Promise(function (resolve, reject) { + // @ts-ignore - file size hacks + request.oncomplete = request.onsuccess = function () { + return resolve(request.result); + }; // @ts-ignore - file size hacks + + + request.onabort = request.onerror = function () { + return reject(request.error); + }; + }); +} + +var qq = {}; + +function openDatabase(_x, _x2) { + return _openDatabase2.apply(this, arguments); +} + +function _openDatabase2() { + _openDatabase2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(dbName, storeName) { + var retry, + q, + db, + _args = arguments; + return _regeneratorRuntime().wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + retry = _args.length > 2 && _args[2] !== undefined ? _args[2] : true; + q = qq[dbName] || {}; + + if (!q.promise) { + q.promise = new Promise(function (resolve) { + return q.resolve = resolve; + }); + qq[dbName] = q; + + _openDatabase(dbName, storeName, q.resolve, retry); + } + + _context.next = 5; + return q.promise; + + case 5: + db = _context.sent; + delete q.promise; + return _context.abrupt("return", db); + + case 8: + case "end": + return _context.stop(); + } + } + }, _callee); + })); + return _openDatabase2.apply(this, arguments); +} + +var databases = {}; + +function _openDatabase(_x3, _x4, _x5, _x6) { + return _openDatabase3.apply(this, arguments); +} + +function _openDatabase3() { + _openDatabase3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(dbName, storeName, resolve, retry) { + var request; + return _regeneratorRuntime().wrap(function _callee2$(_context2) { + while (1) { + switch (_context2.prev = _context2.next) { + case 0: + if (databases[dbName]) { + _context2.next = 9; + break; + } + + request = indexedDB.open(dbName); + + request.onupgradeneeded = function () { + return request.result.createObjectStore(storeName); + }; + + _context2.t0 = resolve; + _context2.next = 6; + return promisifyRequest(request); + + case 6: + _context2.t1 = databases[dbName] = _context2.sent; + (0, _context2.t0)(_context2.t1); + return _context2.abrupt("return"); + + case 9: + if (retry) { + _context2.next = 12; + break; + } + + resolve(databases[dbName]); + return _context2.abrupt("return"); + + case 12: + try { + // Basic way to check if the db is open. + databases[dbName].transaction(storeName); + resolve(databases[dbName]); + } catch (err) { + // Log here on purpose. + console.debug("Could not open a transaction on \"".concat(dbName, "\" due to ").concat(err.name, " (").concat(err.message, "). ") + 'Trying to reopen the connection...'); // Try re-open. + + delete databases[dbName]; + + _openDatabase(dbName, storeName, resolve, retry); + } + + case 13: + case "end": + return _context2.stop(); + } + } + }, _callee2); + })); + return _openDatabase3.apply(this, arguments); +} + +function createStore(dbName, storeName) { + return function (txMode, callback) { + return openDatabase(dbName, storeName).then(function (db) { + return callback(db.transaction(storeName, txMode).objectStore(storeName)); + }); + }; +} + +var defaultGetStoreFunc; + +function defaultGetStore() { + if (!defaultGetStoreFunc) { + defaultGetStoreFunc = createStore('keyval-store', 'keyval'); + } + + return defaultGetStoreFunc; +} +/** + * Get a value by its key. + * + * @param key + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function get(key) { + var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); + return customStore('readonly', function (store) { + return promisifyRequest(store.get(key)); + }); +} +/** + * Set a value with a key. + * + * @param key + * @param value + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function set(key, value) { + var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore(); + return customStore('readwrite', function (store) { + store.put(value, key); + return promisifyRequest(store.transaction); + }); +} +/** + * Set multiple values at once. This is faster than calling set() multiple times. + * It's also atomic – if one of the pairs can't be added, none will be added. + * + * @param entries Array of entries, where each entry is an array of `[key, value]`. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function setMany(entries) { + var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); + return customStore('readwrite', function (store) { + entries.forEach(function (entry) { + return store.put(entry[1], entry[0]); + }); + return promisifyRequest(store.transaction); + }); +} +/** + * Get multiple values by their keys + * + * @param keys + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function getMany(keys) { + var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); + return customStore('readonly', function (store) { + return Promise.all(keys.map(function (key) { + return promisifyRequest(store.get(key)); + })); + }); +} +/** + * Update a value. This lets you see the old value and update it as an atomic operation. + * + * @param key + * @param updater A callback that takes the old value and returns a new value. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function update(key, updater) { + var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore(); + return customStore('readwrite', function (store) { + return (// Need to create the promise manually. + // If I try to chain promises, the transaction closes in browsers + // that use a promise polyfill (IE10/11). + new Promise(function (resolve, reject) { + store.get(key).onsuccess = function () { + try { + store.put(updater(this.result), key); + resolve(promisifyRequest(store.transaction)); + } catch (err) { + reject(err); + } + }; + }) + ); + }); +} +/** + * Delete a particular key from the store. + * + * @param key + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function del(key) { + var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); + return customStore('readwrite', function (store) { + store.delete(key); + return promisifyRequest(store.transaction); + }); +} +/** + * Delete multiple keys at once. + * + * @param keys List of keys to delete. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function delMany(keys) { + var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); + return customStore('readwrite', function (store) { + keys.forEach(function (key) { + return store.delete(key); + }); + return promisifyRequest(store.transaction); + }); +} +/** + * Clear all values in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function clear() { + var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); + return customStore('readwrite', function (store) { + store.clear(); + return promisifyRequest(store.transaction); + }); +} + +function eachCursor(store, callback) { + store.openCursor().onsuccess = function () { + if (!this.result) return; + callback(this.result); + this.result.continue(); + }; + + return promisifyRequest(store.transaction); +} +/** + * Get all keys in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function keys() { + var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); + return customStore('readonly', function (store) { + // Fast path for modern browsers + if (store.getAllKeys) { + return promisifyRequest(store.getAllKeys()); + } + + var items = []; + return eachCursor(store, function (cursor) { + return items.push(cursor.key); + }).then(function () { + return items; + }); + }); +} +/** + * Get all values in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function values() { + var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); + return customStore('readonly', function (store) { + // Fast path for modern browsers + if (store.getAll) { + return promisifyRequest(store.getAll()); + } + + var items = []; + return eachCursor(store, function (cursor) { + return items.push(cursor.value); + }).then(function () { + return items; + }); + }); +} +/** + * Get all entries in the store. Each entry is an array of `[key, value]`. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function entries() { + var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); + return customStore('readonly', function (store) { + // Fast path for modern browsers + // (although, hopefully we'll get a simpler path some day) + if (store.getAll && store.getAllKeys) { + return Promise.all([promisifyRequest(store.getAllKeys()), promisifyRequest(store.getAll())]).then(function (_ref) { + var _ref2 = _slicedToArray(_ref, 2), + keys = _ref2[0], + values = _ref2[1]; + + return keys.map(function (key, i) { + return [key, values[i]]; + }); + }); + } + + var items = []; + return customStore('readonly', function (store) { + return eachCursor(store, function (cursor) { + return items.push([cursor.key, cursor.value]); + }).then(function () { + return items; + }); + }); + }); +} + +exports.clear = clear; +exports.createStore = createStore; +exports.del = del; +exports.delMany = delMany; +exports.entries = entries; +exports.get = get; +exports.getMany = getMany; +exports.keys = keys; +exports.set = set; +exports.setMany = setMany; +exports.update = update; +exports.values = values; diff --git a/dist/compat.d.ts b/dist/compat.d.ts new file mode 100644 index 0000000..8a72944 --- /dev/null +++ b/dist/compat.d.ts @@ -0,0 +1 @@ +export * from './'; \ No newline at end of file diff --git a/dist/compat.js b/dist/compat.js new file mode 100644 index 0000000..4592a92 --- /dev/null +++ b/dist/compat.js @@ -0,0 +1,393 @@ +function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } + +function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; } + +function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } + +function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } + +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } + +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } + +function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } + +function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } + +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); }); }; } + +function promisifyRequest(request) { + return new Promise(function (resolve, reject) { + // @ts-ignore - file size hacks + request.oncomplete = request.onsuccess = function () { + return resolve(request.result); + }; // @ts-ignore - file size hacks + + + request.onabort = request.onerror = function () { + return reject(request.error); + }; + }); +} + +var qq = {}; + +function openDatabase(_x, _x2) { + return _openDatabase2.apply(this, arguments); +} + +function _openDatabase2() { + _openDatabase2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(dbName, storeName) { + var retry, + q, + db, + _args = arguments; + return _regeneratorRuntime().wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + retry = _args.length > 2 && _args[2] !== undefined ? _args[2] : true; + q = qq[dbName] || {}; + + if (!q.promise) { + q.promise = new Promise(function (resolve) { + return q.resolve = resolve; + }); + qq[dbName] = q; + + _openDatabase(dbName, storeName, q.resolve, retry); + } + + _context.next = 5; + return q.promise; + + case 5: + db = _context.sent; + delete q.promise; + return _context.abrupt("return", db); + + case 8: + case "end": + return _context.stop(); + } + } + }, _callee); + })); + return _openDatabase2.apply(this, arguments); +} + +var databases = {}; + +function _openDatabase(_x3, _x4, _x5, _x6) { + return _openDatabase3.apply(this, arguments); +} + +function _openDatabase3() { + _openDatabase3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(dbName, storeName, resolve, retry) { + var request; + return _regeneratorRuntime().wrap(function _callee2$(_context2) { + while (1) { + switch (_context2.prev = _context2.next) { + case 0: + if (databases[dbName]) { + _context2.next = 9; + break; + } + + request = indexedDB.open(dbName); + + request.onupgradeneeded = function () { + return request.result.createObjectStore(storeName); + }; + + _context2.t0 = resolve; + _context2.next = 6; + return promisifyRequest(request); + + case 6: + _context2.t1 = databases[dbName] = _context2.sent; + (0, _context2.t0)(_context2.t1); + return _context2.abrupt("return"); + + case 9: + if (retry) { + _context2.next = 12; + break; + } + + resolve(databases[dbName]); + return _context2.abrupt("return"); + + case 12: + try { + // Basic way to check if the db is open. + databases[dbName].transaction(storeName); + resolve(databases[dbName]); + } catch (err) { + // Log here on purpose. + console.debug("Could not open a transaction on \"".concat(dbName, "\" due to ").concat(err.name, " (").concat(err.message, "). ") + 'Trying to reopen the connection...'); // Try re-open. + + delete databases[dbName]; + + _openDatabase(dbName, storeName, resolve, retry); + } + + case 13: + case "end": + return _context2.stop(); + } + } + }, _callee2); + })); + return _openDatabase3.apply(this, arguments); +} + +function createStore(dbName, storeName) { + return function (txMode, callback) { + return openDatabase(dbName, storeName).then(function (db) { + return callback(db.transaction(storeName, txMode).objectStore(storeName)); + }); + }; +} + +var defaultGetStoreFunc; + +function defaultGetStore() { + if (!defaultGetStoreFunc) { + defaultGetStoreFunc = createStore('keyval-store', 'keyval'); + } + + return defaultGetStoreFunc; +} +/** + * Get a value by its key. + * + * @param key + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function get(key) { + var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); + return customStore('readonly', function (store) { + return promisifyRequest(store.get(key)); + }); +} +/** + * Set a value with a key. + * + * @param key + * @param value + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function set(key, value) { + var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore(); + return customStore('readwrite', function (store) { + store.put(value, key); + return promisifyRequest(store.transaction); + }); +} +/** + * Set multiple values at once. This is faster than calling set() multiple times. + * It's also atomic – if one of the pairs can't be added, none will be added. + * + * @param entries Array of entries, where each entry is an array of `[key, value]`. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function setMany(entries) { + var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); + return customStore('readwrite', function (store) { + entries.forEach(function (entry) { + return store.put(entry[1], entry[0]); + }); + return promisifyRequest(store.transaction); + }); +} +/** + * Get multiple values by their keys + * + * @param keys + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function getMany(keys) { + var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); + return customStore('readonly', function (store) { + return Promise.all(keys.map(function (key) { + return promisifyRequest(store.get(key)); + })); + }); +} +/** + * Update a value. This lets you see the old value and update it as an atomic operation. + * + * @param key + * @param updater A callback that takes the old value and returns a new value. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function update(key, updater) { + var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore(); + return customStore('readwrite', function (store) { + return (// Need to create the promise manually. + // If I try to chain promises, the transaction closes in browsers + // that use a promise polyfill (IE10/11). + new Promise(function (resolve, reject) { + store.get(key).onsuccess = function () { + try { + store.put(updater(this.result), key); + resolve(promisifyRequest(store.transaction)); + } catch (err) { + reject(err); + } + }; + }) + ); + }); +} +/** + * Delete a particular key from the store. + * + * @param key + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function del(key) { + var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); + return customStore('readwrite', function (store) { + store.delete(key); + return promisifyRequest(store.transaction); + }); +} +/** + * Delete multiple keys at once. + * + * @param keys List of keys to delete. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function delMany(keys) { + var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); + return customStore('readwrite', function (store) { + keys.forEach(function (key) { + return store.delete(key); + }); + return promisifyRequest(store.transaction); + }); +} +/** + * Clear all values in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function clear() { + var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); + return customStore('readwrite', function (store) { + store.clear(); + return promisifyRequest(store.transaction); + }); +} + +function eachCursor(store, callback) { + store.openCursor().onsuccess = function () { + if (!this.result) return; + callback(this.result); + this.result.continue(); + }; + + return promisifyRequest(store.transaction); +} +/** + * Get all keys in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function keys() { + var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); + return customStore('readonly', function (store) { + // Fast path for modern browsers + if (store.getAllKeys) { + return promisifyRequest(store.getAllKeys()); + } + + var items = []; + return eachCursor(store, function (cursor) { + return items.push(cursor.key); + }).then(function () { + return items; + }); + }); +} +/** + * Get all values in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function values() { + var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); + return customStore('readonly', function (store) { + // Fast path for modern browsers + if (store.getAll) { + return promisifyRequest(store.getAll()); + } + + var items = []; + return eachCursor(store, function (cursor) { + return items.push(cursor.value); + }).then(function () { + return items; + }); + }); +} +/** + * Get all entries in the store. Each entry is an array of `[key, value]`. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ + + +function entries() { + var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); + return customStore('readonly', function (store) { + // Fast path for modern browsers + // (although, hopefully we'll get a simpler path some day) + if (store.getAll && store.getAllKeys) { + return Promise.all([promisifyRequest(store.getAllKeys()), promisifyRequest(store.getAll())]).then(function (_ref) { + var _ref2 = _slicedToArray(_ref, 2), + keys = _ref2[0], + values = _ref2[1]; + + return keys.map(function (key, i) { + return [key, values[i]]; + }); + }); + } + + var items = []; + return customStore('readonly', function (store) { + return eachCursor(store, function (cursor) { + return items.push([cursor.key, cursor.value]); + }).then(function () { + return items; + }); + }); + }); +} + +export { clear, createStore, del, delMany, entries, get, getMany, keys, set, setMany, update, values }; diff --git a/dist/index.cjs b/dist/index.cjs new file mode 100644 index 0000000..eb9d4cd --- /dev/null +++ b/dist/index.cjs @@ -0,0 +1,235 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); + +function promisifyRequest(request) { + return new Promise((resolve, reject) => { + // @ts-ignore - file size hacks + request.oncomplete = request.onsuccess = () => resolve(request.result); + // @ts-ignore - file size hacks + request.onabort = request.onerror = () => reject(request.error); + }); +} +const qq = {}; +async function openDatabase(dbName, storeName, retry = true) { + const q = qq[dbName] || {}; + if (!q.promise) { + q.promise = new Promise(resolve => q.resolve = resolve); + qq[dbName] = q; + _openDatabase(dbName, storeName, q.resolve, retry); + } + const db = await q.promise; + delete q.promise; + return db; +} +const databases = {}; +async function _openDatabase(dbName, storeName, resolve, retry) { + if (!databases[dbName]) { + const request = indexedDB.open(dbName); + request.onupgradeneeded = () => request.result.createObjectStore(storeName); + resolve(databases[dbName] = await promisifyRequest(request)); + return; + } + if (!retry) { + resolve(databases[dbName]); + return; + } + try { + // Basic way to check if the db is open. + databases[dbName].transaction(storeName); + resolve(databases[dbName]); + } + catch (err) { + // Log here on purpose. + console.debug(`Could not open a transaction on "${dbName}" due to ${err.name} (${err.message}). ` + + 'Trying to reopen the connection...'); + // Try re-open. + delete databases[dbName]; + _openDatabase(dbName, storeName, resolve, retry); + } +} + +function createStore(dbName, storeName) { + return (txMode, callback) => openDatabase(dbName, storeName).then(db => callback(db.transaction(storeName, txMode).objectStore(storeName))); +} +let defaultGetStoreFunc; +function defaultGetStore() { + if (!defaultGetStoreFunc) { + defaultGetStoreFunc = createStore('keyval-store', 'keyval'); + } + return defaultGetStoreFunc; +} +/** + * Get a value by its key. + * + * @param key + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function get(key, customStore = defaultGetStore()) { + return customStore('readonly', (store) => promisifyRequest(store.get(key))); +} +/** + * Set a value with a key. + * + * @param key + * @param value + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function set(key, value, customStore = defaultGetStore()) { + return customStore('readwrite', (store) => { + store.put(value, key); + return promisifyRequest(store.transaction); + }); +} +/** + * Set multiple values at once. This is faster than calling set() multiple times. + * It's also atomic – if one of the pairs can't be added, none will be added. + * + * @param entries Array of entries, where each entry is an array of `[key, value]`. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function setMany(entries, customStore = defaultGetStore()) { + return customStore('readwrite', (store) => { + entries.forEach((entry) => store.put(entry[1], entry[0])); + return promisifyRequest(store.transaction); + }); +} +/** + * Get multiple values by their keys + * + * @param keys + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function getMany(keys, customStore = defaultGetStore()) { + return customStore('readonly', (store) => Promise.all(keys.map((key) => promisifyRequest(store.get(key))))); +} +/** + * Update a value. This lets you see the old value and update it as an atomic operation. + * + * @param key + * @param updater A callback that takes the old value and returns a new value. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function update(key, updater, customStore = defaultGetStore()) { + return customStore('readwrite', (store) => + // Need to create the promise manually. + // If I try to chain promises, the transaction closes in browsers + // that use a promise polyfill (IE10/11). + new Promise((resolve, reject) => { + store.get(key).onsuccess = function () { + try { + store.put(updater(this.result), key); + resolve(promisifyRequest(store.transaction)); + } + catch (err) { + reject(err); + } + }; + })); +} +/** + * Delete a particular key from the store. + * + * @param key + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function del(key, customStore = defaultGetStore()) { + return customStore('readwrite', (store) => { + store.delete(key); + return promisifyRequest(store.transaction); + }); +} +/** + * Delete multiple keys at once. + * + * @param keys List of keys to delete. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function delMany(keys, customStore = defaultGetStore()) { + return customStore('readwrite', (store) => { + keys.forEach((key) => store.delete(key)); + return promisifyRequest(store.transaction); + }); +} +/** + * Clear all values in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function clear(customStore = defaultGetStore()) { + return customStore('readwrite', (store) => { + store.clear(); + return promisifyRequest(store.transaction); + }); +} +function eachCursor(store, callback) { + store.openCursor().onsuccess = function () { + if (!this.result) + return; + callback(this.result); + this.result.continue(); + }; + return promisifyRequest(store.transaction); +} +/** + * Get all keys in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function keys(customStore = defaultGetStore()) { + return customStore('readonly', (store) => { + // Fast path for modern browsers + if (store.getAllKeys) { + return promisifyRequest(store.getAllKeys()); + } + const items = []; + return eachCursor(store, (cursor) => items.push(cursor.key)).then(() => items); + }); +} +/** + * Get all values in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function values(customStore = defaultGetStore()) { + return customStore('readonly', (store) => { + // Fast path for modern browsers + if (store.getAll) { + return promisifyRequest(store.getAll()); + } + const items = []; + return eachCursor(store, (cursor) => items.push(cursor.value)).then(() => items); + }); +} +/** + * Get all entries in the store. Each entry is an array of `[key, value]`. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function entries(customStore = defaultGetStore()) { + return customStore('readonly', (store) => { + // Fast path for modern browsers + // (although, hopefully we'll get a simpler path some day) + if (store.getAll && store.getAllKeys) { + return Promise.all([ + promisifyRequest(store.getAllKeys()), + promisifyRequest(store.getAll()), + ]).then(([keys, values]) => keys.map((key, i) => [key, values[i]])); + } + const items = []; + return customStore('readonly', (store) => eachCursor(store, (cursor) => items.push([cursor.key, cursor.value])).then(() => items)); + }); +} + +exports.clear = clear; +exports.createStore = createStore; +exports.del = del; +exports.delMany = delMany; +exports.entries = entries; +exports.get = get; +exports.getMany = getMany; +exports.keys = keys; +exports.set = set; +exports.setMany = setMany; +exports.update = update; +exports.values = values; diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..37e9493 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,78 @@ +export declare function createStore(dbName: string, storeName: string): UseStore; +export declare type UseStore = (txMode: IDBTransactionMode, callback: (store: IDBObjectStore) => T | PromiseLike) => Promise; +/** + * Get a value by its key. + * + * @param key + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function get(key: IDBValidKey, customStore?: UseStore): Promise; +/** + * Set a value with a key. + * + * @param key + * @param value + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function set(key: IDBValidKey, value: any, customStore?: UseStore): Promise; +/** + * Set multiple values at once. This is faster than calling set() multiple times. + * It's also atomic – if one of the pairs can't be added, none will be added. + * + * @param entries Array of entries, where each entry is an array of `[key, value]`. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function setMany(entries: [IDBValidKey, any][], customStore?: UseStore): Promise; +/** + * Get multiple values by their keys + * + * @param keys + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function getMany(keys: IDBValidKey[], customStore?: UseStore): Promise; +/** + * Update a value. This lets you see the old value and update it as an atomic operation. + * + * @param key + * @param updater A callback that takes the old value and returns a new value. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function update(key: IDBValidKey, updater: (oldValue: T | undefined) => T, customStore?: UseStore): Promise; +/** + * Delete a particular key from the store. + * + * @param key + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function del(key: IDBValidKey, customStore?: UseStore): Promise; +/** + * Delete multiple keys at once. + * + * @param keys List of keys to delete. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function delMany(keys: IDBValidKey[], customStore?: UseStore): Promise; +/** + * Clear all values in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function clear(customStore?: UseStore): Promise; +/** + * Get all keys in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function keys(customStore?: UseStore): Promise; +/** + * Get all values in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function values(customStore?: UseStore): Promise; +/** + * Get all entries in the store. Each entry is an array of `[key, value]`. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +export declare function entries(customStore?: UseStore): Promise<[KeyType, ValueType][]>; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..510dd96 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,220 @@ +function promisifyRequest(request) { + return new Promise((resolve, reject) => { + // @ts-ignore - file size hacks + request.oncomplete = request.onsuccess = () => resolve(request.result); + // @ts-ignore - file size hacks + request.onabort = request.onerror = () => reject(request.error); + }); +} +const qq = {}; +async function openDatabase(dbName, storeName, retry = true) { + const q = qq[dbName] || {}; + if (!q.promise) { + q.promise = new Promise(resolve => q.resolve = resolve); + qq[dbName] = q; + _openDatabase(dbName, storeName, q.resolve, retry); + } + const db = await q.promise; + delete q.promise; + return db; +} +const databases = {}; +async function _openDatabase(dbName, storeName, resolve, retry) { + if (!databases[dbName]) { + const request = indexedDB.open(dbName); + request.onupgradeneeded = () => request.result.createObjectStore(storeName); + resolve(databases[dbName] = await promisifyRequest(request)); + return; + } + if (!retry) { + resolve(databases[dbName]); + return; + } + try { + // Basic way to check if the db is open. + databases[dbName].transaction(storeName); + resolve(databases[dbName]); + } + catch (err) { + // Log here on purpose. + console.debug(`Could not open a transaction on "${dbName}" due to ${err.name} (${err.message}). ` + + 'Trying to reopen the connection...'); + // Try re-open. + delete databases[dbName]; + _openDatabase(dbName, storeName, resolve, retry); + } +} + +function createStore(dbName, storeName) { + return (txMode, callback) => openDatabase(dbName, storeName).then(db => callback(db.transaction(storeName, txMode).objectStore(storeName))); +} +let defaultGetStoreFunc; +function defaultGetStore() { + if (!defaultGetStoreFunc) { + defaultGetStoreFunc = createStore('keyval-store', 'keyval'); + } + return defaultGetStoreFunc; +} +/** + * Get a value by its key. + * + * @param key + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function get(key, customStore = defaultGetStore()) { + return customStore('readonly', (store) => promisifyRequest(store.get(key))); +} +/** + * Set a value with a key. + * + * @param key + * @param value + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function set(key, value, customStore = defaultGetStore()) { + return customStore('readwrite', (store) => { + store.put(value, key); + return promisifyRequest(store.transaction); + }); +} +/** + * Set multiple values at once. This is faster than calling set() multiple times. + * It's also atomic – if one of the pairs can't be added, none will be added. + * + * @param entries Array of entries, where each entry is an array of `[key, value]`. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function setMany(entries, customStore = defaultGetStore()) { + return customStore('readwrite', (store) => { + entries.forEach((entry) => store.put(entry[1], entry[0])); + return promisifyRequest(store.transaction); + }); +} +/** + * Get multiple values by their keys + * + * @param keys + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function getMany(keys, customStore = defaultGetStore()) { + return customStore('readonly', (store) => Promise.all(keys.map((key) => promisifyRequest(store.get(key))))); +} +/** + * Update a value. This lets you see the old value and update it as an atomic operation. + * + * @param key + * @param updater A callback that takes the old value and returns a new value. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function update(key, updater, customStore = defaultGetStore()) { + return customStore('readwrite', (store) => + // Need to create the promise manually. + // If I try to chain promises, the transaction closes in browsers + // that use a promise polyfill (IE10/11). + new Promise((resolve, reject) => { + store.get(key).onsuccess = function () { + try { + store.put(updater(this.result), key); + resolve(promisifyRequest(store.transaction)); + } + catch (err) { + reject(err); + } + }; + })); +} +/** + * Delete a particular key from the store. + * + * @param key + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function del(key, customStore = defaultGetStore()) { + return customStore('readwrite', (store) => { + store.delete(key); + return promisifyRequest(store.transaction); + }); +} +/** + * Delete multiple keys at once. + * + * @param keys List of keys to delete. + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function delMany(keys, customStore = defaultGetStore()) { + return customStore('readwrite', (store) => { + keys.forEach((key) => store.delete(key)); + return promisifyRequest(store.transaction); + }); +} +/** + * Clear all values in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function clear(customStore = defaultGetStore()) { + return customStore('readwrite', (store) => { + store.clear(); + return promisifyRequest(store.transaction); + }); +} +function eachCursor(store, callback) { + store.openCursor().onsuccess = function () { + if (!this.result) + return; + callback(this.result); + this.result.continue(); + }; + return promisifyRequest(store.transaction); +} +/** + * Get all keys in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function keys(customStore = defaultGetStore()) { + return customStore('readonly', (store) => { + // Fast path for modern browsers + if (store.getAllKeys) { + return promisifyRequest(store.getAllKeys()); + } + const items = []; + return eachCursor(store, (cursor) => items.push(cursor.key)).then(() => items); + }); +} +/** + * Get all values in the store. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function values(customStore = defaultGetStore()) { + return customStore('readonly', (store) => { + // Fast path for modern browsers + if (store.getAll) { + return promisifyRequest(store.getAll()); + } + const items = []; + return eachCursor(store, (cursor) => items.push(cursor.value)).then(() => items); + }); +} +/** + * Get all entries in the store. Each entry is an array of `[key, value]`. + * + * @param customStore Method to get a custom store. Use with caution (see the docs). + */ +function entries(customStore = defaultGetStore()) { + return customStore('readonly', (store) => { + // Fast path for modern browsers + // (although, hopefully we'll get a simpler path some day) + if (store.getAll && store.getAllKeys) { + return Promise.all([ + promisifyRequest(store.getAllKeys()), + promisifyRequest(store.getAll()), + ]).then(([keys, values]) => keys.map((key, i) => [key, values[i]])); + } + const items = []; + return customStore('readonly', (store) => eachCursor(store, (cursor) => items.push([cursor.key, cursor.value])).then(() => items)); + }); +} + +export { clear, createStore, del, delMany, entries, get, getMany, keys, set, setMany, update, values }; diff --git a/dist/umd.cjs b/dist/umd.cjs new file mode 100644 index 0000000..b2c7fe4 --- /dev/null +++ b/dist/umd.cjs @@ -0,0 +1 @@ +function _regeneratorRuntime(){"use strict";/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */_regeneratorRuntime=function(){return t};var t={},r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function c(t,r,e,n){var o=r&&r.prototype instanceof s?r:s,i=Object.create(o.prototype),a=new L(n||[]);return i._invoke=function(t,r,e){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return E()}for(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var u=b(a,e);if(u){if(u===l)continue;return u}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var c=f(t,r,e);if("normal"===c.type){if(n=e.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:e.done}}"throw"===c.type&&(n="completed",e.method="throw",e.arg=c.arg)}}}(t,e,a),i}function f(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function s(){}function h(){}function y(){}var p={};u(p,o,(function(){return this}));var d=Object.getPrototypeOf,v=d&&d(d(A([])));v&&v!==r&&e.call(v,o)&&(p=v);var g=y.prototype=s.prototype=Object.create(p);function m(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function w(t,r){function n(o,i,a,u){var c=f(t[o],t,i);if("throw"!==c.type){var l=c.arg,s=l.value;return s&&"object"==_typeof(s)&&e.call(s,"__await")?r.resolve(s.__await).then((function(t){n("next",t,a,u)}),(function(t){n("throw",t,a,u)})):r.resolve(s).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,u)}))}u(c.arg)}var o;this._invoke=function(t,e){function i(){return new r((function(r,o){n(t,e,r,o)}))}return o=o?o.then(i,i):i()}}function b(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,b(t,r),"throw"===r.method))return l;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=f(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,l;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,l):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,l)}function _(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function x(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function A(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function r(){for(;++n=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=e.call(i,"catchLoc"),c=e.call(i,"finallyLoc");if(u&&c){if(this.prev=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),x(e),l}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;x(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:A(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),l}},t}function _slicedToArray(t,r){return _arrayWithHoles(t)||_iterableToArrayLimit(t,r)||_unsupportedIterableToArray(t,r)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(t,r){if(t){if("string"==typeof t)return _arrayLikeToArray(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?_arrayLikeToArray(t,r):void 0}}function _arrayLikeToArray(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e2&&void 0!==c[2])||c[2],(i=e[r]||{}).promise||(i.promise=new Promise((function(t){return i.resolve=t})),e[r]=i,a(r,n,i.resolve,o)),t.next=5,i.promise;case 5:return u=t.sent,delete i.promise,t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)}))),n.apply(this,arguments)}var o,i={};function a(t,r,e,n){return u.apply(this,arguments)}function u(){return(u=_asyncToGenerator(_regeneratorRuntime().mark((function t(e,n,o,u){var c;return _regeneratorRuntime().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(i[e]){t.next=9;break}return(c=indexedDB.open(e)).onupgradeneeded=function(){return c.result.createObjectStore(n)},t.t0=o,t.next=6,r(c);case 6:return t.t1=i[e]=t.sent,(0,t.t0)(t.t1),t.abrupt("return");case 9:if(u){t.next=12;break}return o(i[e]),t.abrupt("return");case 12:try{i[e].transaction(n),o(i[e])}catch(t){console.debug('Could not open a transaction on "'.concat(e,'" due to ').concat(t.name," (").concat(t.message,"). ")+"Trying to reopen the connection..."),delete i[e],a(e,n,o,u)}case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function c(t,r){return function(e,o){return function(t,r){return n.apply(this,arguments)}(t,r).then((function(t){return o(t.transaction(r,e).objectStore(r))}))}}function f(){return o||(o=c("keyval-store","keyval")),o}function l(t,e){return t.openCursor().onsuccess=function(){this.result&&(e(this.result),this.result.continue())},r(t.transaction)}t.clear=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readwrite",(function(t){return t.clear(),r(t.transaction)}))},t.createStore=c,t.del=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return e.delete(t),r(e.transaction)}))},t.delMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return t.forEach((function(t){return e.delete(t)})),r(e.transaction)}))},t.entries=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(e){if(e.getAll&&e.getAllKeys)return Promise.all([r(e.getAllKeys()),r(e.getAll())]).then((function(t){var r=_slicedToArray(t,2),e=r[0],n=r[1];return e.map((function(t,r){return[t,n[r]]}))}));var n=[];return t("readonly",(function(t){return l(t,(function(t){return n.push([t.key,t.value])})).then((function(){return n}))}))}))},t.get=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readonly",(function(e){return r(e.get(t))}))},t.getMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readonly",(function(e){return Promise.all(t.map((function(t){return r(e.get(t))})))}))},t.keys=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(t){if(t.getAllKeys)return r(t.getAllKeys());var e=[];return l(t,(function(t){return e.push(t.key)})).then((function(){return e}))}))},t.set=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:f();return n("readwrite",(function(n){return n.put(e,t),r(n.transaction)}))},t.setMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return t.forEach((function(t){return e.put(t[1],t[0])})),r(e.transaction)}))},t.update=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:f();return n("readwrite",(function(n){return new Promise((function(o,i){n.get(t).onsuccess=function(){try{n.put(e(this.result),t),o(r(n.transaction))}catch(t){i(t)}}}))}))},t.values=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(t){if(t.getAll)return r(t.getAll());var e=[];return l(t,(function(t){return e.push(t.value)})).then((function(){return e}))}))},Object.defineProperty(t,"__esModule",{value:!0})})); diff --git a/dist/umd.d.ts b/dist/umd.d.ts new file mode 100644 index 0000000..8a72944 --- /dev/null +++ b/dist/umd.d.ts @@ -0,0 +1 @@ +export * from './'; \ No newline at end of file diff --git a/dist/umd.js b/dist/umd.js new file mode 100644 index 0000000..b2c7fe4 --- /dev/null +++ b/dist/umd.js @@ -0,0 +1 @@ +function _regeneratorRuntime(){"use strict";/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */_regeneratorRuntime=function(){return t};var t={},r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function c(t,r,e,n){var o=r&&r.prototype instanceof s?r:s,i=Object.create(o.prototype),a=new L(n||[]);return i._invoke=function(t,r,e){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return E()}for(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var u=b(a,e);if(u){if(u===l)continue;return u}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var c=f(t,r,e);if("normal"===c.type){if(n=e.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:e.done}}"throw"===c.type&&(n="completed",e.method="throw",e.arg=c.arg)}}}(t,e,a),i}function f(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function s(){}function h(){}function y(){}var p={};u(p,o,(function(){return this}));var d=Object.getPrototypeOf,v=d&&d(d(A([])));v&&v!==r&&e.call(v,o)&&(p=v);var g=y.prototype=s.prototype=Object.create(p);function m(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function w(t,r){function n(o,i,a,u){var c=f(t[o],t,i);if("throw"!==c.type){var l=c.arg,s=l.value;return s&&"object"==_typeof(s)&&e.call(s,"__await")?r.resolve(s.__await).then((function(t){n("next",t,a,u)}),(function(t){n("throw",t,a,u)})):r.resolve(s).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,u)}))}u(c.arg)}var o;this._invoke=function(t,e){function i(){return new r((function(r,o){n(t,e,r,o)}))}return o=o?o.then(i,i):i()}}function b(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,b(t,r),"throw"===r.method))return l;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=f(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,l;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,l):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,l)}function _(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function x(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function A(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function r(){for(;++n=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=e.call(i,"catchLoc"),c=e.call(i,"finallyLoc");if(u&&c){if(this.prev=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),x(e),l}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;x(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:A(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),l}},t}function _slicedToArray(t,r){return _arrayWithHoles(t)||_iterableToArrayLimit(t,r)||_unsupportedIterableToArray(t,r)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(t,r){if(t){if("string"==typeof t)return _arrayLikeToArray(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?_arrayLikeToArray(t,r):void 0}}function _arrayLikeToArray(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e2&&void 0!==c[2])||c[2],(i=e[r]||{}).promise||(i.promise=new Promise((function(t){return i.resolve=t})),e[r]=i,a(r,n,i.resolve,o)),t.next=5,i.promise;case 5:return u=t.sent,delete i.promise,t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)}))),n.apply(this,arguments)}var o,i={};function a(t,r,e,n){return u.apply(this,arguments)}function u(){return(u=_asyncToGenerator(_regeneratorRuntime().mark((function t(e,n,o,u){var c;return _regeneratorRuntime().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(i[e]){t.next=9;break}return(c=indexedDB.open(e)).onupgradeneeded=function(){return c.result.createObjectStore(n)},t.t0=o,t.next=6,r(c);case 6:return t.t1=i[e]=t.sent,(0,t.t0)(t.t1),t.abrupt("return");case 9:if(u){t.next=12;break}return o(i[e]),t.abrupt("return");case 12:try{i[e].transaction(n),o(i[e])}catch(t){console.debug('Could not open a transaction on "'.concat(e,'" due to ').concat(t.name," (").concat(t.message,"). ")+"Trying to reopen the connection..."),delete i[e],a(e,n,o,u)}case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function c(t,r){return function(e,o){return function(t,r){return n.apply(this,arguments)}(t,r).then((function(t){return o(t.transaction(r,e).objectStore(r))}))}}function f(){return o||(o=c("keyval-store","keyval")),o}function l(t,e){return t.openCursor().onsuccess=function(){this.result&&(e(this.result),this.result.continue())},r(t.transaction)}t.clear=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readwrite",(function(t){return t.clear(),r(t.transaction)}))},t.createStore=c,t.del=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return e.delete(t),r(e.transaction)}))},t.delMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return t.forEach((function(t){return e.delete(t)})),r(e.transaction)}))},t.entries=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(e){if(e.getAll&&e.getAllKeys)return Promise.all([r(e.getAllKeys()),r(e.getAll())]).then((function(t){var r=_slicedToArray(t,2),e=r[0],n=r[1];return e.map((function(t,r){return[t,n[r]]}))}));var n=[];return t("readonly",(function(t){return l(t,(function(t){return n.push([t.key,t.value])})).then((function(){return n}))}))}))},t.get=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readonly",(function(e){return r(e.get(t))}))},t.getMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readonly",(function(e){return Promise.all(t.map((function(t){return r(e.get(t))})))}))},t.keys=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(t){if(t.getAllKeys)return r(t.getAllKeys());var e=[];return l(t,(function(t){return e.push(t.key)})).then((function(){return e}))}))},t.set=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:f();return n("readwrite",(function(n){return n.put(e,t),r(n.transaction)}))},t.setMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return t.forEach((function(t){return e.put(t[1],t[0])})),r(e.transaction)}))},t.update=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:f();return n("readwrite",(function(n){return new Promise((function(o,i){n.get(t).onsuccess=function(){try{n.put(e(this.result),t),o(r(n.transaction))}catch(t){i(t)}}}))}))},t.values=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(t){if(t.getAll)return r(t.getAll());var e=[];return l(t,(function(t){return e.push(t.value)})).then((function(){return e}))}))},Object.defineProperty(t,"__esModule",{value:!0})})); diff --git a/dist/util.d.ts b/dist/util.d.ts new file mode 100644 index 0000000..99c1b7b --- /dev/null +++ b/dist/util.d.ts @@ -0,0 +1,3 @@ +export declare function promisifyRequest(request: IDBRequest | IDBTransaction): Promise; +export declare function openDatabase(dbName: string, storeName: string, retry?: boolean): Promise; +export declare function closeDatabase(dbName: string): Promise; From 3094591d099742406409ad6461072245fb271d0e Mon Sep 17 00:00:00 2001 From: Zarko Hristovski Date: Wed, 21 Aug 2024 09:54:12 +0200 Subject: [PATCH 09/10] Revert "temp" This reverts commit b7fd814e0c4aef51234aedb314513f1991e0510d. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f9a3dfa..43157c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules .ts-tmp +dist tmp From af391aaa2cde4410008615d6b4f8b00124528a4c Mon Sep 17 00:00:00 2001 From: Zarko Hristovski Date: Wed, 21 Aug 2024 09:54:32 +0200 Subject: [PATCH 10/10] Revert "dist" This reverts commit beade592fdc9276ec52c918e66d98b167f6334f2. --- dist/compat.cjs | 410 ----------------------------------------------- dist/compat.d.ts | 1 - dist/compat.js | 393 --------------------------------------------- dist/index.cjs | 235 --------------------------- dist/index.d.ts | 78 --------- dist/index.js | 220 ------------------------- dist/umd.cjs | 1 - dist/umd.d.ts | 1 - dist/umd.js | 1 - dist/util.d.ts | 3 - 10 files changed, 1343 deletions(-) delete mode 100644 dist/compat.cjs delete mode 100644 dist/compat.d.ts delete mode 100644 dist/compat.js delete mode 100644 dist/index.cjs delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/umd.cjs delete mode 100644 dist/umd.d.ts delete mode 100644 dist/umd.js delete mode 100644 dist/util.d.ts diff --git a/dist/compat.cjs b/dist/compat.cjs deleted file mode 100644 index cc25836..0000000 --- a/dist/compat.cjs +++ /dev/null @@ -1,410 +0,0 @@ -'use strict'; - -function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } - -function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; } - -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } - -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } - -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } - -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } - -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } - -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } - -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); }); }; } - -Object.defineProperty(exports, '__esModule', { - value: true -}); - -function promisifyRequest(request) { - return new Promise(function (resolve, reject) { - // @ts-ignore - file size hacks - request.oncomplete = request.onsuccess = function () { - return resolve(request.result); - }; // @ts-ignore - file size hacks - - - request.onabort = request.onerror = function () { - return reject(request.error); - }; - }); -} - -var qq = {}; - -function openDatabase(_x, _x2) { - return _openDatabase2.apply(this, arguments); -} - -function _openDatabase2() { - _openDatabase2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(dbName, storeName) { - var retry, - q, - db, - _args = arguments; - return _regeneratorRuntime().wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - retry = _args.length > 2 && _args[2] !== undefined ? _args[2] : true; - q = qq[dbName] || {}; - - if (!q.promise) { - q.promise = new Promise(function (resolve) { - return q.resolve = resolve; - }); - qq[dbName] = q; - - _openDatabase(dbName, storeName, q.resolve, retry); - } - - _context.next = 5; - return q.promise; - - case 5: - db = _context.sent; - delete q.promise; - return _context.abrupt("return", db); - - case 8: - case "end": - return _context.stop(); - } - } - }, _callee); - })); - return _openDatabase2.apply(this, arguments); -} - -var databases = {}; - -function _openDatabase(_x3, _x4, _x5, _x6) { - return _openDatabase3.apply(this, arguments); -} - -function _openDatabase3() { - _openDatabase3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(dbName, storeName, resolve, retry) { - var request; - return _regeneratorRuntime().wrap(function _callee2$(_context2) { - while (1) { - switch (_context2.prev = _context2.next) { - case 0: - if (databases[dbName]) { - _context2.next = 9; - break; - } - - request = indexedDB.open(dbName); - - request.onupgradeneeded = function () { - return request.result.createObjectStore(storeName); - }; - - _context2.t0 = resolve; - _context2.next = 6; - return promisifyRequest(request); - - case 6: - _context2.t1 = databases[dbName] = _context2.sent; - (0, _context2.t0)(_context2.t1); - return _context2.abrupt("return"); - - case 9: - if (retry) { - _context2.next = 12; - break; - } - - resolve(databases[dbName]); - return _context2.abrupt("return"); - - case 12: - try { - // Basic way to check if the db is open. - databases[dbName].transaction(storeName); - resolve(databases[dbName]); - } catch (err) { - // Log here on purpose. - console.debug("Could not open a transaction on \"".concat(dbName, "\" due to ").concat(err.name, " (").concat(err.message, "). ") + 'Trying to reopen the connection...'); // Try re-open. - - delete databases[dbName]; - - _openDatabase(dbName, storeName, resolve, retry); - } - - case 13: - case "end": - return _context2.stop(); - } - } - }, _callee2); - })); - return _openDatabase3.apply(this, arguments); -} - -function createStore(dbName, storeName) { - return function (txMode, callback) { - return openDatabase(dbName, storeName).then(function (db) { - return callback(db.transaction(storeName, txMode).objectStore(storeName)); - }); - }; -} - -var defaultGetStoreFunc; - -function defaultGetStore() { - if (!defaultGetStoreFunc) { - defaultGetStoreFunc = createStore('keyval-store', 'keyval'); - } - - return defaultGetStoreFunc; -} -/** - * Get a value by its key. - * - * @param key - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function get(key) { - var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); - return customStore('readonly', function (store) { - return promisifyRequest(store.get(key)); - }); -} -/** - * Set a value with a key. - * - * @param key - * @param value - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function set(key, value) { - var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore(); - return customStore('readwrite', function (store) { - store.put(value, key); - return promisifyRequest(store.transaction); - }); -} -/** - * Set multiple values at once. This is faster than calling set() multiple times. - * It's also atomic – if one of the pairs can't be added, none will be added. - * - * @param entries Array of entries, where each entry is an array of `[key, value]`. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function setMany(entries) { - var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); - return customStore('readwrite', function (store) { - entries.forEach(function (entry) { - return store.put(entry[1], entry[0]); - }); - return promisifyRequest(store.transaction); - }); -} -/** - * Get multiple values by their keys - * - * @param keys - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function getMany(keys) { - var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); - return customStore('readonly', function (store) { - return Promise.all(keys.map(function (key) { - return promisifyRequest(store.get(key)); - })); - }); -} -/** - * Update a value. This lets you see the old value and update it as an atomic operation. - * - * @param key - * @param updater A callback that takes the old value and returns a new value. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function update(key, updater) { - var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore(); - return customStore('readwrite', function (store) { - return (// Need to create the promise manually. - // If I try to chain promises, the transaction closes in browsers - // that use a promise polyfill (IE10/11). - new Promise(function (resolve, reject) { - store.get(key).onsuccess = function () { - try { - store.put(updater(this.result), key); - resolve(promisifyRequest(store.transaction)); - } catch (err) { - reject(err); - } - }; - }) - ); - }); -} -/** - * Delete a particular key from the store. - * - * @param key - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function del(key) { - var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); - return customStore('readwrite', function (store) { - store.delete(key); - return promisifyRequest(store.transaction); - }); -} -/** - * Delete multiple keys at once. - * - * @param keys List of keys to delete. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function delMany(keys) { - var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); - return customStore('readwrite', function (store) { - keys.forEach(function (key) { - return store.delete(key); - }); - return promisifyRequest(store.transaction); - }); -} -/** - * Clear all values in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function clear() { - var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); - return customStore('readwrite', function (store) { - store.clear(); - return promisifyRequest(store.transaction); - }); -} - -function eachCursor(store, callback) { - store.openCursor().onsuccess = function () { - if (!this.result) return; - callback(this.result); - this.result.continue(); - }; - - return promisifyRequest(store.transaction); -} -/** - * Get all keys in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function keys() { - var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); - return customStore('readonly', function (store) { - // Fast path for modern browsers - if (store.getAllKeys) { - return promisifyRequest(store.getAllKeys()); - } - - var items = []; - return eachCursor(store, function (cursor) { - return items.push(cursor.key); - }).then(function () { - return items; - }); - }); -} -/** - * Get all values in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function values() { - var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); - return customStore('readonly', function (store) { - // Fast path for modern browsers - if (store.getAll) { - return promisifyRequest(store.getAll()); - } - - var items = []; - return eachCursor(store, function (cursor) { - return items.push(cursor.value); - }).then(function () { - return items; - }); - }); -} -/** - * Get all entries in the store. Each entry is an array of `[key, value]`. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function entries() { - var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); - return customStore('readonly', function (store) { - // Fast path for modern browsers - // (although, hopefully we'll get a simpler path some day) - if (store.getAll && store.getAllKeys) { - return Promise.all([promisifyRequest(store.getAllKeys()), promisifyRequest(store.getAll())]).then(function (_ref) { - var _ref2 = _slicedToArray(_ref, 2), - keys = _ref2[0], - values = _ref2[1]; - - return keys.map(function (key, i) { - return [key, values[i]]; - }); - }); - } - - var items = []; - return customStore('readonly', function (store) { - return eachCursor(store, function (cursor) { - return items.push([cursor.key, cursor.value]); - }).then(function () { - return items; - }); - }); - }); -} - -exports.clear = clear; -exports.createStore = createStore; -exports.del = del; -exports.delMany = delMany; -exports.entries = entries; -exports.get = get; -exports.getMany = getMany; -exports.keys = keys; -exports.set = set; -exports.setMany = setMany; -exports.update = update; -exports.values = values; diff --git a/dist/compat.d.ts b/dist/compat.d.ts deleted file mode 100644 index 8a72944..0000000 --- a/dist/compat.d.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './'; \ No newline at end of file diff --git a/dist/compat.js b/dist/compat.js deleted file mode 100644 index 4592a92..0000000 --- a/dist/compat.js +++ /dev/null @@ -1,393 +0,0 @@ -function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } - -function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; } - -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } - -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } - -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } - -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } - -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } - -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } - -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); }); }; } - -function promisifyRequest(request) { - return new Promise(function (resolve, reject) { - // @ts-ignore - file size hacks - request.oncomplete = request.onsuccess = function () { - return resolve(request.result); - }; // @ts-ignore - file size hacks - - - request.onabort = request.onerror = function () { - return reject(request.error); - }; - }); -} - -var qq = {}; - -function openDatabase(_x, _x2) { - return _openDatabase2.apply(this, arguments); -} - -function _openDatabase2() { - _openDatabase2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(dbName, storeName) { - var retry, - q, - db, - _args = arguments; - return _regeneratorRuntime().wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - retry = _args.length > 2 && _args[2] !== undefined ? _args[2] : true; - q = qq[dbName] || {}; - - if (!q.promise) { - q.promise = new Promise(function (resolve) { - return q.resolve = resolve; - }); - qq[dbName] = q; - - _openDatabase(dbName, storeName, q.resolve, retry); - } - - _context.next = 5; - return q.promise; - - case 5: - db = _context.sent; - delete q.promise; - return _context.abrupt("return", db); - - case 8: - case "end": - return _context.stop(); - } - } - }, _callee); - })); - return _openDatabase2.apply(this, arguments); -} - -var databases = {}; - -function _openDatabase(_x3, _x4, _x5, _x6) { - return _openDatabase3.apply(this, arguments); -} - -function _openDatabase3() { - _openDatabase3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(dbName, storeName, resolve, retry) { - var request; - return _regeneratorRuntime().wrap(function _callee2$(_context2) { - while (1) { - switch (_context2.prev = _context2.next) { - case 0: - if (databases[dbName]) { - _context2.next = 9; - break; - } - - request = indexedDB.open(dbName); - - request.onupgradeneeded = function () { - return request.result.createObjectStore(storeName); - }; - - _context2.t0 = resolve; - _context2.next = 6; - return promisifyRequest(request); - - case 6: - _context2.t1 = databases[dbName] = _context2.sent; - (0, _context2.t0)(_context2.t1); - return _context2.abrupt("return"); - - case 9: - if (retry) { - _context2.next = 12; - break; - } - - resolve(databases[dbName]); - return _context2.abrupt("return"); - - case 12: - try { - // Basic way to check if the db is open. - databases[dbName].transaction(storeName); - resolve(databases[dbName]); - } catch (err) { - // Log here on purpose. - console.debug("Could not open a transaction on \"".concat(dbName, "\" due to ").concat(err.name, " (").concat(err.message, "). ") + 'Trying to reopen the connection...'); // Try re-open. - - delete databases[dbName]; - - _openDatabase(dbName, storeName, resolve, retry); - } - - case 13: - case "end": - return _context2.stop(); - } - } - }, _callee2); - })); - return _openDatabase3.apply(this, arguments); -} - -function createStore(dbName, storeName) { - return function (txMode, callback) { - return openDatabase(dbName, storeName).then(function (db) { - return callback(db.transaction(storeName, txMode).objectStore(storeName)); - }); - }; -} - -var defaultGetStoreFunc; - -function defaultGetStore() { - if (!defaultGetStoreFunc) { - defaultGetStoreFunc = createStore('keyval-store', 'keyval'); - } - - return defaultGetStoreFunc; -} -/** - * Get a value by its key. - * - * @param key - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function get(key) { - var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); - return customStore('readonly', function (store) { - return promisifyRequest(store.get(key)); - }); -} -/** - * Set a value with a key. - * - * @param key - * @param value - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function set(key, value) { - var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore(); - return customStore('readwrite', function (store) { - store.put(value, key); - return promisifyRequest(store.transaction); - }); -} -/** - * Set multiple values at once. This is faster than calling set() multiple times. - * It's also atomic – if one of the pairs can't be added, none will be added. - * - * @param entries Array of entries, where each entry is an array of `[key, value]`. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function setMany(entries) { - var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); - return customStore('readwrite', function (store) { - entries.forEach(function (entry) { - return store.put(entry[1], entry[0]); - }); - return promisifyRequest(store.transaction); - }); -} -/** - * Get multiple values by their keys - * - * @param keys - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function getMany(keys) { - var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); - return customStore('readonly', function (store) { - return Promise.all(keys.map(function (key) { - return promisifyRequest(store.get(key)); - })); - }); -} -/** - * Update a value. This lets you see the old value and update it as an atomic operation. - * - * @param key - * @param updater A callback that takes the old value and returns a new value. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function update(key, updater) { - var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore(); - return customStore('readwrite', function (store) { - return (// Need to create the promise manually. - // If I try to chain promises, the transaction closes in browsers - // that use a promise polyfill (IE10/11). - new Promise(function (resolve, reject) { - store.get(key).onsuccess = function () { - try { - store.put(updater(this.result), key); - resolve(promisifyRequest(store.transaction)); - } catch (err) { - reject(err); - } - }; - }) - ); - }); -} -/** - * Delete a particular key from the store. - * - * @param key - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function del(key) { - var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); - return customStore('readwrite', function (store) { - store.delete(key); - return promisifyRequest(store.transaction); - }); -} -/** - * Delete multiple keys at once. - * - * @param keys List of keys to delete. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function delMany(keys) { - var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore(); - return customStore('readwrite', function (store) { - keys.forEach(function (key) { - return store.delete(key); - }); - return promisifyRequest(store.transaction); - }); -} -/** - * Clear all values in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function clear() { - var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); - return customStore('readwrite', function (store) { - store.clear(); - return promisifyRequest(store.transaction); - }); -} - -function eachCursor(store, callback) { - store.openCursor().onsuccess = function () { - if (!this.result) return; - callback(this.result); - this.result.continue(); - }; - - return promisifyRequest(store.transaction); -} -/** - * Get all keys in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function keys() { - var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); - return customStore('readonly', function (store) { - // Fast path for modern browsers - if (store.getAllKeys) { - return promisifyRequest(store.getAllKeys()); - } - - var items = []; - return eachCursor(store, function (cursor) { - return items.push(cursor.key); - }).then(function () { - return items; - }); - }); -} -/** - * Get all values in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function values() { - var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); - return customStore('readonly', function (store) { - // Fast path for modern browsers - if (store.getAll) { - return promisifyRequest(store.getAll()); - } - - var items = []; - return eachCursor(store, function (cursor) { - return items.push(cursor.value); - }).then(function () { - return items; - }); - }); -} -/** - * Get all entries in the store. Each entry is an array of `[key, value]`. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ - - -function entries() { - var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore(); - return customStore('readonly', function (store) { - // Fast path for modern browsers - // (although, hopefully we'll get a simpler path some day) - if (store.getAll && store.getAllKeys) { - return Promise.all([promisifyRequest(store.getAllKeys()), promisifyRequest(store.getAll())]).then(function (_ref) { - var _ref2 = _slicedToArray(_ref, 2), - keys = _ref2[0], - values = _ref2[1]; - - return keys.map(function (key, i) { - return [key, values[i]]; - }); - }); - } - - var items = []; - return customStore('readonly', function (store) { - return eachCursor(store, function (cursor) { - return items.push([cursor.key, cursor.value]); - }).then(function () { - return items; - }); - }); - }); -} - -export { clear, createStore, del, delMany, entries, get, getMany, keys, set, setMany, update, values }; diff --git a/dist/index.cjs b/dist/index.cjs deleted file mode 100644 index eb9d4cd..0000000 --- a/dist/index.cjs +++ /dev/null @@ -1,235 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { value: true }); - -function promisifyRequest(request) { - return new Promise((resolve, reject) => { - // @ts-ignore - file size hacks - request.oncomplete = request.onsuccess = () => resolve(request.result); - // @ts-ignore - file size hacks - request.onabort = request.onerror = () => reject(request.error); - }); -} -const qq = {}; -async function openDatabase(dbName, storeName, retry = true) { - const q = qq[dbName] || {}; - if (!q.promise) { - q.promise = new Promise(resolve => q.resolve = resolve); - qq[dbName] = q; - _openDatabase(dbName, storeName, q.resolve, retry); - } - const db = await q.promise; - delete q.promise; - return db; -} -const databases = {}; -async function _openDatabase(dbName, storeName, resolve, retry) { - if (!databases[dbName]) { - const request = indexedDB.open(dbName); - request.onupgradeneeded = () => request.result.createObjectStore(storeName); - resolve(databases[dbName] = await promisifyRequest(request)); - return; - } - if (!retry) { - resolve(databases[dbName]); - return; - } - try { - // Basic way to check if the db is open. - databases[dbName].transaction(storeName); - resolve(databases[dbName]); - } - catch (err) { - // Log here on purpose. - console.debug(`Could not open a transaction on "${dbName}" due to ${err.name} (${err.message}). ` - + 'Trying to reopen the connection...'); - // Try re-open. - delete databases[dbName]; - _openDatabase(dbName, storeName, resolve, retry); - } -} - -function createStore(dbName, storeName) { - return (txMode, callback) => openDatabase(dbName, storeName).then(db => callback(db.transaction(storeName, txMode).objectStore(storeName))); -} -let defaultGetStoreFunc; -function defaultGetStore() { - if (!defaultGetStoreFunc) { - defaultGetStoreFunc = createStore('keyval-store', 'keyval'); - } - return defaultGetStoreFunc; -} -/** - * Get a value by its key. - * - * @param key - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function get(key, customStore = defaultGetStore()) { - return customStore('readonly', (store) => promisifyRequest(store.get(key))); -} -/** - * Set a value with a key. - * - * @param key - * @param value - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function set(key, value, customStore = defaultGetStore()) { - return customStore('readwrite', (store) => { - store.put(value, key); - return promisifyRequest(store.transaction); - }); -} -/** - * Set multiple values at once. This is faster than calling set() multiple times. - * It's also atomic – if one of the pairs can't be added, none will be added. - * - * @param entries Array of entries, where each entry is an array of `[key, value]`. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function setMany(entries, customStore = defaultGetStore()) { - return customStore('readwrite', (store) => { - entries.forEach((entry) => store.put(entry[1], entry[0])); - return promisifyRequest(store.transaction); - }); -} -/** - * Get multiple values by their keys - * - * @param keys - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function getMany(keys, customStore = defaultGetStore()) { - return customStore('readonly', (store) => Promise.all(keys.map((key) => promisifyRequest(store.get(key))))); -} -/** - * Update a value. This lets you see the old value and update it as an atomic operation. - * - * @param key - * @param updater A callback that takes the old value and returns a new value. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function update(key, updater, customStore = defaultGetStore()) { - return customStore('readwrite', (store) => - // Need to create the promise manually. - // If I try to chain promises, the transaction closes in browsers - // that use a promise polyfill (IE10/11). - new Promise((resolve, reject) => { - store.get(key).onsuccess = function () { - try { - store.put(updater(this.result), key); - resolve(promisifyRequest(store.transaction)); - } - catch (err) { - reject(err); - } - }; - })); -} -/** - * Delete a particular key from the store. - * - * @param key - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function del(key, customStore = defaultGetStore()) { - return customStore('readwrite', (store) => { - store.delete(key); - return promisifyRequest(store.transaction); - }); -} -/** - * Delete multiple keys at once. - * - * @param keys List of keys to delete. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function delMany(keys, customStore = defaultGetStore()) { - return customStore('readwrite', (store) => { - keys.forEach((key) => store.delete(key)); - return promisifyRequest(store.transaction); - }); -} -/** - * Clear all values in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function clear(customStore = defaultGetStore()) { - return customStore('readwrite', (store) => { - store.clear(); - return promisifyRequest(store.transaction); - }); -} -function eachCursor(store, callback) { - store.openCursor().onsuccess = function () { - if (!this.result) - return; - callback(this.result); - this.result.continue(); - }; - return promisifyRequest(store.transaction); -} -/** - * Get all keys in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function keys(customStore = defaultGetStore()) { - return customStore('readonly', (store) => { - // Fast path for modern browsers - if (store.getAllKeys) { - return promisifyRequest(store.getAllKeys()); - } - const items = []; - return eachCursor(store, (cursor) => items.push(cursor.key)).then(() => items); - }); -} -/** - * Get all values in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function values(customStore = defaultGetStore()) { - return customStore('readonly', (store) => { - // Fast path for modern browsers - if (store.getAll) { - return promisifyRequest(store.getAll()); - } - const items = []; - return eachCursor(store, (cursor) => items.push(cursor.value)).then(() => items); - }); -} -/** - * Get all entries in the store. Each entry is an array of `[key, value]`. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function entries(customStore = defaultGetStore()) { - return customStore('readonly', (store) => { - // Fast path for modern browsers - // (although, hopefully we'll get a simpler path some day) - if (store.getAll && store.getAllKeys) { - return Promise.all([ - promisifyRequest(store.getAllKeys()), - promisifyRequest(store.getAll()), - ]).then(([keys, values]) => keys.map((key, i) => [key, values[i]])); - } - const items = []; - return customStore('readonly', (store) => eachCursor(store, (cursor) => items.push([cursor.key, cursor.value])).then(() => items)); - }); -} - -exports.clear = clear; -exports.createStore = createStore; -exports.del = del; -exports.delMany = delMany; -exports.entries = entries; -exports.get = get; -exports.getMany = getMany; -exports.keys = keys; -exports.set = set; -exports.setMany = setMany; -exports.update = update; -exports.values = values; diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 37e9493..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,78 +0,0 @@ -export declare function createStore(dbName: string, storeName: string): UseStore; -export declare type UseStore = (txMode: IDBTransactionMode, callback: (store: IDBObjectStore) => T | PromiseLike) => Promise; -/** - * Get a value by its key. - * - * @param key - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function get(key: IDBValidKey, customStore?: UseStore): Promise; -/** - * Set a value with a key. - * - * @param key - * @param value - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function set(key: IDBValidKey, value: any, customStore?: UseStore): Promise; -/** - * Set multiple values at once. This is faster than calling set() multiple times. - * It's also atomic – if one of the pairs can't be added, none will be added. - * - * @param entries Array of entries, where each entry is an array of `[key, value]`. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function setMany(entries: [IDBValidKey, any][], customStore?: UseStore): Promise; -/** - * Get multiple values by their keys - * - * @param keys - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function getMany(keys: IDBValidKey[], customStore?: UseStore): Promise; -/** - * Update a value. This lets you see the old value and update it as an atomic operation. - * - * @param key - * @param updater A callback that takes the old value and returns a new value. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function update(key: IDBValidKey, updater: (oldValue: T | undefined) => T, customStore?: UseStore): Promise; -/** - * Delete a particular key from the store. - * - * @param key - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function del(key: IDBValidKey, customStore?: UseStore): Promise; -/** - * Delete multiple keys at once. - * - * @param keys List of keys to delete. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function delMany(keys: IDBValidKey[], customStore?: UseStore): Promise; -/** - * Clear all values in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function clear(customStore?: UseStore): Promise; -/** - * Get all keys in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function keys(customStore?: UseStore): Promise; -/** - * Get all values in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function values(customStore?: UseStore): Promise; -/** - * Get all entries in the store. Each entry is an array of `[key, value]`. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -export declare function entries(customStore?: UseStore): Promise<[KeyType, ValueType][]>; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 510dd96..0000000 --- a/dist/index.js +++ /dev/null @@ -1,220 +0,0 @@ -function promisifyRequest(request) { - return new Promise((resolve, reject) => { - // @ts-ignore - file size hacks - request.oncomplete = request.onsuccess = () => resolve(request.result); - // @ts-ignore - file size hacks - request.onabort = request.onerror = () => reject(request.error); - }); -} -const qq = {}; -async function openDatabase(dbName, storeName, retry = true) { - const q = qq[dbName] || {}; - if (!q.promise) { - q.promise = new Promise(resolve => q.resolve = resolve); - qq[dbName] = q; - _openDatabase(dbName, storeName, q.resolve, retry); - } - const db = await q.promise; - delete q.promise; - return db; -} -const databases = {}; -async function _openDatabase(dbName, storeName, resolve, retry) { - if (!databases[dbName]) { - const request = indexedDB.open(dbName); - request.onupgradeneeded = () => request.result.createObjectStore(storeName); - resolve(databases[dbName] = await promisifyRequest(request)); - return; - } - if (!retry) { - resolve(databases[dbName]); - return; - } - try { - // Basic way to check if the db is open. - databases[dbName].transaction(storeName); - resolve(databases[dbName]); - } - catch (err) { - // Log here on purpose. - console.debug(`Could not open a transaction on "${dbName}" due to ${err.name} (${err.message}). ` - + 'Trying to reopen the connection...'); - // Try re-open. - delete databases[dbName]; - _openDatabase(dbName, storeName, resolve, retry); - } -} - -function createStore(dbName, storeName) { - return (txMode, callback) => openDatabase(dbName, storeName).then(db => callback(db.transaction(storeName, txMode).objectStore(storeName))); -} -let defaultGetStoreFunc; -function defaultGetStore() { - if (!defaultGetStoreFunc) { - defaultGetStoreFunc = createStore('keyval-store', 'keyval'); - } - return defaultGetStoreFunc; -} -/** - * Get a value by its key. - * - * @param key - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function get(key, customStore = defaultGetStore()) { - return customStore('readonly', (store) => promisifyRequest(store.get(key))); -} -/** - * Set a value with a key. - * - * @param key - * @param value - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function set(key, value, customStore = defaultGetStore()) { - return customStore('readwrite', (store) => { - store.put(value, key); - return promisifyRequest(store.transaction); - }); -} -/** - * Set multiple values at once. This is faster than calling set() multiple times. - * It's also atomic – if one of the pairs can't be added, none will be added. - * - * @param entries Array of entries, where each entry is an array of `[key, value]`. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function setMany(entries, customStore = defaultGetStore()) { - return customStore('readwrite', (store) => { - entries.forEach((entry) => store.put(entry[1], entry[0])); - return promisifyRequest(store.transaction); - }); -} -/** - * Get multiple values by their keys - * - * @param keys - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function getMany(keys, customStore = defaultGetStore()) { - return customStore('readonly', (store) => Promise.all(keys.map((key) => promisifyRequest(store.get(key))))); -} -/** - * Update a value. This lets you see the old value and update it as an atomic operation. - * - * @param key - * @param updater A callback that takes the old value and returns a new value. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function update(key, updater, customStore = defaultGetStore()) { - return customStore('readwrite', (store) => - // Need to create the promise manually. - // If I try to chain promises, the transaction closes in browsers - // that use a promise polyfill (IE10/11). - new Promise((resolve, reject) => { - store.get(key).onsuccess = function () { - try { - store.put(updater(this.result), key); - resolve(promisifyRequest(store.transaction)); - } - catch (err) { - reject(err); - } - }; - })); -} -/** - * Delete a particular key from the store. - * - * @param key - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function del(key, customStore = defaultGetStore()) { - return customStore('readwrite', (store) => { - store.delete(key); - return promisifyRequest(store.transaction); - }); -} -/** - * Delete multiple keys at once. - * - * @param keys List of keys to delete. - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function delMany(keys, customStore = defaultGetStore()) { - return customStore('readwrite', (store) => { - keys.forEach((key) => store.delete(key)); - return promisifyRequest(store.transaction); - }); -} -/** - * Clear all values in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function clear(customStore = defaultGetStore()) { - return customStore('readwrite', (store) => { - store.clear(); - return promisifyRequest(store.transaction); - }); -} -function eachCursor(store, callback) { - store.openCursor().onsuccess = function () { - if (!this.result) - return; - callback(this.result); - this.result.continue(); - }; - return promisifyRequest(store.transaction); -} -/** - * Get all keys in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function keys(customStore = defaultGetStore()) { - return customStore('readonly', (store) => { - // Fast path for modern browsers - if (store.getAllKeys) { - return promisifyRequest(store.getAllKeys()); - } - const items = []; - return eachCursor(store, (cursor) => items.push(cursor.key)).then(() => items); - }); -} -/** - * Get all values in the store. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function values(customStore = defaultGetStore()) { - return customStore('readonly', (store) => { - // Fast path for modern browsers - if (store.getAll) { - return promisifyRequest(store.getAll()); - } - const items = []; - return eachCursor(store, (cursor) => items.push(cursor.value)).then(() => items); - }); -} -/** - * Get all entries in the store. Each entry is an array of `[key, value]`. - * - * @param customStore Method to get a custom store. Use with caution (see the docs). - */ -function entries(customStore = defaultGetStore()) { - return customStore('readonly', (store) => { - // Fast path for modern browsers - // (although, hopefully we'll get a simpler path some day) - if (store.getAll && store.getAllKeys) { - return Promise.all([ - promisifyRequest(store.getAllKeys()), - promisifyRequest(store.getAll()), - ]).then(([keys, values]) => keys.map((key, i) => [key, values[i]])); - } - const items = []; - return customStore('readonly', (store) => eachCursor(store, (cursor) => items.push([cursor.key, cursor.value])).then(() => items)); - }); -} - -export { clear, createStore, del, delMany, entries, get, getMany, keys, set, setMany, update, values }; diff --git a/dist/umd.cjs b/dist/umd.cjs deleted file mode 100644 index b2c7fe4..0000000 --- a/dist/umd.cjs +++ /dev/null @@ -1 +0,0 @@ -function _regeneratorRuntime(){"use strict";/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */_regeneratorRuntime=function(){return t};var t={},r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function c(t,r,e,n){var o=r&&r.prototype instanceof s?r:s,i=Object.create(o.prototype),a=new L(n||[]);return i._invoke=function(t,r,e){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return E()}for(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var u=b(a,e);if(u){if(u===l)continue;return u}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var c=f(t,r,e);if("normal"===c.type){if(n=e.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:e.done}}"throw"===c.type&&(n="completed",e.method="throw",e.arg=c.arg)}}}(t,e,a),i}function f(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function s(){}function h(){}function y(){}var p={};u(p,o,(function(){return this}));var d=Object.getPrototypeOf,v=d&&d(d(A([])));v&&v!==r&&e.call(v,o)&&(p=v);var g=y.prototype=s.prototype=Object.create(p);function m(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function w(t,r){function n(o,i,a,u){var c=f(t[o],t,i);if("throw"!==c.type){var l=c.arg,s=l.value;return s&&"object"==_typeof(s)&&e.call(s,"__await")?r.resolve(s.__await).then((function(t){n("next",t,a,u)}),(function(t){n("throw",t,a,u)})):r.resolve(s).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,u)}))}u(c.arg)}var o;this._invoke=function(t,e){function i(){return new r((function(r,o){n(t,e,r,o)}))}return o=o?o.then(i,i):i()}}function b(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,b(t,r),"throw"===r.method))return l;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=f(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,l;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,l):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,l)}function _(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function x(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function A(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function r(){for(;++n=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=e.call(i,"catchLoc"),c=e.call(i,"finallyLoc");if(u&&c){if(this.prev=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),x(e),l}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;x(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:A(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),l}},t}function _slicedToArray(t,r){return _arrayWithHoles(t)||_iterableToArrayLimit(t,r)||_unsupportedIterableToArray(t,r)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(t,r){if(t){if("string"==typeof t)return _arrayLikeToArray(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?_arrayLikeToArray(t,r):void 0}}function _arrayLikeToArray(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e2&&void 0!==c[2])||c[2],(i=e[r]||{}).promise||(i.promise=new Promise((function(t){return i.resolve=t})),e[r]=i,a(r,n,i.resolve,o)),t.next=5,i.promise;case 5:return u=t.sent,delete i.promise,t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)}))),n.apply(this,arguments)}var o,i={};function a(t,r,e,n){return u.apply(this,arguments)}function u(){return(u=_asyncToGenerator(_regeneratorRuntime().mark((function t(e,n,o,u){var c;return _regeneratorRuntime().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(i[e]){t.next=9;break}return(c=indexedDB.open(e)).onupgradeneeded=function(){return c.result.createObjectStore(n)},t.t0=o,t.next=6,r(c);case 6:return t.t1=i[e]=t.sent,(0,t.t0)(t.t1),t.abrupt("return");case 9:if(u){t.next=12;break}return o(i[e]),t.abrupt("return");case 12:try{i[e].transaction(n),o(i[e])}catch(t){console.debug('Could not open a transaction on "'.concat(e,'" due to ').concat(t.name," (").concat(t.message,"). ")+"Trying to reopen the connection..."),delete i[e],a(e,n,o,u)}case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function c(t,r){return function(e,o){return function(t,r){return n.apply(this,arguments)}(t,r).then((function(t){return o(t.transaction(r,e).objectStore(r))}))}}function f(){return o||(o=c("keyval-store","keyval")),o}function l(t,e){return t.openCursor().onsuccess=function(){this.result&&(e(this.result),this.result.continue())},r(t.transaction)}t.clear=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readwrite",(function(t){return t.clear(),r(t.transaction)}))},t.createStore=c,t.del=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return e.delete(t),r(e.transaction)}))},t.delMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return t.forEach((function(t){return e.delete(t)})),r(e.transaction)}))},t.entries=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(e){if(e.getAll&&e.getAllKeys)return Promise.all([r(e.getAllKeys()),r(e.getAll())]).then((function(t){var r=_slicedToArray(t,2),e=r[0],n=r[1];return e.map((function(t,r){return[t,n[r]]}))}));var n=[];return t("readonly",(function(t){return l(t,(function(t){return n.push([t.key,t.value])})).then((function(){return n}))}))}))},t.get=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readonly",(function(e){return r(e.get(t))}))},t.getMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readonly",(function(e){return Promise.all(t.map((function(t){return r(e.get(t))})))}))},t.keys=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(t){if(t.getAllKeys)return r(t.getAllKeys());var e=[];return l(t,(function(t){return e.push(t.key)})).then((function(){return e}))}))},t.set=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:f();return n("readwrite",(function(n){return n.put(e,t),r(n.transaction)}))},t.setMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return t.forEach((function(t){return e.put(t[1],t[0])})),r(e.transaction)}))},t.update=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:f();return n("readwrite",(function(n){return new Promise((function(o,i){n.get(t).onsuccess=function(){try{n.put(e(this.result),t),o(r(n.transaction))}catch(t){i(t)}}}))}))},t.values=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(t){if(t.getAll)return r(t.getAll());var e=[];return l(t,(function(t){return e.push(t.value)})).then((function(){return e}))}))},Object.defineProperty(t,"__esModule",{value:!0})})); diff --git a/dist/umd.d.ts b/dist/umd.d.ts deleted file mode 100644 index 8a72944..0000000 --- a/dist/umd.d.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './'; \ No newline at end of file diff --git a/dist/umd.js b/dist/umd.js deleted file mode 100644 index b2c7fe4..0000000 --- a/dist/umd.js +++ /dev/null @@ -1 +0,0 @@ -function _regeneratorRuntime(){"use strict";/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */_regeneratorRuntime=function(){return t};var t={},r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function c(t,r,e,n){var o=r&&r.prototype instanceof s?r:s,i=Object.create(o.prototype),a=new L(n||[]);return i._invoke=function(t,r,e){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return E()}for(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var u=b(a,e);if(u){if(u===l)continue;return u}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var c=f(t,r,e);if("normal"===c.type){if(n=e.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:e.done}}"throw"===c.type&&(n="completed",e.method="throw",e.arg=c.arg)}}}(t,e,a),i}function f(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function s(){}function h(){}function y(){}var p={};u(p,o,(function(){return this}));var d=Object.getPrototypeOf,v=d&&d(d(A([])));v&&v!==r&&e.call(v,o)&&(p=v);var g=y.prototype=s.prototype=Object.create(p);function m(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function w(t,r){function n(o,i,a,u){var c=f(t[o],t,i);if("throw"!==c.type){var l=c.arg,s=l.value;return s&&"object"==_typeof(s)&&e.call(s,"__await")?r.resolve(s.__await).then((function(t){n("next",t,a,u)}),(function(t){n("throw",t,a,u)})):r.resolve(s).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,u)}))}u(c.arg)}var o;this._invoke=function(t,e){function i(){return new r((function(r,o){n(t,e,r,o)}))}return o=o?o.then(i,i):i()}}function b(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,b(t,r),"throw"===r.method))return l;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=f(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,l;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,l):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,l)}function _(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function x(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function A(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function r(){for(;++n=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=e.call(i,"catchLoc"),c=e.call(i,"finallyLoc");if(u&&c){if(this.prev=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),x(e),l}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;x(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:A(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),l}},t}function _slicedToArray(t,r){return _arrayWithHoles(t)||_iterableToArrayLimit(t,r)||_unsupportedIterableToArray(t,r)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(t,r){if(t){if("string"==typeof t)return _arrayLikeToArray(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?_arrayLikeToArray(t,r):void 0}}function _arrayLikeToArray(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e2&&void 0!==c[2])||c[2],(i=e[r]||{}).promise||(i.promise=new Promise((function(t){return i.resolve=t})),e[r]=i,a(r,n,i.resolve,o)),t.next=5,i.promise;case 5:return u=t.sent,delete i.promise,t.abrupt("return",u);case 8:case"end":return t.stop()}}),t)}))),n.apply(this,arguments)}var o,i={};function a(t,r,e,n){return u.apply(this,arguments)}function u(){return(u=_asyncToGenerator(_regeneratorRuntime().mark((function t(e,n,o,u){var c;return _regeneratorRuntime().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(i[e]){t.next=9;break}return(c=indexedDB.open(e)).onupgradeneeded=function(){return c.result.createObjectStore(n)},t.t0=o,t.next=6,r(c);case 6:return t.t1=i[e]=t.sent,(0,t.t0)(t.t1),t.abrupt("return");case 9:if(u){t.next=12;break}return o(i[e]),t.abrupt("return");case 12:try{i[e].transaction(n),o(i[e])}catch(t){console.debug('Could not open a transaction on "'.concat(e,'" due to ').concat(t.name," (").concat(t.message,"). ")+"Trying to reopen the connection..."),delete i[e],a(e,n,o,u)}case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function c(t,r){return function(e,o){return function(t,r){return n.apply(this,arguments)}(t,r).then((function(t){return o(t.transaction(r,e).objectStore(r))}))}}function f(){return o||(o=c("keyval-store","keyval")),o}function l(t,e){return t.openCursor().onsuccess=function(){this.result&&(e(this.result),this.result.continue())},r(t.transaction)}t.clear=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readwrite",(function(t){return t.clear(),r(t.transaction)}))},t.createStore=c,t.del=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return e.delete(t),r(e.transaction)}))},t.delMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return t.forEach((function(t){return e.delete(t)})),r(e.transaction)}))},t.entries=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(e){if(e.getAll&&e.getAllKeys)return Promise.all([r(e.getAllKeys()),r(e.getAll())]).then((function(t){var r=_slicedToArray(t,2),e=r[0],n=r[1];return e.map((function(t,r){return[t,n[r]]}))}));var n=[];return t("readonly",(function(t){return l(t,(function(t){return n.push([t.key,t.value])})).then((function(){return n}))}))}))},t.get=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readonly",(function(e){return r(e.get(t))}))},t.getMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readonly",(function(e){return Promise.all(t.map((function(t){return r(e.get(t))})))}))},t.keys=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(t){if(t.getAllKeys)return r(t.getAllKeys());var e=[];return l(t,(function(t){return e.push(t.key)})).then((function(){return e}))}))},t.set=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:f();return n("readwrite",(function(n){return n.put(e,t),r(n.transaction)}))},t.setMany=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f();return e("readwrite",(function(e){return t.forEach((function(t){return e.put(t[1],t[0])})),r(e.transaction)}))},t.update=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:f();return n("readwrite",(function(n){return new Promise((function(o,i){n.get(t).onsuccess=function(){try{n.put(e(this.result),t),o(r(n.transaction))}catch(t){i(t)}}}))}))},t.values=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:f();return t("readonly",(function(t){if(t.getAll)return r(t.getAll());var e=[];return l(t,(function(t){return e.push(t.value)})).then((function(){return e}))}))},Object.defineProperty(t,"__esModule",{value:!0})})); diff --git a/dist/util.d.ts b/dist/util.d.ts deleted file mode 100644 index 99c1b7b..0000000 --- a/dist/util.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -export declare function promisifyRequest(request: IDBRequest | IDBTransaction): Promise; -export declare function openDatabase(dbName: string, storeName: string, retry?: boolean): Promise; -export declare function closeDatabase(dbName: string): Promise;