fake-indexeddb 6.1.0 → 6.2.1
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/README.md +15 -0
- package/auto/index.js +34 -13
- package/auto/index.mjs +34 -13
- package/build/cjs/FDBDatabase.js +13 -22
- package/build/cjs/FDBTransaction.js +2 -1
- package/build/cjs/forceCloseDatabase.js +29 -0
- package/build/cjs/index.js +7 -0
- package/build/cjs/lib/FakeEventTarget.js +1 -0
- package/build/cjs/lib/Index.js +3 -3
- package/build/cjs/lib/ObjectStore.js +20 -10
- package/build/cjs/lib/RecordStore.js +59 -142
- package/build/cjs/lib/binarySearchTree.js +308 -0
- package/build/cjs/lib/closeConnection.js +36 -0
- package/build/cjs/lib/extractKey.js +4 -1
- package/build/cjs/lib/scheduling.js +11 -1
- package/build/esm/FDBDatabase.js +12 -21
- package/build/esm/FDBTransaction.js +2 -1
- package/build/esm/forceCloseDatabase.js +22 -0
- package/build/esm/index.js +2 -1
- package/build/esm/lib/FakeEventTarget.js +1 -0
- package/build/esm/lib/Index.js +3 -3
- package/build/esm/lib/ObjectStore.js +21 -11
- package/build/esm/lib/RecordStore.js +59 -142
- package/build/esm/lib/binarySearchTree.js +300 -0
- package/build/esm/lib/closeConnection.js +28 -0
- package/build/esm/lib/extractKey.js +4 -1
- package/build/esm/lib/scheduling.js +11 -1
- package/package.json +7 -2
- package/types.d.ts +2 -0
- package/build/cjs/lib/binarySearch.js +0 -85
- package/build/esm/lib/binarySearch.js +0 -74
package/README.md
CHANGED
|
@@ -178,6 +178,21 @@ import { IDBFactory } from "fake-indexeddb";
|
|
|
178
178
|
indexedDB = new IDBFactory();
|
|
179
179
|
```
|
|
180
180
|
|
|
181
|
+
### Triggering the `"close"` event
|
|
182
|
+
|
|
183
|
+
An `IDBDatabase` will fire a `"close"` event when closed for [abnormal reasons](https://www.w3.org/TR/IndexedDB/#closing-connection), such as the user manually deleting databases in DevTools. If you want to simulate this event for test coverage, you can use `forceCloseDatabase()`:
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
import { forceCloseDatabase } from "fake-indexeddb";
|
|
187
|
+
|
|
188
|
+
db.addEventListener("close", () => {
|
|
189
|
+
console.log("Forcibly closed!");
|
|
190
|
+
});
|
|
191
|
+
forceCloseDatabase(db); // invokes the event listener
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Note that `forceCloseDatabase()` is not a standard IndexedDB API and is unique to fake-indexeddb.
|
|
195
|
+
|
|
181
196
|
### With PhantomJS and other really old environments
|
|
182
197
|
|
|
183
198
|
PhantomJS (and other really old environments) are missing tons of modern JavaScript features. In fact, that may be why you use fake-indexeddb in such an environment! Prior to v3.0.0, fake-indexeddb imported core-js and automatically applied its polyfills. However, since most fake-indexeddb users are not using really old environments, I got rid of that runtime dependency in v3.0.0. To work around that, you can import core-js yourself before you import fake-indexeddb, like:
|
package/auto/index.js
CHANGED
|
@@ -22,16 +22,37 @@ var globalVar =
|
|
|
22
22
|
? global
|
|
23
23
|
: Function("return this;")();
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
// Match the native behavior for `globalThis.indexedDB`, `globlThis.IDBCursor`, etc.
|
|
26
|
+
// Per the IDL, `indexedDB` is readonly but the others are readwrite
|
|
27
|
+
// https://w3c.github.io/IndexedDB/#idl-index
|
|
28
|
+
const createPropertyDescriptor = (value, readOnly = false) => {
|
|
29
|
+
return {
|
|
30
|
+
...(readOnly
|
|
31
|
+
? {
|
|
32
|
+
set: undefined,
|
|
33
|
+
get: () => value,
|
|
34
|
+
}
|
|
35
|
+
: {
|
|
36
|
+
value,
|
|
37
|
+
writable: true,
|
|
38
|
+
}),
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
Object.defineProperties(globalVar, {
|
|
45
|
+
indexedDB: createPropertyDescriptor(fakeIndexedDB, true),
|
|
46
|
+
IDBCursor: createPropertyDescriptor(FDBCursor),
|
|
47
|
+
IDBCursorWithValue: createPropertyDescriptor(FDBCursorWithValue),
|
|
48
|
+
IDBDatabase: createPropertyDescriptor(FDBDatabase),
|
|
49
|
+
IDBFactory: createPropertyDescriptor(FDBFactory),
|
|
50
|
+
IDBIndex: createPropertyDescriptor(FDBIndex),
|
|
51
|
+
IDBKeyRange: createPropertyDescriptor(FDBKeyRange),
|
|
52
|
+
IDBObjectStore: createPropertyDescriptor(FDBObjectStore),
|
|
53
|
+
IDBOpenDBRequest: createPropertyDescriptor(FDBOpenDBRequest),
|
|
54
|
+
IDBRecord: createPropertyDescriptor(FDBRecord),
|
|
55
|
+
IDBRequest: createPropertyDescriptor(FDBRequest),
|
|
56
|
+
IDBTransaction: createPropertyDescriptor(FDBTransaction),
|
|
57
|
+
IDBVersionChangeEvent: createPropertyDescriptor(FDBVersionChangeEvent),
|
|
58
|
+
});
|
package/auto/index.mjs
CHANGED
|
@@ -22,16 +22,37 @@ var globalVar =
|
|
|
22
22
|
? global
|
|
23
23
|
: Function("return this;")();
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
// Match the native behavior for `globalThis.indexedDB`, `globlThis.IDBCursor`, etc.
|
|
26
|
+
// Per the IDL, `indexedDB` is readonly but the others are readwrite
|
|
27
|
+
// https://w3c.github.io/IndexedDB/#idl-index
|
|
28
|
+
const createPropertyDescriptor = (value, readOnly = false) => {
|
|
29
|
+
return {
|
|
30
|
+
...(readOnly
|
|
31
|
+
? {
|
|
32
|
+
set: undefined,
|
|
33
|
+
get: () => value,
|
|
34
|
+
}
|
|
35
|
+
: {
|
|
36
|
+
value,
|
|
37
|
+
writable: true,
|
|
38
|
+
}),
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
Object.defineProperties(globalVar, {
|
|
45
|
+
indexedDB: createPropertyDescriptor(fakeIndexedDB, true),
|
|
46
|
+
IDBCursor: createPropertyDescriptor(FDBCursor),
|
|
47
|
+
IDBCursorWithValue: createPropertyDescriptor(FDBCursorWithValue),
|
|
48
|
+
IDBDatabase: createPropertyDescriptor(FDBDatabase),
|
|
49
|
+
IDBFactory: createPropertyDescriptor(FDBFactory),
|
|
50
|
+
IDBIndex: createPropertyDescriptor(FDBIndex),
|
|
51
|
+
IDBKeyRange: createPropertyDescriptor(FDBKeyRange),
|
|
52
|
+
IDBObjectStore: createPropertyDescriptor(FDBObjectStore),
|
|
53
|
+
IDBOpenDBRequest: createPropertyDescriptor(FDBOpenDBRequest),
|
|
54
|
+
IDBRecord: createPropertyDescriptor(FDBRecord),
|
|
55
|
+
IDBRequest: createPropertyDescriptor(FDBRequest),
|
|
56
|
+
IDBTransaction: createPropertyDescriptor(FDBTransaction),
|
|
57
|
+
IDBVersionChangeEvent: createPropertyDescriptor(FDBVersionChangeEvent),
|
|
58
|
+
});
|
package/build/cjs/FDBDatabase.js
CHANGED
|
@@ -9,8 +9,8 @@ var _errors = require("./lib/errors.js");
|
|
|
9
9
|
var _FakeDOMStringList = _interopRequireDefault(require("./lib/FakeDOMStringList.js"));
|
|
10
10
|
var _FakeEventTarget = _interopRequireDefault(require("./lib/FakeEventTarget.js"));
|
|
11
11
|
var _ObjectStore = _interopRequireDefault(require("./lib/ObjectStore.js"));
|
|
12
|
-
var _scheduling = require("./lib/scheduling.js");
|
|
13
12
|
var _validateKeyPath = _interopRequireDefault(require("./lib/validateKeyPath.js"));
|
|
13
|
+
var _closeConnection = _interopRequireDefault(require("./lib/closeConnection.js"));
|
|
14
14
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
15
15
|
const confirmActiveVersionchangeTransaction = database => {
|
|
16
16
|
if (!database._runningVersionchangeTransaction) {
|
|
@@ -31,24 +31,6 @@ const confirmActiveVersionchangeTransaction = database => {
|
|
|
31
31
|
return transaction;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#database-closing-steps
|
|
35
|
-
const closeConnection = connection => {
|
|
36
|
-
connection._closePending = true;
|
|
37
|
-
const transactionsComplete = connection._rawDatabase.transactions.every(transaction => {
|
|
38
|
-
return transaction._state === "finished";
|
|
39
|
-
});
|
|
40
|
-
if (transactionsComplete) {
|
|
41
|
-
connection._closed = true;
|
|
42
|
-
connection._rawDatabase.connections = connection._rawDatabase.connections.filter(otherConnection => {
|
|
43
|
-
return connection !== otherConnection;
|
|
44
|
-
});
|
|
45
|
-
} else {
|
|
46
|
-
(0, _scheduling.queueTask)(() => {
|
|
47
|
-
closeConnection(connection);
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
|
|
52
34
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#database-interface
|
|
53
35
|
class FDBDatabase extends _FakeEventTarget.default {
|
|
54
36
|
_closePending = false;
|
|
@@ -121,7 +103,7 @@ class FDBDatabase extends _FakeEventTarget.default {
|
|
|
121
103
|
this._rawDatabase.rawObjectStores.delete(name);
|
|
122
104
|
transaction._objectStoresCache.delete(name);
|
|
123
105
|
}
|
|
124
|
-
transaction(storeNames, mode) {
|
|
106
|
+
transaction(storeNames, mode, options) {
|
|
125
107
|
mode = mode !== undefined ? mode : "readonly";
|
|
126
108
|
if (mode !== "readonly" && mode !== "readwrite" && mode !== "versionchange") {
|
|
127
109
|
throw new TypeError("Invalid mode: " + mode);
|
|
@@ -146,14 +128,23 @@ class FDBDatabase extends _FakeEventTarget.default {
|
|
|
146
128
|
throw new _errors.NotFoundError("No objectStore named " + storeName + " in this database");
|
|
147
129
|
}
|
|
148
130
|
}
|
|
149
|
-
|
|
131
|
+
|
|
132
|
+
// the actual algo is more complex but this passes the IDB tests: https://webidl.spec.whatwg.org/#es-dictionary
|
|
133
|
+
const durability = options?.durability ?? "default";
|
|
134
|
+
// invalid enums throw a TypeError: https://webidl.spec.whatwg.org/#es-enumeration
|
|
135
|
+
if (durability !== "default" && durability !== "strict" && durability !== "relaxed") {
|
|
136
|
+
throw new TypeError(
|
|
137
|
+
// based on Firefox's error message
|
|
138
|
+
`'${durability}' (value of 'durability' member of IDBTransactionOptions) ` + `is not a valid value for enumeration IDBTransactionDurability`);
|
|
139
|
+
}
|
|
140
|
+
const tx = new _FDBTransaction.default(storeNames, mode, durability, this);
|
|
150
141
|
this._rawDatabase.transactions.push(tx);
|
|
151
142
|
this._rawDatabase.processTransactions(); // See if can start right away (async)
|
|
152
143
|
|
|
153
144
|
return tx;
|
|
154
145
|
}
|
|
155
146
|
close() {
|
|
156
|
-
|
|
147
|
+
(0, _closeConnection.default)(this);
|
|
157
148
|
}
|
|
158
149
|
get [Symbol.toStringTag]() {
|
|
159
150
|
return "IDBDatabase";
|
|
@@ -23,10 +23,11 @@ class FDBTransaction extends _FakeEventTarget.default {
|
|
|
23
23
|
oncomplete = null;
|
|
24
24
|
onerror = null;
|
|
25
25
|
_requests = [];
|
|
26
|
-
constructor(storeNames, mode, db) {
|
|
26
|
+
constructor(storeNames, mode, durability, db) {
|
|
27
27
|
super();
|
|
28
28
|
this._scope = new Set(storeNames);
|
|
29
29
|
this.mode = mode;
|
|
30
|
+
this.durability = durability;
|
|
30
31
|
this.db = db;
|
|
31
32
|
this.objectStoreNames = new _FakeDOMStringList.default(...Array.from(this._scope).sort());
|
|
32
33
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = forceCloseDatabase;
|
|
7
|
+
var _closeConnection = _interopRequireDefault(require("./lib/closeConnection.js"));
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
/**
|
|
10
|
+
* Forcibly closes a database. This simulates a database being closed due to abnormal reasons, such as
|
|
11
|
+
* using DevTools to clear data while a database is open. Spec-wise, this is equivalent to
|
|
12
|
+
* [closing a database connection](https://www.w3.org/TR/IndexedDB/#closing-connection) with the `forced flag`
|
|
13
|
+
* set to true.
|
|
14
|
+
*
|
|
15
|
+
* Use this API if you want to have more test coverage for unusual IndexedDB events, such as a database firing
|
|
16
|
+
* the "close" event:
|
|
17
|
+
*
|
|
18
|
+
* ```js
|
|
19
|
+
* db.addEventListener("close", () => {
|
|
20
|
+
* console.log("Forcibly closed!");
|
|
21
|
+
* });
|
|
22
|
+
* forceCloseDatabase(db); // invokes the event listener
|
|
23
|
+
* ```
|
|
24
|
+
* @param db
|
|
25
|
+
*/
|
|
26
|
+
function forceCloseDatabase(db) {
|
|
27
|
+
(0, _closeConnection.default)(db, true);
|
|
28
|
+
}
|
|
29
|
+
module.exports = exports.default;
|
package/build/cjs/index.js
CHANGED
|
@@ -76,6 +76,12 @@ Object.defineProperty(exports, "IDBVersionChangeEvent", {
|
|
|
76
76
|
}
|
|
77
77
|
});
|
|
78
78
|
exports.default = void 0;
|
|
79
|
+
Object.defineProperty(exports, "forceCloseDatabase", {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
get: function () {
|
|
82
|
+
return _forceCloseDatabase.default;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
79
85
|
Object.defineProperty(exports, "indexedDB", {
|
|
80
86
|
enumerable: true,
|
|
81
87
|
get: function () {
|
|
@@ -95,5 +101,6 @@ var _FDBRecord = _interopRequireDefault(require("./FDBRecord.js"));
|
|
|
95
101
|
var _FDBRequest = _interopRequireDefault(require("./FDBRequest.js"));
|
|
96
102
|
var _FDBTransaction = _interopRequireDefault(require("./FDBTransaction.js"));
|
|
97
103
|
var _FDBVersionChangeEvent = _interopRequireDefault(require("./FDBVersionChangeEvent.js"));
|
|
104
|
+
var _forceCloseDatabase = _interopRequireDefault(require("./forceCloseDatabase.js"));
|
|
98
105
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
99
106
|
var _default = exports.default = _fakeIndexedDB.default;
|
package/build/cjs/lib/Index.js
CHANGED
|
@@ -16,13 +16,13 @@ class Index {
|
|
|
16
16
|
// Initialized should be used to decide whether to throw an error or abort the versionchange transaction when there is a
|
|
17
17
|
// constraint
|
|
18
18
|
initialized = false;
|
|
19
|
-
records = new _RecordStore.default();
|
|
20
19
|
constructor(rawObjectStore, name, keyPath, multiEntry, unique) {
|
|
21
20
|
this.rawObjectStore = rawObjectStore;
|
|
22
21
|
this.name = name;
|
|
23
22
|
this.keyPath = keyPath;
|
|
24
23
|
this.multiEntry = multiEntry;
|
|
25
24
|
this.unique = unique;
|
|
25
|
+
this.records = new _RecordStore.default(unique);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-retrieving-a-value-from-an-index
|
|
@@ -133,13 +133,13 @@ class Index {
|
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
135
|
if (!this.multiEntry || !Array.isArray(indexKey)) {
|
|
136
|
-
this.records.
|
|
136
|
+
this.records.put({
|
|
137
137
|
key: indexKey,
|
|
138
138
|
value: newRecord.key
|
|
139
139
|
});
|
|
140
140
|
} else {
|
|
141
141
|
for (const individualIndexKey of indexKey) {
|
|
142
|
-
this.records.
|
|
142
|
+
this.records.put({
|
|
143
143
|
key: individualIndexKey,
|
|
144
144
|
value: newRecord.key
|
|
145
145
|
});
|
|
@@ -13,7 +13,7 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
|
|
|
13
13
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-object-store
|
|
14
14
|
class ObjectStore {
|
|
15
15
|
deleted = false;
|
|
16
|
-
records = new _RecordStore.default();
|
|
16
|
+
records = new _RecordStore.default(true);
|
|
17
17
|
rawIndexes = new Map();
|
|
18
18
|
constructor(rawDatabase, name, keyPath, autoIncrement) {
|
|
19
19
|
this.rawDatabase = rawDatabase;
|
|
@@ -130,20 +130,26 @@ class ObjectStore {
|
|
|
130
130
|
} else if (this.keyGenerator !== null && typeof newRecord.key === "number") {
|
|
131
131
|
this.keyGenerator.setIfLarger(newRecord.key);
|
|
132
132
|
}
|
|
133
|
-
const existingRecord = this.records.
|
|
134
|
-
if (existingRecord) {
|
|
135
|
-
if (noOverwrite) {
|
|
136
|
-
throw new _errors.ConstraintError();
|
|
137
|
-
}
|
|
138
|
-
this.deleteRecord(newRecord.key, rollbackLog);
|
|
139
|
-
}
|
|
140
|
-
this.records.add(newRecord);
|
|
133
|
+
const existingRecord = this.records.put(newRecord, noOverwrite);
|
|
141
134
|
if (rollbackLog) {
|
|
142
135
|
rollbackLog.push(() => {
|
|
143
|
-
|
|
136
|
+
if (existingRecord) {
|
|
137
|
+
// overwrite on rollback
|
|
138
|
+
this.storeRecord(existingRecord, false);
|
|
139
|
+
} else {
|
|
140
|
+
// delete on rollback
|
|
141
|
+
this.deleteRecord(newRecord.key);
|
|
142
|
+
}
|
|
144
143
|
});
|
|
145
144
|
}
|
|
146
145
|
|
|
146
|
+
// Delete existing indexes
|
|
147
|
+
if (existingRecord) {
|
|
148
|
+
for (const rawIndex of this.rawIndexes.values()) {
|
|
149
|
+
rawIndex.records.deleteByValue(newRecord.key);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
147
153
|
// Update indexes
|
|
148
154
|
for (const rawIndex of this.rawIndexes.values()) {
|
|
149
155
|
if (rawIndex.initialized) {
|
|
@@ -183,6 +189,10 @@ class ObjectStore {
|
|
|
183
189
|
}
|
|
184
190
|
}
|
|
185
191
|
count(range) {
|
|
192
|
+
// optimization: if there is no range, or if the range is everything, then we can just count the total size
|
|
193
|
+
if (range === undefined || range.lower === undefined && range.upper === undefined) {
|
|
194
|
+
return this.records.size();
|
|
195
|
+
}
|
|
186
196
|
let count = 0;
|
|
187
197
|
|
|
188
198
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -5,131 +5,58 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
var _FDBKeyRange = _interopRequireDefault(require("../FDBKeyRange.js"));
|
|
8
|
-
var _binarySearch = require("./binarySearch.js");
|
|
9
8
|
var _cmp = _interopRequireDefault(require("./cmp.js"));
|
|
9
|
+
var _binarySearchTree = _interopRequireDefault(require("./binarySearchTree.js"));
|
|
10
10
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
class RecordStore {
|
|
12
|
-
|
|
12
|
+
constructor(keysAreUnique) {
|
|
13
|
+
this.keysAreUnique = keysAreUnique;
|
|
14
|
+
this.records = new _binarySearchTree.default(this.keysAreUnique);
|
|
15
|
+
}
|
|
13
16
|
get(key) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
return (0, _binarySearch.getByKey)(this.records, key);
|
|
17
|
+
const range = key instanceof _FDBKeyRange.default ? key : _FDBKeyRange.default.only(key);
|
|
18
|
+
return this.records.getRecords(range).next().value;
|
|
18
19
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// If no matching key, add to end
|
|
28
|
-
i = this.records.length;
|
|
29
|
-
} else {
|
|
30
|
-
// If matching key, advance to appropriate position based on value (used in indexes)
|
|
31
|
-
while (i < this.records.length && (0, _cmp.default)(this.records[i].key, newRecord.key) === 0) {
|
|
32
|
-
if ((0, _cmp.default)(this.records[i].value, newRecord.value) !== -1) {
|
|
33
|
-
// Record value >= newRecord value, so insert here
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
i += 1; // Look at next record
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
this.records.splice(i, 0, newRecord);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Put a new record, and return the overwritten record if an overwrite occurred.
|
|
23
|
+
* @param newRecord
|
|
24
|
+
* @param noOverwrite - throw a ConstraintError in case of overwrite
|
|
25
|
+
*/
|
|
26
|
+
put(newRecord, noOverwrite = false) {
|
|
27
|
+
return this.records.put(newRecord, noOverwrite);
|
|
41
28
|
}
|
|
42
29
|
delete(key) {
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (idx === -1) {
|
|
48
|
-
break;
|
|
49
|
-
}
|
|
50
|
-
deletedRecords.push(this.records[idx]);
|
|
51
|
-
this.records.splice(idx, 1);
|
|
30
|
+
const range = key instanceof _FDBKeyRange.default ? key : _FDBKeyRange.default.only(key);
|
|
31
|
+
const deletedRecords = [...this.records.getRecords(range)];
|
|
32
|
+
for (const record of deletedRecords) {
|
|
33
|
+
this.records.delete(record);
|
|
52
34
|
}
|
|
53
35
|
return deletedRecords;
|
|
54
36
|
}
|
|
55
37
|
deleteByValue(key) {
|
|
56
38
|
const range = key instanceof _FDBKeyRange.default ? key : _FDBKeyRange.default.only(key);
|
|
57
39
|
const deletedRecords = [];
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
40
|
+
for (const record of this.records.getAllRecords()) {
|
|
41
|
+
if (range.includes(record.value)) {
|
|
42
|
+
this.records.delete(record);
|
|
61
43
|
deletedRecords.push(record);
|
|
62
44
|
}
|
|
63
|
-
|
|
64
|
-
});
|
|
45
|
+
}
|
|
65
46
|
return deletedRecords;
|
|
66
47
|
}
|
|
67
48
|
clear() {
|
|
68
|
-
const deletedRecords = this.records.
|
|
69
|
-
this.records =
|
|
49
|
+
const deletedRecords = [...this.records.getAllRecords()];
|
|
50
|
+
this.records = new _binarySearchTree.default(this.keysAreUnique);
|
|
70
51
|
return deletedRecords;
|
|
71
52
|
}
|
|
72
53
|
values(range, direction = "next") {
|
|
54
|
+
const descending = direction === "prev" || direction === "prevunique";
|
|
55
|
+
const records = range ? this.records.getRecords(range, descending) : this.records.getAllRecords(descending);
|
|
73
56
|
return {
|
|
74
57
|
[Symbol.iterator]: () => {
|
|
75
|
-
let i;
|
|
76
|
-
if (direction === "next" || direction === "nextunique") {
|
|
77
|
-
i = 0;
|
|
78
|
-
if (range !== undefined && range.lower !== undefined) {
|
|
79
|
-
while (this.records[i] !== undefined) {
|
|
80
|
-
const cmpResult = (0, _cmp.default)(this.records[i].key, range.lower);
|
|
81
|
-
if (cmpResult === 1 || cmpResult === 0 && !range.lowerOpen) {
|
|
82
|
-
break;
|
|
83
|
-
}
|
|
84
|
-
i += 1;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
} else {
|
|
88
|
-
i = this.records.length - 1;
|
|
89
|
-
if (range !== undefined && range.upper !== undefined) {
|
|
90
|
-
while (this.records[i] !== undefined) {
|
|
91
|
-
const cmpResult = (0, _cmp.default)(this.records[i].key, range.upper);
|
|
92
|
-
if (cmpResult === -1 || cmpResult === 0 && !range.upperOpen) {
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
i -= 1;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
58
|
const next = () => {
|
|
100
|
-
|
|
101
|
-
let value;
|
|
102
|
-
if (direction === "next" || direction === "nextunique") {
|
|
103
|
-
value = this.records[i];
|
|
104
|
-
done = i >= this.records.length;
|
|
105
|
-
i += 1;
|
|
106
|
-
if (!done && range !== undefined && range.upper !== undefined) {
|
|
107
|
-
const cmpResult = (0, _cmp.default)(value.key, range.upper);
|
|
108
|
-
done = cmpResult === 1 || cmpResult === 0 && range.upperOpen;
|
|
109
|
-
if (done) {
|
|
110
|
-
value = undefined;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
} else {
|
|
114
|
-
value = this.records[i];
|
|
115
|
-
done = i < 0;
|
|
116
|
-
i -= 1;
|
|
117
|
-
if (!done && range !== undefined && range.lower !== undefined) {
|
|
118
|
-
const cmpResult = (0, _cmp.default)(value.key, range.lower);
|
|
119
|
-
done = cmpResult === -1 || cmpResult === 0 && range.lowerOpen;
|
|
120
|
-
if (done) {
|
|
121
|
-
value = undefined;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// The weird "as IteratorResult<Record>" is needed because of
|
|
127
|
-
// https://github.com/Microsoft/TypeScript/issues/11375 and
|
|
128
|
-
// https://github.com/Microsoft/TypeScript/issues/2983
|
|
129
|
-
return {
|
|
130
|
-
done,
|
|
131
|
-
value
|
|
132
|
-
};
|
|
59
|
+
return records.next();
|
|
133
60
|
};
|
|
134
61
|
if (direction === "next" || direction === "prev") {
|
|
135
62
|
return {
|
|
@@ -137,59 +64,49 @@ class RecordStore {
|
|
|
137
64
|
};
|
|
138
65
|
}
|
|
139
66
|
|
|
140
|
-
// peek at the next value without incrementing the iterator
|
|
141
|
-
const peek = () => {
|
|
142
|
-
const iOriginal = i;
|
|
143
|
-
const result = next();
|
|
144
|
-
i = iOriginal;
|
|
145
|
-
return result;
|
|
146
|
-
};
|
|
147
|
-
|
|
148
67
|
// For nextunique/prevunique, return an iterator that skips seen values
|
|
149
|
-
// Note that we must
|
|
68
|
+
// Note that we must return the _lowest_ value regardless of direction:
|
|
150
69
|
// > Iterating with "prevunique" visits the same records that "nextunique"
|
|
151
70
|
// > visits, but in reverse order.
|
|
152
71
|
// https://w3c.github.io/IndexedDB/#dom-idbcursordirection-prevunique
|
|
153
|
-
|
|
72
|
+
if (direction === "nextunique") {
|
|
73
|
+
let previousValue = undefined;
|
|
74
|
+
return {
|
|
75
|
+
next: () => {
|
|
76
|
+
let current = next();
|
|
77
|
+
// for nextunique, continue if we already emitted the lowest unique value
|
|
78
|
+
while (!current.done && previousValue !== undefined && (0, _cmp.default)(previousValue.key, current.value.key) === 0) {
|
|
79
|
+
current = next();
|
|
80
|
+
}
|
|
81
|
+
previousValue = current.value;
|
|
82
|
+
return current;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// prevunique is a bit more complex due to needing to check the next value, which
|
|
88
|
+
// invokes the iterable, so we need to keep a buffer of one "lookahead" result
|
|
89
|
+
let current = next();
|
|
90
|
+
let nextResult = next();
|
|
154
91
|
return {
|
|
155
92
|
next: () => {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
value
|
|
161
|
-
} = current;
|
|
162
|
-
if (direction === "nextunique") {
|
|
163
|
-
// for nextunique, continue if we already emitted the lowest unique value
|
|
164
|
-
if (prevValue !== undefined && (0, _cmp.default)(prevValue.key, value.key) === 0) {
|
|
165
|
-
continue;
|
|
166
|
-
}
|
|
167
|
-
} else {
|
|
168
|
-
// for prevunique, we need to peek to see if the next value will be different,
|
|
169
|
-
// since we're trying to return the lowest unique value
|
|
170
|
-
const {
|
|
171
|
-
value: nextValue,
|
|
172
|
-
done: nextDone
|
|
173
|
-
} = peek();
|
|
174
|
-
if (!nextDone && (0, _cmp.default)(nextValue.key, value.key) === 0) {
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
prevValue = value;
|
|
179
|
-
return {
|
|
180
|
-
value,
|
|
181
|
-
done
|
|
182
|
-
};
|
|
93
|
+
while (!nextResult.done && (0, _cmp.default)(current.value.key, nextResult.value.key) === 0) {
|
|
94
|
+
// note we return the _lowest_ possible value, hence set the current
|
|
95
|
+
current = nextResult;
|
|
96
|
+
nextResult = next();
|
|
183
97
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
98
|
+
const result = current;
|
|
99
|
+
current = nextResult;
|
|
100
|
+
nextResult = next();
|
|
101
|
+
return result;
|
|
188
102
|
}
|
|
189
103
|
};
|
|
190
104
|
}
|
|
191
105
|
};
|
|
192
106
|
}
|
|
107
|
+
size() {
|
|
108
|
+
return this.records.size();
|
|
109
|
+
}
|
|
193
110
|
}
|
|
194
111
|
var _default = exports.default = RecordStore;
|
|
195
112
|
module.exports = exports.default;
|