@dumbmatter/idb 7.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 +87 -0
- package/LICENSE +6 -0
- package/README.md +505 -0
- package/build/async-iterators.cjs +57 -0
- package/build/async-iterators.d.ts +1 -0
- package/build/async-iterators.js +55 -0
- package/build/database-extras.d.ts +1 -0
- package/build/entry.d.ts +629 -0
- package/build/index.cjs +94 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +88 -0
- package/build/umd-with-async-ittr.js +1 -0
- package/build/umd.js +1 -0
- package/build/util.d.ts +3 -0
- package/build/wrap-idb-value.cjs +191 -0
- package/build/wrap-idb-value.d.ts +34 -0
- package/build/wrap-idb-value.js +185 -0
- package/package.json +55 -0
- package/with-async-ittr.cjs +2 -0
- package/with-async-ittr.d.ts +1 -0
- package/with-async-ittr.js +2 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { r as replaceTraps, i as instanceOfAny, a as reverseTransformCache, u as unwrap } from './wrap-idb-value.js';
|
|
2
|
+
|
|
3
|
+
const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];
|
|
4
|
+
const methodMap = {};
|
|
5
|
+
const advanceResults = new WeakMap();
|
|
6
|
+
const ittrProxiedCursorToOriginalProxy = new WeakMap();
|
|
7
|
+
const cursorIteratorTraps = {
|
|
8
|
+
get(target, prop) {
|
|
9
|
+
if (!advanceMethodProps.includes(prop))
|
|
10
|
+
return target[prop];
|
|
11
|
+
let cachedFunc = methodMap[prop];
|
|
12
|
+
if (!cachedFunc) {
|
|
13
|
+
cachedFunc = methodMap[prop] = function (...args) {
|
|
14
|
+
advanceResults.set(this, ittrProxiedCursorToOriginalProxy.get(this)[prop](...args));
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return cachedFunc;
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
async function* iterate(...args) {
|
|
21
|
+
// tslint:disable-next-line:no-this-assignment
|
|
22
|
+
let cursor = this;
|
|
23
|
+
if (!(cursor instanceof IDBCursor)) {
|
|
24
|
+
cursor = await cursor.openCursor(...args);
|
|
25
|
+
}
|
|
26
|
+
if (!cursor)
|
|
27
|
+
return;
|
|
28
|
+
cursor = cursor;
|
|
29
|
+
const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);
|
|
30
|
+
ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);
|
|
31
|
+
// Map this double-proxy back to the original, so other cursor methods work.
|
|
32
|
+
reverseTransformCache.set(proxiedCursor, unwrap(cursor));
|
|
33
|
+
while (cursor) {
|
|
34
|
+
yield proxiedCursor;
|
|
35
|
+
// If one of the advancing methods was not called, call continue().
|
|
36
|
+
cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());
|
|
37
|
+
advanceResults.delete(proxiedCursor);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function isIteratorProp(target, prop) {
|
|
41
|
+
return ((prop === Symbol.asyncIterator &&
|
|
42
|
+
instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||
|
|
43
|
+
(prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore])));
|
|
44
|
+
}
|
|
45
|
+
replaceTraps((oldTraps) => ({
|
|
46
|
+
...oldTraps,
|
|
47
|
+
get(target, prop, receiver) {
|
|
48
|
+
if (isIteratorProp(target, prop))
|
|
49
|
+
return iterate;
|
|
50
|
+
return oldTraps.get(target, prop, receiver);
|
|
51
|
+
},
|
|
52
|
+
has(target, prop) {
|
|
53
|
+
return isIteratorProp(target, prop) || oldTraps.has(target, prop);
|
|
54
|
+
},
|
|
55
|
+
}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|