@aztec/kv-store 1.0.0 → 1.1.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/dest/indexeddb/store.d.ts.map +1 -1
- package/dest/indexeddb/store.js +22 -20
- package/package.json +5 -5
- package/src/indexeddb/store.ts +24 -22
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/indexeddb/store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/indexeddb/store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAGpD,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,YAAY,EAAoB,MAAM,KAAK,CAAC;AAEzE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AACrE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AACrE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAOhE,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,KAAK,IAAI;IACxC,KAAK,EAAE,CAAC,CAAC;IACT,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE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K,EAAE,IAAI,EAAE,MAAM,GAAG,eAAe,CAAC,CAAC,CAAC;IAM5D;;;;OAIG;IACH,aAAa,CAAC,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,mBAAmB,CAAC,CAAC,CAAC;IAMpE;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAuB3D;;OAEG;IACG,KAAK;IAIX,kDAAkD;IAClD,MAAM;IAMN,YAAY,IAAI,OAAO,CAAC,SAAS,CAAC;IAIlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAG9D"}
|
package/dest/indexeddb/store.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SerialQueue } from '@aztec/foundation/queue';
|
|
1
2
|
import { deleteDB, openDB } from 'idb';
|
|
2
3
|
import { IndexedDBAztecArray } from './array.js';
|
|
3
4
|
import { IndexedDBAztecMap } from './map.js';
|
|
@@ -10,13 +11,15 @@ import { IndexedDBAztecSingleton } from './singleton.js';
|
|
|
10
11
|
isEphemeral;
|
|
11
12
|
#rootDB;
|
|
12
13
|
#name;
|
|
13
|
-
#
|
|
14
|
+
#txQueue;
|
|
14
15
|
#containers;
|
|
15
16
|
constructor(rootDB, isEphemeral, name){
|
|
16
17
|
this.isEphemeral = isEphemeral;
|
|
17
18
|
this.#containers = new Set();
|
|
18
19
|
this.#rootDB = rootDB;
|
|
19
20
|
this.#name = name;
|
|
21
|
+
this.#txQueue = new SerialQueue();
|
|
22
|
+
this.#txQueue.start();
|
|
20
23
|
}
|
|
21
24
|
/**
|
|
22
25
|
* Creates a new AztecKVStore backed by IndexedDB. The path to the database is optional. If not provided,
|
|
@@ -116,27 +119,26 @@ import { IndexedDBAztecSingleton } from './singleton.js';
|
|
|
116
119
|
* Runs a callback in a transaction.
|
|
117
120
|
* @param callback - Function to execute in a transaction
|
|
118
121
|
* @returns A promise that resolves to the return value of the callback
|
|
119
|
-
*/
|
|
122
|
+
*/ transactionAsync(callback) {
|
|
120
123
|
// We can only have one transaction at a time for the same store
|
|
121
124
|
// So we need to wait for the current one to finish
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return await runningPromise;
|
|
125
|
+
return this.#txQueue.put(async ()=>{
|
|
126
|
+
const tx = this.#rootDB.transaction('data', 'readwrite');
|
|
127
|
+
for (const container of this.#containers){
|
|
128
|
+
container.db = tx.store;
|
|
129
|
+
}
|
|
130
|
+
// Avoid awaiting this promise so it doesn't get scheduled in the next microtask
|
|
131
|
+
// By then, the tx would be closed
|
|
132
|
+
const runningPromise = callback();
|
|
133
|
+
// Wait for the transaction to finish
|
|
134
|
+
await tx.done;
|
|
135
|
+
for (const container of this.#containers){
|
|
136
|
+
container.db = undefined;
|
|
137
|
+
}
|
|
138
|
+
// Return the result of the callback.
|
|
139
|
+
// Tx is guaranteed to already be closed, so the await doesn't hurt anything here
|
|
140
|
+
return await runningPromise;
|
|
141
|
+
});
|
|
140
142
|
}
|
|
141
143
|
/**
|
|
142
144
|
* Clears all entries in the store & sub DBs.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/kv-store",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/interfaces/index.js",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
"./package.local.json"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@aztec/ethereum": "1.
|
|
28
|
-
"@aztec/foundation": "1.
|
|
29
|
-
"@aztec/native": "1.
|
|
30
|
-
"@aztec/stdlib": "1.
|
|
27
|
+
"@aztec/ethereum": "1.1.0",
|
|
28
|
+
"@aztec/foundation": "1.1.0",
|
|
29
|
+
"@aztec/native": "1.1.0",
|
|
30
|
+
"@aztec/stdlib": "1.1.0",
|
|
31
31
|
"idb": "^8.0.0",
|
|
32
32
|
"lmdb": "^3.2.0",
|
|
33
33
|
"msgpackr": "^1.11.2",
|
package/src/indexeddb/store.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Logger } from '@aztec/foundation/log';
|
|
2
|
+
import { SerialQueue } from '@aztec/foundation/queue';
|
|
2
3
|
|
|
3
|
-
import { type DBSchema, type IDBPDatabase,
|
|
4
|
+
import { type DBSchema, type IDBPDatabase, deleteDB, openDB } from 'idb';
|
|
4
5
|
|
|
5
6
|
import type { AztecAsyncArray } from '../interfaces/array.js';
|
|
6
7
|
import type { Key, StoreSize, Value } from '../interfaces/common.js';
|
|
@@ -40,7 +41,7 @@ export interface AztecIDBSchema extends DBSchema {
|
|
|
40
41
|
export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
41
42
|
#rootDB: IDBPDatabase<AztecIDBSchema>;
|
|
42
43
|
#name: string;
|
|
43
|
-
#
|
|
44
|
+
#txQueue: SerialQueue;
|
|
44
45
|
|
|
45
46
|
#containers = new Set<
|
|
46
47
|
| IndexedDBAztecArray<any>
|
|
@@ -57,6 +58,8 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
57
58
|
) {
|
|
58
59
|
this.#rootDB = rootDB;
|
|
59
60
|
this.#name = name;
|
|
61
|
+
this.#txQueue = new SerialQueue();
|
|
62
|
+
this.#txQueue.start();
|
|
60
63
|
}
|
|
61
64
|
/**
|
|
62
65
|
* Creates a new AztecKVStore backed by IndexedDB. The path to the database is optional. If not provided,
|
|
@@ -153,28 +156,27 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
153
156
|
* @param callback - Function to execute in a transaction
|
|
154
157
|
* @returns A promise that resolves to the return value of the callback
|
|
155
158
|
*/
|
|
156
|
-
|
|
159
|
+
transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
|
|
157
160
|
// We can only have one transaction at a time for the same store
|
|
158
161
|
// So we need to wait for the current one to finish
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
return await runningPromise;
|
|
162
|
+
return this.#txQueue.put(async () => {
|
|
163
|
+
const tx = this.#rootDB.transaction('data', 'readwrite');
|
|
164
|
+
for (const container of this.#containers) {
|
|
165
|
+
container.db = tx.store;
|
|
166
|
+
}
|
|
167
|
+
// Avoid awaiting this promise so it doesn't get scheduled in the next microtask
|
|
168
|
+
// By then, the tx would be closed
|
|
169
|
+
const runningPromise = callback();
|
|
170
|
+
// Wait for the transaction to finish
|
|
171
|
+
await tx.done;
|
|
172
|
+
for (const container of this.#containers) {
|
|
173
|
+
container.db = undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Return the result of the callback.
|
|
177
|
+
// Tx is guaranteed to already be closed, so the await doesn't hurt anything here
|
|
178
|
+
return await runningPromise;
|
|
179
|
+
});
|
|
178
180
|
}
|
|
179
181
|
|
|
180
182
|
/**
|