@dumbmatter/idb 7.0.0 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +21 -24
- package/build/entry.d.ts +19 -7
- package/build/index.cjs +231 -13
- package/build/index.d.ts +3 -2
- package/build/index.js +227 -10
- package/build/umd.js +1 -1
- package/build/wrap-idb-value.d.ts +1 -1
- package/package.json +16 -19
- package/build/async-iterators.cjs +0 -57
- package/build/async-iterators.js +0 -55
- package/build/umd-with-async-ittr.js +0 -1
- package/build/wrap-idb-value.cjs +0 -191
- package/build/wrap-idb-value.js +0 -185
- package/with-async-ittr.cjs +0 -2
- package/with-async-ittr.d.ts +0 -1
- package/with-async-ittr.js +0 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
# Breaking changes in 8.x
|
|
2
|
+
|
|
3
|
+
- Finally dropped support for old EdgeHTML engine.
|
|
4
|
+
- Dropped support for browsers that don't support [`cursor.request`](https://caniuse.com/mdn-api_idbcursor_request).
|
|
5
|
+
- Removed separate async iterators build. It's now one build with async iterator support.
|
|
6
|
+
|
|
1
7
|
# Breaking changes in 7.x
|
|
2
8
|
|
|
3
9
|
- No longer committing `build` to GitHub.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# IndexedDB with usability.
|
|
2
2
|
|
|
3
|
-
This is a tiny (~1.
|
|
3
|
+
This is a tiny (~1.19kB brotli'd) library that mostly mirrors the IndexedDB API, but with small improvements that make a big difference to usability.
|
|
4
4
|
|
|
5
5
|
1. [Installation](#installation)
|
|
6
6
|
1. [Changes](#changes)
|
|
@@ -42,7 +42,7 @@ async function doDatabaseStuff() {
|
|
|
42
42
|
|
|
43
43
|
```html
|
|
44
44
|
<script type="module">
|
|
45
|
-
import { openDB, deleteDB, wrap, unwrap } from 'https://cdn.jsdelivr.net/npm/idb@
|
|
45
|
+
import { openDB, deleteDB, wrap, unwrap } from 'https://cdn.jsdelivr.net/npm/idb@8/+esm';
|
|
46
46
|
|
|
47
47
|
async function doDatabaseStuff() {
|
|
48
48
|
const db = await openDB(…);
|
|
@@ -53,7 +53,7 @@ async function doDatabaseStuff() {
|
|
|
53
53
|
### Using external script reference
|
|
54
54
|
|
|
55
55
|
```html
|
|
56
|
-
<script src="https://cdn.jsdelivr.net/npm/idb@
|
|
56
|
+
<script src="https://cdn.jsdelivr.net/npm/idb@8/build/umd.js"></script>
|
|
57
57
|
<script>
|
|
58
58
|
async function doDatabaseStuff() {
|
|
59
59
|
const db = await idb.openDB(…);
|
|
@@ -71,8 +71,6 @@ A global, `idb`, will be created, containing all exports of the module version.
|
|
|
71
71
|
|
|
72
72
|
This library targets modern browsers, as in Chrome, Firefox, Safari, and other browsers that use those engines, such as Edge. IE is not supported.
|
|
73
73
|
|
|
74
|
-
If you want to target much older versions of those browsers, you can transpile the library using something like [Babel](https://babeljs.io/). You can't transpile the library for IE, as it relies on a proper implementation of [JavaScript proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy).
|
|
75
|
-
|
|
76
74
|
# API
|
|
77
75
|
|
|
78
76
|
## `openDB`
|
|
@@ -81,13 +79,13 @@ This method opens a database, and returns a promise for an enhanced [`IDBDatabas
|
|
|
81
79
|
|
|
82
80
|
```js
|
|
83
81
|
const db = await openDB(name, version, {
|
|
84
|
-
upgrade(db, oldVersion, newVersion, transaction) {
|
|
82
|
+
upgrade(db, oldVersion, newVersion, transaction, event) {
|
|
85
83
|
// …
|
|
86
84
|
},
|
|
87
|
-
blocked() {
|
|
85
|
+
blocked(currentVersion, blockedVersion, event) {
|
|
88
86
|
// …
|
|
89
87
|
},
|
|
90
|
-
blocking() {
|
|
88
|
+
blocking(currentVersion, blockedVersion, event) {
|
|
91
89
|
// …
|
|
92
90
|
},
|
|
93
91
|
terminated() {
|
|
@@ -103,8 +101,15 @@ const db = await openDB(name, version, {
|
|
|
103
101
|
- `oldVersion`: Last version of the database opened by the user.
|
|
104
102
|
- `newVersion`: Whatever new version you provided.
|
|
105
103
|
- `transaction`: An enhanced transaction for this upgrade. This is useful if you need to get data from other stores as part of a migration.
|
|
104
|
+
- `event`: The event object for the associated `upgradeneeded` event.
|
|
106
105
|
- `blocked` (optional): Called if there are older versions of the database open on the origin, so this version cannot open. This is similar to the [`blocked` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBOpenDBRequest/blocked_event) in plain IndexedDB.
|
|
106
|
+
- `currentVersion`: Version of the database that's blocking this one.
|
|
107
|
+
- `blockedVersion`: The version of the database being blocked (whatever version you provided to `openDB`).
|
|
108
|
+
- `event`: The event object for the associated `blocked` event.
|
|
107
109
|
- `blocking` (optional): Called if this connection is blocking a future version of the database from opening. This is similar to the [`versionchange` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/versionchange_event) in plain IndexedDB.
|
|
110
|
+
- `currentVersion`: Version of the open database (whatever version you provided to `openDB`).
|
|
111
|
+
- `blockedVersion`: The version of the database that's being blocked.
|
|
112
|
+
- `event`: The event object for the associated `versionchange` event.
|
|
108
113
|
- `terminated` (optional): Called if the browser abnormally terminates the connection, but not on regular closures like calling `db.close()`. This is similar to the [`close` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/close_event) in plain IndexedDB.
|
|
109
114
|
|
|
110
115
|
## `deleteDB`
|
|
@@ -121,6 +126,8 @@ await deleteDB(name, {
|
|
|
121
126
|
|
|
122
127
|
- `name`: Name of the database.
|
|
123
128
|
- `blocked` (optional): Called if the database already exists and there are open connections that don’t close in response to a versionchange event, the request will be blocked until they all close.
|
|
129
|
+
- `currentVersion`: Version of the database that's blocking the delete operation.
|
|
130
|
+
- `event`: The event object for the associated 'versionchange' event.
|
|
124
131
|
|
|
125
132
|
## `unwrap`
|
|
126
133
|
|
|
@@ -142,8 +149,6 @@ const wrapped = wrap(unwrapped);
|
|
|
142
149
|
|
|
143
150
|
This is useful if some third party code gives you an `IDBDatabase` object and you want it to have the features of this library.
|
|
144
151
|
|
|
145
|
-
This doesn't work with `IDBCursor`, [due to missing primitives](https://github.com/w3c/IndexedDB/issues/255). Also, if you wrap an `IDBTransaction`, `tx.store` and `tx.objectStoreNames` won't work in Edge. To avoid these issues, wrap the `IDBDatabase` object, and use the wrapped object to create a new transaction.
|
|
146
|
-
|
|
147
152
|
## General enhancements
|
|
148
153
|
|
|
149
154
|
Once you've opened the database the API is the same as IndexedDB, except for a few changes to make things easier.
|
|
@@ -261,15 +266,7 @@ while (cursor) {
|
|
|
261
266
|
|
|
262
267
|
## Async iterators
|
|
263
268
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
```js
|
|
267
|
-
import { openDB } from 'idb/with-async-ittr';
|
|
268
|
-
```
|
|
269
|
-
|
|
270
|
-
Or `https://cdn.jsdelivr.net/npm/idb@7/build/umd-with-async-ittr.js` if you're using the non-module version.
|
|
271
|
-
|
|
272
|
-
Now you can iterate over stores, indexes, and cursors:
|
|
269
|
+
You can iterate over stores, indexes, and cursors:
|
|
273
270
|
|
|
274
271
|
```js
|
|
275
272
|
const tx = db.transaction(storeName);
|
|
@@ -320,19 +317,19 @@ const dbPromise = openDB('keyval-store', 1, {
|
|
|
320
317
|
|
|
321
318
|
export async function get(key) {
|
|
322
319
|
return (await dbPromise).get('keyval', key);
|
|
323
|
-
}
|
|
320
|
+
}
|
|
324
321
|
export async function set(key, val) {
|
|
325
322
|
return (await dbPromise).put('keyval', val, key);
|
|
326
|
-
}
|
|
323
|
+
}
|
|
327
324
|
export async function del(key) {
|
|
328
325
|
return (await dbPromise).delete('keyval', key);
|
|
329
|
-
}
|
|
326
|
+
}
|
|
330
327
|
export async function clear() {
|
|
331
328
|
return (await dbPromise).clear('keyval');
|
|
332
|
-
}
|
|
329
|
+
}
|
|
333
330
|
export async function keys() {
|
|
334
331
|
return (await dbPromise).getAllKeys('keyval');
|
|
335
|
-
}
|
|
332
|
+
}
|
|
336
333
|
```
|
|
337
334
|
|
|
338
335
|
## Article store
|
package/build/entry.d.ts
CHANGED
|
@@ -6,19 +6,28 @@ export interface OpenDBCallbacks<DBTypes extends DBSchema | unknown> {
|
|
|
6
6
|
* @param database A database instance that you can use to add/remove stores and indexes.
|
|
7
7
|
* @param oldVersion Last version of the database opened by the user.
|
|
8
8
|
* @param newVersion Whatever new version you provided.
|
|
9
|
-
* @param transaction The transaction for this upgrade.
|
|
10
|
-
* from other stores as part of a migration.
|
|
9
|
+
* @param transaction The transaction for this upgrade.
|
|
10
|
+
* This is useful if you need to get data from other stores as part of a migration.
|
|
11
|
+
* @param event The event object for the associated 'upgradeneeded' event.
|
|
11
12
|
*/
|
|
12
|
-
upgrade?(database: IDBPDatabase<DBTypes>, oldVersion: number, newVersion: number | null, transaction: IDBPTransaction<DBTypes, StoreNames<DBTypes>[], 'versionchange'
|
|
13
|
+
upgrade?(database: IDBPDatabase<DBTypes>, oldVersion: number, newVersion: number | null, transaction: IDBPTransaction<DBTypes, StoreNames<DBTypes>[], 'versionchange'>, event: IDBVersionChangeEvent): void;
|
|
13
14
|
/**
|
|
14
15
|
* Called if there are older versions of the database open on the origin, so this version cannot
|
|
15
16
|
* open.
|
|
17
|
+
*
|
|
18
|
+
* @param currentVersion Version of the database that's blocking this one.
|
|
19
|
+
* @param blockedVersion The version of the database being blocked (whatever version you provided to `openDB`).
|
|
20
|
+
* @param event The event object for the associated `blocked` event.
|
|
16
21
|
*/
|
|
17
|
-
blocked?(): void;
|
|
22
|
+
blocked?(currentVersion: number, blockedVersion: number | null, event: IDBVersionChangeEvent): void;
|
|
18
23
|
/**
|
|
19
24
|
* Called if this connection is blocking a future version of the database from opening.
|
|
25
|
+
*
|
|
26
|
+
* @param currentVersion Version of the open database (whatever version you provided to `openDB`).
|
|
27
|
+
* @param blockedVersion The version of the database that's being blocked.
|
|
28
|
+
* @param event The event object for the associated `versionchange` event.
|
|
20
29
|
*/
|
|
21
|
-
blocking?(): void;
|
|
30
|
+
blocking?(currentVersion: number, blockedVersion: number | null, event: IDBVersionChangeEvent): void;
|
|
22
31
|
/**
|
|
23
32
|
* Called if the browser abnormally terminates the connection.
|
|
24
33
|
* This is not called when `db.close()` is called.
|
|
@@ -36,8 +45,11 @@ export declare function openDB<DBTypes extends DBSchema | unknown = unknown>(nam
|
|
|
36
45
|
export interface DeleteDBCallbacks {
|
|
37
46
|
/**
|
|
38
47
|
* Called if there are connections to this database open, so it cannot be deleted.
|
|
48
|
+
*
|
|
49
|
+
* @param currentVersion Version of the database that's blocking the delete operation.
|
|
50
|
+
* @param event The event object for the associated `blocked` event.
|
|
39
51
|
*/
|
|
40
|
-
blocked?(): void;
|
|
52
|
+
blocked?(currentVersion: number, event: IDBVersionChangeEvent): void;
|
|
41
53
|
}
|
|
42
54
|
/**
|
|
43
55
|
* Delete a database.
|
|
@@ -45,7 +57,7 @@ export interface DeleteDBCallbacks {
|
|
|
45
57
|
* @param name Name of the database.
|
|
46
58
|
*/
|
|
47
59
|
export declare function deleteDB(name: string, { blocked }?: DeleteDBCallbacks): Promise<void>;
|
|
48
|
-
export { unwrap, wrap } from './wrap-idb-value';
|
|
60
|
+
export { unwrap, wrap } from './wrap-idb-value.js';
|
|
49
61
|
declare type KeyToKeyNoIndex<T> = {
|
|
50
62
|
[K in keyof T]: string extends K ? never : number extends K ? never : K;
|
|
51
63
|
};
|
package/build/index.cjs
CHANGED
|
@@ -2,7 +2,164 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const instanceOfAny = (object, constructors) => constructors.some((c) => object instanceof c);
|
|
6
|
+
|
|
7
|
+
let idbProxyableTypes;
|
|
8
|
+
let cursorAdvanceMethods;
|
|
9
|
+
// This is a function to prevent it throwing up in node environments.
|
|
10
|
+
function getIdbProxyableTypes() {
|
|
11
|
+
return (idbProxyableTypes ||
|
|
12
|
+
(idbProxyableTypes = [
|
|
13
|
+
IDBDatabase,
|
|
14
|
+
IDBObjectStore,
|
|
15
|
+
IDBIndex,
|
|
16
|
+
IDBCursor,
|
|
17
|
+
IDBTransaction,
|
|
18
|
+
]));
|
|
19
|
+
}
|
|
20
|
+
// This is a function to prevent it throwing up in node environments.
|
|
21
|
+
function getCursorAdvanceMethods() {
|
|
22
|
+
return (cursorAdvanceMethods ||
|
|
23
|
+
(cursorAdvanceMethods = [
|
|
24
|
+
IDBCursor.prototype.advance,
|
|
25
|
+
IDBCursor.prototype.continue,
|
|
26
|
+
IDBCursor.prototype.continuePrimaryKey,
|
|
27
|
+
]));
|
|
28
|
+
}
|
|
29
|
+
const transactionDoneMap = new WeakMap();
|
|
30
|
+
const transformCache = new WeakMap();
|
|
31
|
+
const reverseTransformCache = new WeakMap();
|
|
32
|
+
function promisifyRequest(request) {
|
|
33
|
+
const promise = new Promise((resolve, reject) => {
|
|
34
|
+
const unlisten = () => {
|
|
35
|
+
request.removeEventListener('success', success);
|
|
36
|
+
request.removeEventListener('error', error);
|
|
37
|
+
};
|
|
38
|
+
const success = () => {
|
|
39
|
+
resolve(wrap(request.result));
|
|
40
|
+
unlisten();
|
|
41
|
+
};
|
|
42
|
+
const error = () => {
|
|
43
|
+
reject(request.error);
|
|
44
|
+
unlisten();
|
|
45
|
+
};
|
|
46
|
+
request.addEventListener('success', success);
|
|
47
|
+
request.addEventListener('error', error);
|
|
48
|
+
});
|
|
49
|
+
// This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
|
|
50
|
+
// is because we create many promises from a single IDBRequest.
|
|
51
|
+
reverseTransformCache.set(promise, request);
|
|
52
|
+
return promise;
|
|
53
|
+
}
|
|
54
|
+
function cacheDonePromiseForTransaction(tx) {
|
|
55
|
+
// Early bail if we've already created a done promise for this transaction.
|
|
56
|
+
if (transactionDoneMap.has(tx))
|
|
57
|
+
return;
|
|
58
|
+
const done = new Promise((resolve, reject) => {
|
|
59
|
+
const unlisten = () => {
|
|
60
|
+
tx.removeEventListener('complete', complete);
|
|
61
|
+
tx.removeEventListener('error', error);
|
|
62
|
+
tx.removeEventListener('abort', error);
|
|
63
|
+
};
|
|
64
|
+
const complete = () => {
|
|
65
|
+
resolve();
|
|
66
|
+
unlisten();
|
|
67
|
+
};
|
|
68
|
+
const error = () => {
|
|
69
|
+
reject(tx.error || new DOMException('AbortError', 'AbortError'));
|
|
70
|
+
unlisten();
|
|
71
|
+
};
|
|
72
|
+
tx.addEventListener('complete', complete);
|
|
73
|
+
tx.addEventListener('error', error);
|
|
74
|
+
tx.addEventListener('abort', error);
|
|
75
|
+
});
|
|
76
|
+
// Cache it for later retrieval.
|
|
77
|
+
transactionDoneMap.set(tx, done);
|
|
78
|
+
}
|
|
79
|
+
let idbProxyTraps = {
|
|
80
|
+
get(target, prop, receiver) {
|
|
81
|
+
if (target instanceof IDBTransaction) {
|
|
82
|
+
// Special handling for transaction.done.
|
|
83
|
+
if (prop === 'done')
|
|
84
|
+
return transactionDoneMap.get(target);
|
|
85
|
+
// Make tx.store return the only store in the transaction, or undefined if there are many.
|
|
86
|
+
if (prop === 'store') {
|
|
87
|
+
return receiver.objectStoreNames[1]
|
|
88
|
+
? undefined
|
|
89
|
+
: receiver.objectStore(receiver.objectStoreNames[0]);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Else transform whatever we get back.
|
|
93
|
+
return wrap(target[prop]);
|
|
94
|
+
},
|
|
95
|
+
set(target, prop, value) {
|
|
96
|
+
target[prop] = value;
|
|
97
|
+
return true;
|
|
98
|
+
},
|
|
99
|
+
has(target, prop) {
|
|
100
|
+
if (target instanceof IDBTransaction &&
|
|
101
|
+
(prop === 'done' || prop === 'store')) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
return prop in target;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
function replaceTraps(callback) {
|
|
108
|
+
idbProxyTraps = callback(idbProxyTraps);
|
|
109
|
+
}
|
|
110
|
+
function wrapFunction(func) {
|
|
111
|
+
// Due to expected object equality (which is enforced by the caching in `wrap`), we
|
|
112
|
+
// only create one new func per func.
|
|
113
|
+
// Cursor methods are special, as the behaviour is a little more different to standard IDB. In
|
|
114
|
+
// IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
|
|
115
|
+
// cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
|
|
116
|
+
// with real promises, so each advance methods returns a new promise for the cursor object, or
|
|
117
|
+
// undefined if the end of the cursor has been reached.
|
|
118
|
+
if (getCursorAdvanceMethods().includes(func)) {
|
|
119
|
+
return function (...args) {
|
|
120
|
+
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
|
|
121
|
+
// the original object.
|
|
122
|
+
func.apply(unwrap(this), args);
|
|
123
|
+
return wrap(this.request);
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return function (...args) {
|
|
127
|
+
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
|
|
128
|
+
// the original object.
|
|
129
|
+
return wrap(func.apply(unwrap(this), args));
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function transformCachableValue(value) {
|
|
133
|
+
if (typeof value === 'function')
|
|
134
|
+
return wrapFunction(value);
|
|
135
|
+
// This doesn't return, it just creates a 'done' promise for the transaction,
|
|
136
|
+
// which is later returned for transaction.done (see idbObjectHandler).
|
|
137
|
+
if (value instanceof IDBTransaction)
|
|
138
|
+
cacheDonePromiseForTransaction(value);
|
|
139
|
+
if (instanceOfAny(value, getIdbProxyableTypes()))
|
|
140
|
+
return new Proxy(value, idbProxyTraps);
|
|
141
|
+
// Return the same value back if we're not going to transform it.
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
144
|
+
function wrap(value) {
|
|
145
|
+
// We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
|
|
146
|
+
// IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
|
|
147
|
+
if (value instanceof IDBRequest)
|
|
148
|
+
return promisifyRequest(value);
|
|
149
|
+
// If we've already transformed this value before, reuse the transformed value.
|
|
150
|
+
// This is faster, but it also provides object equality.
|
|
151
|
+
if (transformCache.has(value))
|
|
152
|
+
return transformCache.get(value);
|
|
153
|
+
const newValue = transformCachableValue(value);
|
|
154
|
+
// Not all types are transformed.
|
|
155
|
+
// These may be primitive types, so they can't be WeakMap keys.
|
|
156
|
+
if (newValue !== value) {
|
|
157
|
+
transformCache.set(value, newValue);
|
|
158
|
+
reverseTransformCache.set(newValue, value);
|
|
159
|
+
}
|
|
160
|
+
return newValue;
|
|
161
|
+
}
|
|
162
|
+
const unwrap = (value) => reverseTransformCache.get(value);
|
|
6
163
|
|
|
7
164
|
/**
|
|
8
165
|
* Open a database.
|
|
@@ -13,20 +170,24 @@ var wrapIdbValue = require('./wrap-idb-value.cjs');
|
|
|
13
170
|
*/
|
|
14
171
|
function openDB(name, version, { blocked, upgrade, blocking, terminated } = {}) {
|
|
15
172
|
const request = indexedDB.open(name, version);
|
|
16
|
-
const openPromise =
|
|
173
|
+
const openPromise = wrap(request);
|
|
17
174
|
if (upgrade) {
|
|
18
175
|
request.addEventListener('upgradeneeded', (event) => {
|
|
19
|
-
upgrade(
|
|
176
|
+
upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction), event);
|
|
20
177
|
});
|
|
21
178
|
}
|
|
22
|
-
if (blocked)
|
|
23
|
-
request.addEventListener('blocked', () => blocked(
|
|
179
|
+
if (blocked) {
|
|
180
|
+
request.addEventListener('blocked', (event) => blocked(
|
|
181
|
+
// Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
|
|
182
|
+
event.oldVersion, event.newVersion, event));
|
|
183
|
+
}
|
|
24
184
|
openPromise
|
|
25
185
|
.then((db) => {
|
|
26
186
|
if (terminated)
|
|
27
187
|
db.addEventListener('close', () => terminated());
|
|
28
|
-
if (blocking)
|
|
29
|
-
db.addEventListener('versionchange', () => blocking());
|
|
188
|
+
if (blocking) {
|
|
189
|
+
db.addEventListener('versionchange', (event) => blocking(event.oldVersion, event.newVersion, event));
|
|
190
|
+
}
|
|
30
191
|
})
|
|
31
192
|
.catch(() => { });
|
|
32
193
|
return openPromise;
|
|
@@ -38,9 +199,12 @@ function openDB(name, version, { blocked, upgrade, blocking, terminated } = {})
|
|
|
38
199
|
*/
|
|
39
200
|
function deleteDB(name, { blocked } = {}) {
|
|
40
201
|
const request = indexedDB.deleteDatabase(name);
|
|
41
|
-
if (blocked)
|
|
42
|
-
request.addEventListener('blocked', () => blocked(
|
|
43
|
-
|
|
202
|
+
if (blocked) {
|
|
203
|
+
request.addEventListener('blocked', (event) => blocked(
|
|
204
|
+
// Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
|
|
205
|
+
event.oldVersion, event));
|
|
206
|
+
}
|
|
207
|
+
return wrap(request).then(() => undefined);
|
|
44
208
|
}
|
|
45
209
|
|
|
46
210
|
const readMethods = ['get', 'getKey', 'getAll', 'getAllKeys', 'count'];
|
|
@@ -82,13 +246,67 @@ function getMethod(target, prop) {
|
|
|
82
246
|
cachedMethods.set(prop, method);
|
|
83
247
|
return method;
|
|
84
248
|
}
|
|
85
|
-
|
|
249
|
+
replaceTraps((oldTraps) => ({
|
|
86
250
|
...oldTraps,
|
|
87
251
|
get: (target, prop, receiver) => getMethod(target, prop) || oldTraps.get(target, prop, receiver),
|
|
88
252
|
has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop),
|
|
89
253
|
}));
|
|
90
254
|
|
|
91
|
-
|
|
92
|
-
|
|
255
|
+
const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];
|
|
256
|
+
const methodMap = {};
|
|
257
|
+
const advanceResults = new WeakMap();
|
|
258
|
+
const ittrProxiedCursorToOriginalProxy = new WeakMap();
|
|
259
|
+
const cursorIteratorTraps = {
|
|
260
|
+
get(target, prop) {
|
|
261
|
+
if (!advanceMethodProps.includes(prop))
|
|
262
|
+
return target[prop];
|
|
263
|
+
let cachedFunc = methodMap[prop];
|
|
264
|
+
if (!cachedFunc) {
|
|
265
|
+
cachedFunc = methodMap[prop] = function (...args) {
|
|
266
|
+
advanceResults.set(this, ittrProxiedCursorToOriginalProxy.get(this)[prop](...args));
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
return cachedFunc;
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
async function* iterate(...args) {
|
|
273
|
+
// tslint:disable-next-line:no-this-assignment
|
|
274
|
+
let cursor = this;
|
|
275
|
+
if (!(cursor instanceof IDBCursor)) {
|
|
276
|
+
cursor = await cursor.openCursor(...args);
|
|
277
|
+
}
|
|
278
|
+
if (!cursor)
|
|
279
|
+
return;
|
|
280
|
+
cursor = cursor;
|
|
281
|
+
const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);
|
|
282
|
+
ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);
|
|
283
|
+
// Map this double-proxy back to the original, so other cursor methods work.
|
|
284
|
+
reverseTransformCache.set(proxiedCursor, unwrap(cursor));
|
|
285
|
+
while (cursor) {
|
|
286
|
+
yield proxiedCursor;
|
|
287
|
+
// If one of the advancing methods was not called, call continue().
|
|
288
|
+
cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());
|
|
289
|
+
advanceResults.delete(proxiedCursor);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
function isIteratorProp(target, prop) {
|
|
293
|
+
return ((prop === Symbol.asyncIterator &&
|
|
294
|
+
instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||
|
|
295
|
+
(prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore])));
|
|
296
|
+
}
|
|
297
|
+
replaceTraps((oldTraps) => ({
|
|
298
|
+
...oldTraps,
|
|
299
|
+
get(target, prop, receiver) {
|
|
300
|
+
if (isIteratorProp(target, prop))
|
|
301
|
+
return iterate;
|
|
302
|
+
return oldTraps.get(target, prop, receiver);
|
|
303
|
+
},
|
|
304
|
+
has(target, prop) {
|
|
305
|
+
return isIteratorProp(target, prop) || oldTraps.has(target, prop);
|
|
306
|
+
},
|
|
307
|
+
}));
|
|
308
|
+
|
|
93
309
|
exports.deleteDB = deleteDB;
|
|
94
310
|
exports.openDB = openDB;
|
|
311
|
+
exports.unwrap = unwrap;
|
|
312
|
+
exports.wrap = wrap;
|
package/build/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * from './entry';
|
|
2
|
-
import './database-extras';
|
|
1
|
+
export * from './entry.js';
|
|
2
|
+
import './database-extras.js';
|
|
3
|
+
import './async-iterators.js';
|