fake-indexeddb 6.1.0 → 6.2.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/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
- globalVar.indexedDB = fakeIndexedDB;
26
- globalVar.IDBCursor = FDBCursor;
27
- globalVar.IDBCursorWithValue = FDBCursorWithValue;
28
- globalVar.IDBDatabase = FDBDatabase;
29
- globalVar.IDBFactory = FDBFactory;
30
- globalVar.IDBIndex = FDBIndex;
31
- globalVar.IDBKeyRange = FDBKeyRange;
32
- globalVar.IDBObjectStore = FDBObjectStore;
33
- globalVar.IDBOpenDBRequest = FDBOpenDBRequest;
34
- globalVar.IDBRecord = FDBRecord;
35
- globalVar.IDBRequest = FDBRequest;
36
- globalVar.IDBTransaction = FDBTransaction;
37
- globalVar.IDBVersionChangeEvent = FDBVersionChangeEvent;
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
- globalVar.indexedDB = fakeIndexedDB;
26
- globalVar.IDBCursor = FDBCursor;
27
- globalVar.IDBCursorWithValue = FDBCursorWithValue;
28
- globalVar.IDBDatabase = FDBDatabase;
29
- globalVar.IDBFactory = FDBFactory;
30
- globalVar.IDBIndex = FDBIndex;
31
- globalVar.IDBKeyRange = FDBKeyRange;
32
- globalVar.IDBObjectStore = FDBObjectStore;
33
- globalVar.IDBOpenDBRequest = FDBOpenDBRequest;
34
- globalVar.IDBRecord = FDBRecord;
35
- globalVar.IDBRequest = FDBRequest;
36
- globalVar.IDBTransaction = FDBTransaction;
37
- globalVar.IDBVersionChangeEvent = FDBVersionChangeEvent;
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
+ });
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = exports.closeConnection = void 0;
7
7
  var _FDBTransaction = _interopRequireDefault(require("./FDBTransaction.js"));
8
8
  var _errors = require("./lib/errors.js");
9
9
  var _FakeDOMStringList = _interopRequireDefault(require("./lib/FakeDOMStringList.js"));
@@ -11,6 +11,7 @@ var _FakeEventTarget = _interopRequireDefault(require("./lib/FakeEventTarget.js"
11
11
  var _ObjectStore = _interopRequireDefault(require("./lib/ObjectStore.js"));
12
12
  var _scheduling = require("./lib/scheduling.js");
13
13
  var _validateKeyPath = _interopRequireDefault(require("./lib/validateKeyPath.js"));
14
+ var _FakeEvent = _interopRequireDefault(require("./lib/FakeEvent.js"));
14
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
16
  const confirmActiveVersionchangeTransaction = database => {
16
17
  if (!database._runningVersionchangeTransaction) {
@@ -32,7 +33,7 @@ const confirmActiveVersionchangeTransaction = database => {
32
33
  };
33
34
 
34
35
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#database-closing-steps
35
- const closeConnection = connection => {
36
+ const closeConnection = (connection, forced = false) => {
36
37
  connection._closePending = true;
37
38
  const transactionsComplete = connection._rawDatabase.transactions.every(transaction => {
38
39
  return transaction._state === "finished";
@@ -42,14 +43,23 @@ const closeConnection = connection => {
42
43
  connection._rawDatabase.connections = connection._rawDatabase.connections.filter(otherConnection => {
43
44
  return connection !== otherConnection;
44
45
  });
46
+ if (forced) {
47
+ const event = new _FakeEvent.default("close", {
48
+ bubbles: false,
49
+ cancelable: false
50
+ });
51
+ event.eventPath = [];
52
+ connection.dispatchEvent(event);
53
+ }
45
54
  } else {
46
55
  (0, _scheduling.queueTask)(() => {
47
- closeConnection(connection);
56
+ closeConnection(connection, forced);
48
57
  });
49
58
  }
50
59
  };
51
60
 
52
61
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#database-interface
62
+ exports.closeConnection = closeConnection;
53
63
  class FDBDatabase extends _FakeEventTarget.default {
54
64
  _closePending = false;
55
65
  _closed = false;
@@ -121,7 +131,7 @@ class FDBDatabase extends _FakeEventTarget.default {
121
131
  this._rawDatabase.rawObjectStores.delete(name);
122
132
  transaction._objectStoresCache.delete(name);
123
133
  }
124
- transaction(storeNames, mode) {
134
+ transaction(storeNames, mode, options) {
125
135
  mode = mode !== undefined ? mode : "readonly";
126
136
  if (mode !== "readonly" && mode !== "readwrite" && mode !== "versionchange") {
127
137
  throw new TypeError("Invalid mode: " + mode);
@@ -146,7 +156,16 @@ class FDBDatabase extends _FakeEventTarget.default {
146
156
  throw new _errors.NotFoundError("No objectStore named " + storeName + " in this database");
147
157
  }
148
158
  }
149
- const tx = new _FDBTransaction.default(storeNames, mode, this);
159
+
160
+ // the actual algo is more complex but this passes the IDB tests: https://webidl.spec.whatwg.org/#es-dictionary
161
+ const durability = options?.durability ?? "default";
162
+ // invalid enums throw a TypeError: https://webidl.spec.whatwg.org/#es-enumeration
163
+ if (durability !== "default" && durability !== "strict" && durability !== "relaxed") {
164
+ throw new TypeError(
165
+ // based on Firefox's error message
166
+ `'${durability}' (value of 'durability' member of IDBTransactionOptions) ` + `is not a valid value for enumeration IDBTransactionDurability`);
167
+ }
168
+ const tx = new _FDBTransaction.default(storeNames, mode, durability, this);
150
169
  this._rawDatabase.transactions.push(tx);
151
170
  this._rawDatabase.processTransactions(); // See if can start right away (async)
152
171
 
@@ -159,5 +178,4 @@ class FDBDatabase extends _FakeEventTarget.default {
159
178
  return "IDBDatabase";
160
179
  }
161
180
  }
162
- var _default = exports.default = FDBDatabase;
163
- module.exports = exports.default;
181
+ var _default = exports.default = FDBDatabase;
@@ -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,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = forceCloseDatabase;
7
+ var _FDBDatabase = require("./FDBDatabase.js");
8
+ /**
9
+ * Forcibly closes a database. This simulates a database being closed due to abnormal reasons, such as
10
+ * using DevTools to clear data while a database is open. Spec-wise, this is equivalent to
11
+ * [closing a database connection](https://www.w3.org/TR/IndexedDB/#closing-connection) with the `forced flag`
12
+ * set to true.
13
+ *
14
+ * Use this API if you want to have more test coverage for unusual IndexedDB events, such as a database firing
15
+ * the "close" event:
16
+ *
17
+ * ```js
18
+ * db.addEventListener("close", () => {
19
+ * console.log("Forcibly closed!");
20
+ * });
21
+ * forceCloseDatabase(db); // invokes the event listener
22
+ * ```
23
+ * @param db
24
+ */
25
+ function forceCloseDatabase(db) {
26
+ (0, _FDBDatabase.closeConnection)(db, true);
27
+ }
28
+ module.exports = exports.default;
@@ -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;
@@ -27,6 +27,7 @@ const invokeEventListeners = (event, obj) => {
27
27
  const typeToProp = {
28
28
  abort: "onabort",
29
29
  blocked: "onblocked",
30
+ close: "onclose",
30
31
  complete: "oncomplete",
31
32
  error: "onerror",
32
33
  success: "onsuccess",
@@ -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.add({
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.add({
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.get(newRecord.key);
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
- this.deleteRecord(newRecord.key);
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
- records = [];
12
+ constructor(keysAreUnique) {
13
+ this.keysAreUnique = keysAreUnique;
14
+ this.records = new _binarySearchTree.default(this.keysAreUnique);
15
+ }
13
16
  get(key) {
14
- if (key instanceof _FDBKeyRange.default) {
15
- return (0, _binarySearch.getByKeyRange)(this.records, key);
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
- add(newRecord) {
20
- // Find where to put it so it's sorted by key
21
- let i;
22
- if (this.records.length === 0) {
23
- i = 0;
24
- } else {
25
- i = (0, _binarySearch.getIndexByKeyGTE)(this.records, newRecord.key);
26
- if (i === -1) {
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 deletedRecords = [];
44
- const isRange = key instanceof _FDBKeyRange.default;
45
- while (true) {
46
- const idx = isRange ? (0, _binarySearch.getIndexByKeyRange)(this.records, key) : (0, _binarySearch.getIndexByKey)(this.records, key);
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
- this.records = this.records.filter(record => {
59
- const shouldDelete = range.includes(record.value);
60
- if (shouldDelete) {
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
- return !shouldDelete;
64
- });
45
+ }
65
46
  return deletedRecords;
66
47
  }
67
48
  clear() {
68
- const deletedRecords = this.records.slice();
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
- let done;
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 resturn the _lowest_ value regardless of direction:
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
- let prevValue = undefined;
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
- let current;
157
- while (!(current = next()).done) {
158
- const {
159
- done,
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
- return {
185
- value: undefined,
186
- done: true
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;