fake-indexeddb 6.2.1 → 6.2.3
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/auto/index.js +7 -13
- package/auto/index.mjs +7 -13
- package/build/cjs/FDBFactory.js +276 -136
- package/build/cjs/FDBTransaction.js +49 -11
- package/build/cjs/lib/Database.js +21 -12
- package/build/cjs/lib/FakeEventTarget.js +17 -5
- package/build/cjs/lib/Index.js +1 -1
- package/build/cjs/lib/ObjectStore.js +1 -1
- package/build/cjs/lib/binarySearchTree.js +1 -1
- package/build/cjs/lib/extractGetAllOptions.js +2 -2
- package/build/cjs/lib/intersection.js +18 -0
- package/build/cjs/lib/isPotentiallyValidKeyRange.js +13 -20
- package/build/cjs/lib/scheduling.js +4 -4
- package/build/cjs/lib/valueToKey.js +8 -53
- package/build/cjs/lib/valueToKeyWithoutThrowing.js +82 -0
- package/build/esm/FDBFactory.js +276 -136
- package/build/esm/FDBKeyRange.js +0 -1
- package/build/esm/FDBTransaction.js +49 -11
- package/build/esm/forceCloseDatabase.js +0 -1
- package/build/esm/lib/Database.js +21 -13
- package/build/esm/lib/FakeEventTarget.js +17 -5
- package/build/esm/lib/Index.js +1 -2
- package/build/esm/lib/ObjectStore.js +1 -2
- package/build/esm/lib/binarySearchTree.js +1 -2
- package/build/esm/lib/extractGetAllOptions.js +2 -3
- package/build/esm/lib/extractKey.js +0 -1
- package/build/esm/lib/intersection.js +12 -0
- package/build/esm/lib/isPotentiallyValidKeyRange.js +12 -20
- package/build/esm/lib/scheduling.js +4 -4
- package/build/esm/lib/validateKeyPath.js +0 -1
- package/build/esm/lib/valueToKey.js +7 -53
- package/build/esm/lib/valueToKeyWithoutThrowing.js +75 -0
- package/package.json +2 -1
package/build/esm/FDBFactory.js
CHANGED
|
@@ -7,6 +7,15 @@ import enforceRange from "./lib/enforceRange.js";
|
|
|
7
7
|
import { AbortError, VersionError } from "./lib/errors.js";
|
|
8
8
|
import FakeEvent from "./lib/FakeEvent.js";
|
|
9
9
|
import { queueTask } from "./lib/scheduling.js";
|
|
10
|
+
// https://w3c.github.io/IndexedDB/#connection-queue
|
|
11
|
+
const runTaskInConnectionQueue = (connectionQueues, name, task) => {
|
|
12
|
+
// Let queue be the connection queue for storageKey and name.
|
|
13
|
+
// (note FakeIndexedDB does not support storageKeys currently)
|
|
14
|
+
// Add request to queue.
|
|
15
|
+
// Wait until all previous requests in queue have been processed.
|
|
16
|
+
const queue = connectionQueues.get(name) ?? Promise.resolve();
|
|
17
|
+
connectionQueues.set(name, queue.then(task));
|
|
18
|
+
};
|
|
10
19
|
const waitForOthersClosedDelete = (databases, name, openDatabases, cb) => {
|
|
11
20
|
const anyOpen = openDatabases.some(openDatabase2 => {
|
|
12
21
|
return !openDatabase2._closed && !openDatabase2._closePending;
|
|
@@ -19,164 +28,287 @@ const waitForOthersClosedDelete = (databases, name, openDatabases, cb) => {
|
|
|
19
28
|
cb(null);
|
|
20
29
|
};
|
|
21
30
|
|
|
22
|
-
//
|
|
23
|
-
const deleteDatabase = (databases, name, request, cb) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
// https://w3c.github.io/IndexedDB/#delete-a-database
|
|
32
|
+
const deleteDatabase = (databases, connectionQueues, name, request, cb) => {
|
|
33
|
+
const deleteDBTask = () => {
|
|
34
|
+
return new Promise(resolve => {
|
|
35
|
+
const db = databases.get(name);
|
|
36
|
+
const oldVersion = db !== undefined ? db.version : 0;
|
|
37
|
+
const onComplete = err => {
|
|
38
|
+
try {
|
|
39
|
+
if (err) {
|
|
40
|
+
cb(err);
|
|
41
|
+
} else {
|
|
42
|
+
cb(null, oldVersion);
|
|
43
|
+
}
|
|
44
|
+
} finally {
|
|
45
|
+
resolve();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
const db = databases.get(name);
|
|
50
|
+
if (db === undefined) {
|
|
51
|
+
onComplete(null);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Let openConnections be the set of all connections associated with db.
|
|
56
|
+
const openConnections = db.connections.filter(connection => {
|
|
57
|
+
return !connection._closed;
|
|
39
58
|
});
|
|
40
|
-
|
|
59
|
+
|
|
60
|
+
// For each entry of openConnections that does not have its close pending flag set to true, queue a
|
|
61
|
+
// database task to fire a version change event named versionchange at entry with db’s version and null.
|
|
62
|
+
for (const openDatabase2 of openConnections) {
|
|
63
|
+
if (!openDatabase2._closePending) {
|
|
64
|
+
queueTask(() => {
|
|
65
|
+
const event = new FDBVersionChangeEvent("versionchange", {
|
|
66
|
+
newVersion: null,
|
|
67
|
+
oldVersion: db.version
|
|
68
|
+
});
|
|
69
|
+
openDatabase2.dispatchEvent(event);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Wait for all of the events to be fired. (i.e. queue a task)
|
|
75
|
+
queueTask(() => {
|
|
76
|
+
// If any of the connections in openConnections are still not closed, queue a database task to
|
|
77
|
+
// fire a version change event named blocked at request with db’s version and null.
|
|
78
|
+
|
|
79
|
+
const anyOpen = openConnections.some(openDatabase3 => {
|
|
80
|
+
return !openDatabase3._closed && !openDatabase3._closePending;
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// If any of the connections in openConnections are still not closed, queue a database task to
|
|
84
|
+
// fire a version change event named blocked at request with db’s version and null.
|
|
85
|
+
if (anyOpen) {
|
|
86
|
+
queueTask(() => {
|
|
87
|
+
const event = new FDBVersionChangeEvent("blocked", {
|
|
88
|
+
newVersion: null,
|
|
89
|
+
oldVersion: db.version
|
|
90
|
+
});
|
|
91
|
+
request.dispatchEvent(event);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Wait until all connections in openConnections are closed.
|
|
96
|
+
waitForOthersClosedDelete(databases, name, openConnections, onComplete);
|
|
97
|
+
});
|
|
98
|
+
} catch (err) {
|
|
99
|
+
onComplete(err);
|
|
41
100
|
}
|
|
42
|
-
}
|
|
43
|
-
const anyOpen = openDatabases.some(openDatabase3 => {
|
|
44
|
-
return !openDatabase3._closed && !openDatabase3._closePending;
|
|
45
101
|
});
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
newVersion: null,
|
|
49
|
-
oldVersion: db.version
|
|
50
|
-
});
|
|
51
|
-
request.dispatchEvent(event);
|
|
52
|
-
}
|
|
53
|
-
waitForOthersClosedDelete(databases, name, openDatabases, cb);
|
|
54
|
-
} catch (err) {
|
|
55
|
-
cb(err);
|
|
56
|
-
}
|
|
102
|
+
};
|
|
103
|
+
runTaskInConnectionQueue(connectionQueues, name, deleteDBTask);
|
|
57
104
|
};
|
|
58
105
|
|
|
59
|
-
//
|
|
106
|
+
// https://w3c.github.io/IndexedDB/#ref-for-database-version%E2%91%A0%E2%91%A2
|
|
60
107
|
const runVersionchangeTransaction = (connection, version, request, cb) => {
|
|
61
108
|
connection._runningVersionchangeTransaction = true;
|
|
62
|
-
const oldVersion = connection.version;
|
|
63
|
-
|
|
109
|
+
const oldVersion = connection._oldVersion = connection.version;
|
|
110
|
+
|
|
111
|
+
// Let openConnections be the set of all connections, except connection, associated with db.
|
|
112
|
+
const openConnections = connection._rawDatabase.connections.filter(otherDatabase => {
|
|
64
113
|
return connection !== otherDatabase;
|
|
65
114
|
});
|
|
66
|
-
|
|
115
|
+
|
|
116
|
+
// For each entry of openConnections that does not have its close pending flag set to true, queue a
|
|
117
|
+
// database task to fire a version change event named versionchange at entry with db’s version and version.
|
|
118
|
+
for (const openDatabase2 of openConnections) {
|
|
67
119
|
if (!openDatabase2._closed && !openDatabase2._closePending) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
120
|
+
queueTask(() => {
|
|
121
|
+
const event = new FDBVersionChangeEvent("versionchange", {
|
|
122
|
+
newVersion: version,
|
|
123
|
+
oldVersion
|
|
124
|
+
});
|
|
125
|
+
openDatabase2.dispatchEvent(event);
|
|
71
126
|
});
|
|
72
|
-
openDatabase2.dispatchEvent(event);
|
|
73
127
|
}
|
|
74
128
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
oldVersion
|
|
82
|
-
});
|
|
83
|
-
request.dispatchEvent(event);
|
|
84
|
-
}
|
|
85
|
-
const waitForOthersClosed = () => {
|
|
86
|
-
const anyOpen2 = openDatabases.some(openDatabase2 => {
|
|
87
|
-
return !openDatabase2._closed && !openDatabase2._closePending;
|
|
129
|
+
|
|
130
|
+
// Wait for all of the events to be fired.
|
|
131
|
+
// (i.e. queue a task)
|
|
132
|
+
queueTask(() => {
|
|
133
|
+
const anyOpen = openConnections.some(openDatabase3 => {
|
|
134
|
+
return !openDatabase3._closed && !openDatabase3._closePending;
|
|
88
135
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
136
|
+
|
|
137
|
+
// If any of the connections in openConnections are still not closed, queue a database task to fire a version change event named blocked at request with db’s version and version.
|
|
138
|
+
if (anyOpen) {
|
|
139
|
+
queueTask(() => {
|
|
140
|
+
const event = new FDBVersionChangeEvent("blocked", {
|
|
141
|
+
newVersion: version,
|
|
142
|
+
oldVersion
|
|
143
|
+
});
|
|
144
|
+
request.dispatchEvent(event);
|
|
145
|
+
});
|
|
92
146
|
}
|
|
93
147
|
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
148
|
+
// Wait until all connections in openConnections are closed.
|
|
149
|
+
const waitForOthersClosed = () => {
|
|
150
|
+
const anyOpen2 = openConnections.some(openDatabase2 => {
|
|
151
|
+
return !openDatabase2._closed && !openDatabase2._closePending;
|
|
152
|
+
});
|
|
153
|
+
if (anyOpen2) {
|
|
154
|
+
queueTask(waitForOthersClosed);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
98
157
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
connection.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
//
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
request.transaction = null;
|
|
121
|
-
queueTask(() => {
|
|
122
|
-
cb(new AbortError());
|
|
158
|
+
// Set the version of database to version. This change is considered part of the transaction, and so if the
|
|
159
|
+
// transaction is aborted, this change is reverted.
|
|
160
|
+
connection._rawDatabase.version = version;
|
|
161
|
+
connection.version = version;
|
|
162
|
+
|
|
163
|
+
// Get rid of this setImmediate?
|
|
164
|
+
const transaction = connection.transaction(Array.from(connection.objectStoreNames), "versionchange");
|
|
165
|
+
|
|
166
|
+
// associate the transaction with the open request for later lookup
|
|
167
|
+
transaction._openRequest = request;
|
|
168
|
+
|
|
169
|
+
// https://w3c.github.io/IndexedDB/#upgrade-a-database
|
|
170
|
+
// Set request’s result to connection.
|
|
171
|
+
request.result = connection;
|
|
172
|
+
// Set request’s done flag to true.
|
|
173
|
+
request.readyState = "done";
|
|
174
|
+
// Set request’s transaction to transaction.
|
|
175
|
+
request.transaction = transaction;
|
|
176
|
+
transaction._rollbackLog.push(() => {
|
|
177
|
+
connection._rawDatabase.version = oldVersion;
|
|
178
|
+
connection.version = oldVersion;
|
|
123
179
|
});
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
// Let
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
180
|
+
|
|
181
|
+
// Set transaction’s state to active.
|
|
182
|
+
transaction._state = "active";
|
|
183
|
+
|
|
184
|
+
// Let didThrow be the result of firing a version change event named upgradeneeded at request with old version and version.
|
|
185
|
+
const event = new FDBVersionChangeEvent("upgradeneeded", {
|
|
186
|
+
newVersion: version,
|
|
187
|
+
oldVersion
|
|
188
|
+
});
|
|
189
|
+
let didThrow = false;
|
|
190
|
+
try {
|
|
191
|
+
request.dispatchEvent(event);
|
|
192
|
+
} catch (_err) {
|
|
193
|
+
didThrow = true;
|
|
194
|
+
}
|
|
195
|
+
const concludeUpgrade = () => {
|
|
196
|
+
// If transaction’s state is active, then:
|
|
197
|
+
if (transaction._state === "active") {
|
|
198
|
+
// Set transaction’s state to inactive.
|
|
199
|
+
transaction._state = "inactive";
|
|
200
|
+
if (didThrow) {
|
|
201
|
+
// If didThrow is true, run abort a transaction with transaction and a newly created "AbortError" DOMException.
|
|
202
|
+
transaction._abort("AbortError");
|
|
203
|
+
}
|
|
134
204
|
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// The "upgrade a database" steps are supposed to run as a database task on the database access task source
|
|
208
|
+
// (i.e. off the main thread), but since we're actually running on the main thread, we have to be tricky:
|
|
209
|
+
// 1. If any `upgradeneeded` event handlers errored, abort synchronously
|
|
210
|
+
// 2. Else yield to allow any microtasks to run in response to that event
|
|
211
|
+
if (didThrow) {
|
|
212
|
+
concludeUpgrade();
|
|
213
|
+
} else {
|
|
214
|
+
queueTask(concludeUpgrade);
|
|
215
|
+
}
|
|
216
|
+
transaction.addEventListener("error", () => {
|
|
217
|
+
connection._runningVersionchangeTransaction = false;
|
|
218
|
+
connection._oldVersion = undefined;
|
|
219
|
+
// throw arguments[0].target.error;
|
|
220
|
+
// console.log("error in versionchange transaction - not sure if anything needs to be done here", e.target.error.name);
|
|
135
221
|
});
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
222
|
+
transaction.addEventListener("abort", () => {
|
|
223
|
+
connection._runningVersionchangeTransaction = false;
|
|
224
|
+
connection._oldVersion = undefined;
|
|
225
|
+
request.transaction = null;
|
|
226
|
+
queueTask(() => {
|
|
227
|
+
cb(new AbortError());
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
transaction.addEventListener("complete", () => {
|
|
231
|
+
connection._runningVersionchangeTransaction = false;
|
|
232
|
+
connection._oldVersion = undefined;
|
|
233
|
+
request.transaction = null;
|
|
234
|
+
// Let other complete event handlers run before continuing
|
|
235
|
+
queueTask(() => {
|
|
236
|
+
if (connection._closePending) {
|
|
237
|
+
cb(new AbortError());
|
|
238
|
+
} else {
|
|
239
|
+
cb(null);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
};
|
|
244
|
+
waitForOthersClosed();
|
|
245
|
+
});
|
|
139
246
|
};
|
|
140
247
|
|
|
141
|
-
//
|
|
142
|
-
const openDatabase = (databases, name, version, request, cb) => {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
248
|
+
// https://w3c.github.io/IndexedDB/#opening
|
|
249
|
+
const openDatabase = (databases, connectionQueues, name, version, request, cb) => {
|
|
250
|
+
const openDBTask = () => {
|
|
251
|
+
return new Promise(resolve => {
|
|
252
|
+
const onComplete = err => {
|
|
253
|
+
try {
|
|
254
|
+
if (err) {
|
|
255
|
+
// DO THIS HERE: ensure that connection is closed by running the steps for closing a database connection before these
|
|
256
|
+
// steps are aborted.
|
|
257
|
+
cb(err);
|
|
258
|
+
} else {
|
|
259
|
+
cb(null, connection);
|
|
260
|
+
}
|
|
261
|
+
} finally {
|
|
262
|
+
resolve();
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
// Let db be the database named name in storageKey, or null otherwise.
|
|
267
|
+
let db = databases.get(name);
|
|
268
|
+
if (db === undefined) {
|
|
269
|
+
// If db is null, let db be a new database with name `name`, version 0 (zero), and with no object stores.
|
|
270
|
+
db = new Database(name, 0);
|
|
271
|
+
databases.set(name, db);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// If version is undefined, let version be 1 if db is null, or db’s version otherwise.
|
|
275
|
+
if (version === undefined) {
|
|
276
|
+
version = db.version !== 0 ? db.version : 1;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// If db’s version is greater than version, return a newly created "VersionError" DOMException and abort these steps.
|
|
280
|
+
if (db.version > version) {
|
|
281
|
+
return onComplete(new VersionError());
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Let connection be a new connection to db.
|
|
285
|
+
const connection = new FDBDatabase(db);
|
|
286
|
+
|
|
287
|
+
// If db’s version is less than version, then:
|
|
288
|
+
if (db.version < version) {
|
|
289
|
+
// (run a version change transaction and resolve so that the next promise in the queue will execute)
|
|
290
|
+
runVersionchangeTransaction(connection, version, request, err => {
|
|
291
|
+
onComplete(err);
|
|
292
|
+
});
|
|
293
|
+
} else {
|
|
294
|
+
onComplete(null);
|
|
161
295
|
}
|
|
162
|
-
cb(null, connection);
|
|
163
296
|
});
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
}
|
|
297
|
+
};
|
|
298
|
+
runTaskInConnectionQueue(connectionQueues, name, openDBTask);
|
|
167
299
|
};
|
|
168
300
|
class FDBFactory {
|
|
169
301
|
cmp = cmp;
|
|
170
302
|
_databases = new Map();
|
|
303
|
+
// https://w3c.github.io/IndexedDB/#connection-queue
|
|
304
|
+
_connectionQueues = new Map(); // promise chain as lightweight FIFO task queue
|
|
171
305
|
|
|
172
|
-
//
|
|
306
|
+
// https://w3c.github.io/IndexedDB/#dom-idbfactory-deletedatabase
|
|
173
307
|
deleteDatabase(name) {
|
|
174
308
|
const request = new FDBOpenDBRequest();
|
|
175
309
|
request.source = null;
|
|
176
310
|
queueTask(() => {
|
|
177
|
-
|
|
178
|
-
const oldVersion = db !== undefined ? db.version : 0;
|
|
179
|
-
deleteDatabase(this._databases, name, request, err => {
|
|
311
|
+
deleteDatabase(this._databases, this._connectionQueues, name, request, (err, oldVersion) => {
|
|
180
312
|
if (err) {
|
|
181
313
|
request.error = new DOMException(err.message, err.name);
|
|
182
314
|
request.readyState = "done";
|
|
@@ -213,7 +345,7 @@ class FDBFactory {
|
|
|
213
345
|
const request = new FDBOpenDBRequest();
|
|
214
346
|
request.source = null;
|
|
215
347
|
queueTask(() => {
|
|
216
|
-
openDatabase(this._databases, name, version, request, (err, connection) => {
|
|
348
|
+
openDatabase(this._databases, this._connectionQueues, name, version, request, (err, connection) => {
|
|
217
349
|
if (err) {
|
|
218
350
|
request.result = undefined;
|
|
219
351
|
request.readyState = "done";
|
|
@@ -238,16 +370,24 @@ class FDBFactory {
|
|
|
238
370
|
|
|
239
371
|
// https://w3c.github.io/IndexedDB/#dom-idbfactory-databases
|
|
240
372
|
databases() {
|
|
241
|
-
return
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
373
|
+
return Promise.resolve(Array.from(this._databases.entries(), ([name, database]) => {
|
|
374
|
+
const activeVersionChangeConnection = database.connections.find(connection => connection._runningVersionchangeTransaction);
|
|
375
|
+
// If a versionchange is in progress, report the old version. See `get-databases.any.js` test:
|
|
376
|
+
// "The result of databases() should contain the versions of databases at the time of calling,
|
|
377
|
+
// regardless of versionchange transactions currently running."
|
|
378
|
+
const version = activeVersionChangeConnection ? activeVersionChangeConnection._oldVersion : database.version;
|
|
379
|
+
return {
|
|
380
|
+
name,
|
|
381
|
+
version
|
|
382
|
+
};
|
|
383
|
+
}).filter(({
|
|
384
|
+
version
|
|
385
|
+
}) => {
|
|
386
|
+
// Ignore newly-created DBs with active versionchange transactions. See `get-databases.any.js` test:
|
|
387
|
+
// "The result of databases() should be only those databases which have been created at the
|
|
388
|
+
// time of calling, regardless of versionchange transactions currently running."
|
|
389
|
+
return version > 0;
|
|
390
|
+
}));
|
|
251
391
|
}
|
|
252
392
|
get [Symbol.toStringTag]() {
|
|
253
393
|
return "IDBFactory";
|
package/build/esm/FDBKeyRange.js
CHANGED
|
@@ -11,6 +11,7 @@ class FDBTransaction extends FakeEventTarget {
|
|
|
11
11
|
_started = false;
|
|
12
12
|
_rollbackLog = [];
|
|
13
13
|
_objectStoresCache = new Map();
|
|
14
|
+
_openRequest = null;
|
|
14
15
|
error = null;
|
|
15
16
|
onabort = null;
|
|
16
17
|
oncomplete = null;
|
|
@@ -25,7 +26,7 @@ class FDBTransaction extends FakeEventTarget {
|
|
|
25
26
|
this.objectStoreNames = new FakeDOMStringList(...Array.from(this._scope).sort());
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
//
|
|
29
|
+
// https://w3c.github.io/IndexedDB/#abort-transaction
|
|
29
30
|
_abort(errName) {
|
|
30
31
|
for (const f of this._rollbackLog.reverse()) {
|
|
31
32
|
f();
|
|
@@ -42,24 +43,61 @@ class FDBTransaction extends FakeEventTarget {
|
|
|
42
43
|
if (request.readyState !== "done") {
|
|
43
44
|
request.readyState = "done"; // This will cancel execution of this request's operation
|
|
44
45
|
if (request.source) {
|
|
45
|
-
|
|
46
|
-
request
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
// https://w3c.github.io/IndexedDB/#ref-for-list-iterate%E2%91%A2
|
|
47
|
+
// For each request of transaction’s request list, abort the steps to asynchronously
|
|
48
|
+
// execute a request for request, set request’s processed flag to true, and queue a
|
|
49
|
+
// database task to run these steps:
|
|
50
|
+
queueTask(() => {
|
|
51
|
+
// Set request’s result to undefined.
|
|
52
|
+
request.result = undefined;
|
|
53
|
+
// Set request’s error to a newly created "AbortError" DOMException.
|
|
54
|
+
request.error = new AbortError();
|
|
55
|
+
|
|
56
|
+
// Fire an event named error at request with its bubbles and cancelable attributes initialized
|
|
57
|
+
// to true.
|
|
58
|
+
const event = new FakeEvent("error", {
|
|
59
|
+
bubbles: true,
|
|
60
|
+
cancelable: true
|
|
61
|
+
});
|
|
62
|
+
event.eventPath = [this.db, this];
|
|
63
|
+
try {
|
|
64
|
+
request.dispatchEvent(event);
|
|
65
|
+
} catch (_err) {
|
|
66
|
+
if (this._state === "active") {
|
|
67
|
+
this._abort("AbortError");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
50
70
|
});
|
|
51
|
-
event.eventPath = [this.db, this];
|
|
52
|
-
request.dispatchEvent(event);
|
|
53
71
|
}
|
|
54
72
|
}
|
|
55
73
|
}
|
|
74
|
+
|
|
75
|
+
// Queue a database task to run these steps:
|
|
56
76
|
queueTask(() => {
|
|
77
|
+
// If transaction is an upgrade transaction, then set transaction’s connection’s associated database’s
|
|
78
|
+
// upgrade transaction to null.
|
|
79
|
+
// (i.e. remove it from the list of `db.connections`)
|
|
80
|
+
const isUpgradeTransaction = this.mode === "versionchange";
|
|
81
|
+
if (isUpgradeTransaction) {
|
|
82
|
+
this.db._rawDatabase.connections = this.db._rawDatabase.connections.filter(connection => !connection._rawDatabase.transactions.includes(this));
|
|
83
|
+
}
|
|
84
|
+
// Fire an event named abort at transaction with its bubbles attribute initialized to true.
|
|
57
85
|
const event = new FakeEvent("abort", {
|
|
58
86
|
bubbles: true,
|
|
59
87
|
cancelable: false
|
|
60
88
|
});
|
|
61
89
|
event.eventPath = [this.db];
|
|
62
90
|
this.dispatchEvent(event);
|
|
91
|
+
|
|
92
|
+
// If transaction is an upgrade transaction, then:
|
|
93
|
+
if (isUpgradeTransaction) {
|
|
94
|
+
// Let request be the open request associated with transaction.
|
|
95
|
+
const request = this._openRequest;
|
|
96
|
+
// Set request’s transaction to null.
|
|
97
|
+
request.transaction = null;
|
|
98
|
+
// Set request’s result to undefined.
|
|
99
|
+
request.result = undefined;
|
|
100
|
+
}
|
|
63
101
|
});
|
|
64
102
|
this._state = "finished";
|
|
65
103
|
}
|
|
@@ -171,11 +209,11 @@ class FDBTransaction extends FakeEventTarget {
|
|
|
171
209
|
try {
|
|
172
210
|
event.eventPath = [this.db, this];
|
|
173
211
|
request.dispatchEvent(event);
|
|
174
|
-
} catch (
|
|
175
|
-
if (this._state
|
|
212
|
+
} catch (_err) {
|
|
213
|
+
if (this._state === "active") {
|
|
176
214
|
this._abort("AbortError");
|
|
215
|
+
defaultAction = undefined; // do not abort again
|
|
177
216
|
}
|
|
178
|
-
throw err;
|
|
179
217
|
}
|
|
180
218
|
|
|
181
219
|
// Default action of event
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { queueTask } from "./scheduling.js";
|
|
2
|
-
|
|
2
|
+
import { intersection } from "./intersection.js";
|
|
3
3
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-database
|
|
4
4
|
class Database {
|
|
5
|
-
deletePending = false;
|
|
6
5
|
transactions = [];
|
|
7
6
|
rawObjectStores = new Map();
|
|
8
7
|
connections = [];
|
|
@@ -13,18 +12,27 @@ class Database {
|
|
|
13
12
|
}
|
|
14
13
|
processTransactions() {
|
|
15
14
|
queueTask(() => {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
next.addEventListener("abort", this.processTransactions);
|
|
26
|
-
next._start();
|
|
15
|
+
const running = this.transactions.filter(transaction => transaction._started && transaction._state !== "finished");
|
|
16
|
+
const waiting = this.transactions.filter(transaction => !transaction._started && transaction._state !== "finished");
|
|
17
|
+
|
|
18
|
+
// The next transaction to run is the first waiting one that doesn't overlap with either a running one or a
|
|
19
|
+
// preceding waiting one. This allows non-overlapping transactions to run in parallel.
|
|
20
|
+
const next = waiting.find((transaction, i) => {
|
|
21
|
+
const anyRunning = running.some(other => intersection(other._scope, transaction._scope).size > 0);
|
|
22
|
+
if (anyRunning) {
|
|
23
|
+
return false;
|
|
27
24
|
}
|
|
25
|
+
|
|
26
|
+
// If any _preceding_ waiting transactions are blocked, then that's also blocking.
|
|
27
|
+
// E.g. if you have 3 transactions: [a], [a,b], and [b,c], then [a] blocks [a,b] which blocks [b,c]
|
|
28
|
+
// until [a] is complete, even though [a] and [b,c] share no overlap.
|
|
29
|
+
const anyWaiting = waiting.slice(0, i).some(other => intersection(other._scope, transaction._scope).size > 0);
|
|
30
|
+
return !anyWaiting;
|
|
31
|
+
});
|
|
32
|
+
if (next) {
|
|
33
|
+
next.addEventListener("complete", this.processTransactions);
|
|
34
|
+
next.addEventListener("abort", this.processTransactions);
|
|
35
|
+
next._start();
|
|
28
36
|
}
|
|
29
37
|
});
|
|
30
38
|
}
|