fake-indexeddb 6.2.2 → 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.
@@ -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
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-deleting-a-database
23
- const deleteDatabase = (databases, name, request, cb) => {
24
- try {
25
- const db = databases.get(name);
26
- if (db === undefined) {
27
- cb(null);
28
- return;
29
- }
30
- db.deletePending = true;
31
- const openDatabases = db.connections.filter(connection => {
32
- return !connection._closed && !connection._closePending;
33
- });
34
- for (const openDatabase2 of openDatabases) {
35
- if (!openDatabase2._closePending) {
36
- const event = new FDBVersionChangeEvent("versionchange", {
37
- newVersion: null,
38
- oldVersion: db.version
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
- openDatabase2.dispatchEvent(event);
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
- if (request && anyOpen) {
47
- const event = new FDBVersionChangeEvent("blocked", {
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
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-running-a-versionchange-transaction
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
- const openDatabases = connection._rawDatabase.connections.filter(otherDatabase => {
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
- for (const openDatabase2 of openDatabases) {
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
- const event = new FDBVersionChangeEvent("versionchange", {
69
- newVersion: version,
70
- oldVersion
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
- const anyOpen = openDatabases.some(openDatabase3 => {
76
- return !openDatabase3._closed && !openDatabase3._closePending;
77
- });
78
- if (anyOpen) {
79
- const event = new FDBVersionChangeEvent("blocked", {
80
- newVersion: version,
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
- if (anyOpen2) {
90
- queueTask(waitForOthersClosed);
91
- return;
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
- // Set the version of database to version. This change is considered part of the transaction, and so if the
95
- // transaction is aborted, this change is reverted.
96
- connection._rawDatabase.version = version;
97
- connection.version = version;
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
- // Get rid of this setImmediate?
100
- const transaction = connection.transaction(Array.from(connection.objectStoreNames), "versionchange");
101
- request.result = connection;
102
- request.readyState = "done";
103
- request.transaction = transaction;
104
- transaction._rollbackLog.push(() => {
105
- connection._rawDatabase.version = oldVersion;
106
- connection.version = oldVersion;
107
- });
108
- const event = new FDBVersionChangeEvent("upgradeneeded", {
109
- newVersion: version,
110
- oldVersion
111
- });
112
- request.dispatchEvent(event);
113
- transaction.addEventListener("error", () => {
114
- connection._runningVersionchangeTransaction = false;
115
- // throw arguments[0].target.error;
116
- // console.log("error in versionchange transaction - not sure if anything needs to be done here", e.target.error.name);
117
- });
118
- transaction.addEventListener("abort", () => {
119
- connection._runningVersionchangeTransaction = false;
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
- transaction.addEventListener("complete", () => {
126
- connection._runningVersionchangeTransaction = false;
127
- request.transaction = null;
128
- // Let other complete event handlers run before continuing
129
- queueTask(() => {
130
- if (connection._closePending) {
131
- cb(new AbortError());
132
- } else {
133
- cb(null);
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
- waitForOthersClosed();
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
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-opening-a-database
142
- const openDatabase = (databases, name, version, request, cb) => {
143
- let db = databases.get(name);
144
- if (db === undefined) {
145
- db = new Database(name, 0);
146
- databases.set(name, db);
147
- }
148
- if (version === undefined) {
149
- version = db.version !== 0 ? db.version : 1;
150
- }
151
- if (db.version > version) {
152
- return cb(new VersionError());
153
- }
154
- const connection = new FDBDatabase(db);
155
- if (db.version < version) {
156
- runVersionchangeTransaction(connection, version, request, err => {
157
- if (err) {
158
- // DO THIS HERE: ensure that connection is closed by running the steps for closing a database connection before these
159
- // steps are aborted.
160
- return cb(err);
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
- } else {
165
- cb(null, connection);
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
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBFactory-deleteDatabase-IDBOpenDBRequest-DOMString-name
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
- const db = this._databases.get(name);
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 new Promise(resolve => {
242
- const result = [];
243
- for (const [name, database] of this._databases) {
244
- result.push({
245
- name,
246
- version: database.version
247
- });
248
- }
249
- resolve(result);
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";
@@ -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
- // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-aborting-a-transaction
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
- request.result = undefined;
46
- request.error = new AbortError();
47
- const event = new FakeEvent("error", {
48
- bubbles: true,
49
- cancelable: true
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 (err) {
175
- if (this._state !== "committing") {
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,7 +1,7 @@
1
1
  import { queueTask } from "./scheduling.js";
2
+ import { intersection } from "./intersection.js";
2
3
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-database
3
4
  class Database {
4
- deletePending = false;
5
5
  transactions = [];
6
6
  rawObjectStores = new Map();
7
7
  connections = [];
@@ -12,18 +12,27 @@ class Database {
12
12
  }
13
13
  processTransactions() {
14
14
  queueTask(() => {
15
- const anyRunning = this.transactions.some(transaction => {
16
- return transaction._started && transaction._state !== "finished";
17
- });
18
- if (!anyRunning) {
19
- const next = this.transactions.find(transaction => {
20
- return !transaction._started && transaction._state !== "finished";
21
- });
22
- if (next) {
23
- next.addEventListener("complete", this.processTransactions);
24
- next.addEventListener("abort", this.processTransactions);
25
- 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;
26
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();
27
36
  }
28
37
  });
29
38
  }
@@ -6,6 +6,15 @@ const stopped = (event, listener) => {
6
6
  // http://www.w3.org/TR/dom/#concept-event-listener-invoke
7
7
  const invokeEventListeners = (event, obj) => {
8
8
  event.currentTarget = obj;
9
+ const errors = [];
10
+ const invoke = callback => {
11
+ try {
12
+ // @ts-expect-error EventCallback's types are not quite right here
13
+ callback.call(event.currentTarget, event);
14
+ } catch (err) {
15
+ errors.push(err);
16
+ }
17
+ };
9
18
 
10
19
  // The callback might cause obj.listeners to mutate as we traverse it.
11
20
  // Take a copy of the array so that nothing sneaks in and we don't lose
@@ -14,9 +23,7 @@ const invokeEventListeners = (event, obj) => {
14
23
  if (event.type !== listener.type || stopped(event, listener)) {
15
24
  continue;
16
25
  }
17
-
18
- // @ts-ignore
19
- listener.callback.call(event.currentTarget, event);
26
+ invoke(listener.callback);
20
27
  }
21
28
  const typeToProp = {
22
29
  abort: "onabort",
@@ -40,10 +47,15 @@ const invokeEventListeners = (event, obj) => {
40
47
  type: event.type
41
48
  };
42
49
  if (!stopped(event, listener)) {
43
- // @ts-ignore
44
- listener.callback.call(event.currentTarget, event);
50
+ invoke(listener.callback);
45
51
  }
46
52
  }
53
+
54
+ // we want to execute all listeners before deciding if we want to throw, because there could be an error thrown by
55
+ // the first listener, but the second should still be invoked
56
+ if (errors.length) {
57
+ throw new AggregateError(errors);
58
+ }
47
59
  };
48
60
  class FakeEventTarget {
49
61
  listeners = [];