fake-indexeddb 4.0.2 → 5.0.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.
Files changed (64) hide show
  1. package/README.md +0 -6
  2. package/build/cjs/FDBCursor.js +39 -159
  3. package/build/cjs/FDBCursorWithValue.js +1 -9
  4. package/build/cjs/FDBDatabase.js +9 -72
  5. package/build/cjs/FDBFactory.js +23 -66
  6. package/build/cjs/FDBIndex.js +17 -73
  7. package/build/cjs/FDBKeyRange.js +3 -28
  8. package/build/cjs/FDBObjectStore.js +25 -145
  9. package/build/cjs/FDBOpenDBRequest.js +1 -8
  10. package/build/cjs/FDBRequest.js +1 -15
  11. package/build/cjs/FDBTransaction.js +26 -64
  12. package/build/cjs/FDBVersionChangeEvent.js +1 -8
  13. package/build/cjs/fakeIndexedDB.js +1 -5
  14. package/build/cjs/index.js +1 -16
  15. package/build/cjs/lib/Database.js +1 -11
  16. package/build/cjs/lib/FakeDOMStringList.js +5 -13
  17. package/build/cjs/lib/FakeEvent.js +3 -10
  18. package/build/cjs/lib/FakeEventTarget.js +15 -32
  19. package/build/cjs/lib/Index.js +14 -46
  20. package/build/cjs/lib/KeyGenerator.js +3 -12
  21. package/build/cjs/lib/ObjectStore.js +24 -63
  22. package/build/cjs/lib/RecordStore.js +4 -42
  23. package/build/cjs/lib/binarySearch.js +5 -24
  24. package/build/cjs/lib/canInjectKey.js +2 -14
  25. package/build/cjs/lib/cmp.js +3 -29
  26. package/build/cjs/lib/enforceRange.js +2 -6
  27. package/build/cjs/lib/errors.js +0 -32
  28. package/build/cjs/lib/extractKey.js +2 -17
  29. package/build/cjs/lib/scheduling.js +4 -7
  30. package/build/cjs/lib/validateKeyPath.js +4 -16
  31. package/build/cjs/lib/valueToKey.js +2 -15
  32. package/build/cjs/lib/valueToKeyRange.js +1 -11
  33. package/build/esm/FDBCursor.js +33 -144
  34. package/build/esm/FDBCursorWithValue.js +0 -5
  35. package/build/esm/FDBDatabase.js +8 -62
  36. package/build/esm/FDBFactory.js +22 -54
  37. package/build/esm/FDBIndex.js +16 -61
  38. package/build/esm/FDBKeyRange.js +4 -22
  39. package/build/esm/FDBObjectStore.js +23 -126
  40. package/build/esm/FDBOpenDBRequest.js +0 -4
  41. package/build/esm/FDBRequest.js +0 -10
  42. package/build/esm/FDBTransaction.js +25 -54
  43. package/build/esm/FDBVersionChangeEvent.js +0 -4
  44. package/build/esm/lib/Database.js +2 -8
  45. package/build/esm/lib/FakeDOMStringList.js +4 -9
  46. package/build/esm/lib/FakeEvent.js +2 -7
  47. package/build/esm/lib/FakeEventTarget.js +14 -29
  48. package/build/esm/lib/Index.js +14 -37
  49. package/build/esm/lib/KeyGenerator.js +2 -8
  50. package/build/esm/lib/ObjectStore.js +19 -51
  51. package/build/esm/lib/RecordStore.js +3 -36
  52. package/build/esm/lib/binarySearch.js +5 -18
  53. package/build/esm/lib/canInjectKey.js +1 -11
  54. package/build/esm/lib/cmp.js +2 -24
  55. package/build/esm/lib/enforceRange.js +1 -3
  56. package/build/esm/lib/errors.js +0 -11
  57. package/build/esm/lib/extractKey.js +3 -13
  58. package/build/esm/lib/scheduling.js +3 -3
  59. package/build/esm/lib/validateKeyPath.js +3 -13
  60. package/build/esm/lib/valueToKey.js +1 -12
  61. package/build/esm/lib/valueToKeyRange.js +2 -5
  62. package/package.json +30 -28
  63. package/build/cjs/lib/structuredClone.js +0 -30
  64. package/build/esm/lib/structuredClone.js +0 -18
@@ -5,7 +5,6 @@ import FakeDOMStringList from "./lib/FakeDOMStringList.js";
5
5
  import FakeEvent from "./lib/FakeEvent.js";
6
6
  import FakeEventTarget from "./lib/FakeEventTarget.js";
7
7
  import { queueTask } from "./lib/scheduling.js";
8
-
9
8
  // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#transaction
10
9
  class FDBTransaction extends FakeEventTarget {
11
10
  _state = "active";
@@ -17,34 +16,31 @@ class FDBTransaction extends FakeEventTarget {
17
16
  oncomplete = null;
18
17
  onerror = null;
19
18
  _requests = [];
20
-
21
19
  constructor(storeNames, mode, db) {
22
20
  super();
23
21
  this._scope = new Set(storeNames);
24
22
  this.mode = mode;
25
23
  this.db = db;
26
24
  this.objectStoreNames = new FakeDOMStringList(...Array.from(this._scope).sort());
27
- } // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-aborting-a-transaction
28
-
25
+ }
29
26
 
27
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-aborting-a-transaction
30
28
  _abort(errName) {
31
29
  for (const f of this._rollbackLog.reverse()) {
32
30
  f();
33
31
  }
34
-
35
32
  if (errName !== null) {
36
33
  const e = new Error();
37
34
  e.name = errName;
38
35
  this.error = e;
39
- } // Should this directly remove from _requests?
40
-
36
+ }
41
37
 
38
+ // Should this directly remove from _requests?
42
39
  for (const {
43
40
  request
44
41
  } of this._requests) {
45
42
  if (request.readyState !== "done") {
46
43
  request.readyState = "done"; // This will cancel execution of this request's operation
47
-
48
44
  if (request.source) {
49
45
  request.result = undefined;
50
46
  request.error = new AbortError();
@@ -57,7 +53,6 @@ class FDBTransaction extends FakeEventTarget {
57
53
  }
58
54
  }
59
55
  }
60
-
61
56
  queueTask(() => {
62
57
  const event = new FakeEvent("abort", {
63
58
  bubbles: true,
@@ -68,53 +63,42 @@ class FDBTransaction extends FakeEventTarget {
68
63
  });
69
64
  this._state = "finished";
70
65
  }
71
-
72
66
  abort() {
73
67
  if (this._state === "committing" || this._state === "finished") {
74
68
  throw new InvalidStateError();
75
69
  }
76
-
77
70
  this._state = "active";
78
-
79
71
  this._abort(null);
80
- } // http://w3c.github.io/IndexedDB/#dom-idbtransaction-objectstore
81
-
72
+ }
82
73
 
74
+ // http://w3c.github.io/IndexedDB/#dom-idbtransaction-objectstore
83
75
  objectStore(name) {
84
76
  if (this._state !== "active") {
85
77
  throw new InvalidStateError();
86
78
  }
87
-
88
79
  const objectStore = this._objectStoresCache.get(name);
89
-
90
80
  if (objectStore !== undefined) {
91
81
  return objectStore;
92
82
  }
93
-
94
83
  const rawObjectStore = this.db._rawDatabase.rawObjectStores.get(name);
95
-
96
84
  if (!this._scope.has(name) || rawObjectStore === undefined) {
97
85
  throw new NotFoundError();
98
86
  }
99
-
100
87
  const objectStore2 = new FDBObjectStore(this, rawObjectStore);
101
-
102
88
  this._objectStoresCache.set(name, objectStore2);
103
-
104
89
  return objectStore2;
105
- } // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-asynchronously-executing-a-request
106
-
90
+ }
107
91
 
92
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-asynchronously-executing-a-request
108
93
  _execRequestAsync(obj) {
109
94
  const source = obj.source;
110
95
  const operation = obj.operation;
111
- let request = obj.hasOwnProperty("request") ? obj.request : null;
112
-
96
+ let request = Object.hasOwn(obj, "request") ? obj.request : null;
113
97
  if (this._state !== "active") {
114
98
  throw new TransactionInactiveError();
115
- } // Request should only be passed for cursors
116
-
99
+ }
117
100
 
101
+ // Request should only be passed for cursors
118
102
  if (!request) {
119
103
  if (!source) {
120
104
  // Special requests like indexes that just need to run some code
@@ -125,32 +109,28 @@ class FDBTransaction extends FakeEventTarget {
125
109
  request.transaction = source.transaction;
126
110
  }
127
111
  }
128
-
129
112
  this._requests.push({
130
113
  operation,
131
114
  request
132
115
  });
133
-
134
116
  return request;
135
117
  }
136
-
137
118
  _start() {
138
- this._started = true; // Remove from request queue - cursor ones will be added back if necessary by cursor.continue and such
119
+ this._started = true;
139
120
 
121
+ // Remove from request queue - cursor ones will be added back if necessary by cursor.continue and such
140
122
  let operation;
141
123
  let request;
142
-
143
124
  while (this._requests.length > 0) {
144
- const r = this._requests.shift(); // This should only be false if transaction was aborted
145
-
125
+ const r = this._requests.shift();
146
126
 
127
+ // This should only be false if transaction was aborted
147
128
  if (r && r.request.readyState !== "done") {
148
129
  request = r.request;
149
130
  operation = r.operation;
150
131
  break;
151
132
  }
152
133
  }
153
-
154
134
  if (request && operation) {
155
135
  if (!request.source) {
156
136
  // Special requests like indexes that just need to run some code, with error handling already built into
@@ -159,17 +139,16 @@ class FDBTransaction extends FakeEventTarget {
159
139
  } else {
160
140
  let defaultAction;
161
141
  let event;
162
-
163
142
  try {
164
143
  const result = operation();
165
144
  request.readyState = "done";
166
145
  request.result = result;
167
- request.error = undefined; // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-fire-a-success-event
146
+ request.error = undefined;
168
147
 
148
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-fire-a-success-event
169
149
  if (this._state === "inactive") {
170
150
  this._state = "active";
171
151
  }
172
-
173
152
  event = new FakeEvent("success", {
174
153
  bubbles: false,
175
154
  cancelable: false
@@ -177,19 +156,18 @@ class FDBTransaction extends FakeEventTarget {
177
156
  } catch (err) {
178
157
  request.readyState = "done";
179
158
  request.result = undefined;
180
- request.error = err; // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-fire-an-error-event
159
+ request.error = err;
181
160
 
161
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-fire-an-error-event
182
162
  if (this._state === "inactive") {
183
163
  this._state = "active";
184
164
  }
185
-
186
165
  event = new FakeEvent("error", {
187
166
  bubbles: true,
188
167
  cancelable: true
189
168
  });
190
169
  defaultAction = this._abort.bind(this, err.name);
191
170
  }
192
-
193
171
  try {
194
172
  event.eventPath = [this.db, this];
195
173
  request.dispatchEvent(event);
@@ -197,47 +175,40 @@ class FDBTransaction extends FakeEventTarget {
197
175
  if (this._state !== "committing") {
198
176
  this._abort("AbortError");
199
177
  }
200
-
201
178
  throw err;
202
- } // Default action of event
203
-
179
+ }
204
180
 
181
+ // Default action of event
205
182
  if (!event.canceled) {
206
183
  if (defaultAction) {
207
184
  defaultAction();
208
185
  }
209
186
  }
210
- } // Give it another chance for new handlers to be set before finishing
211
-
187
+ }
212
188
 
189
+ // Give it another chance for new handlers to be set before finishing
213
190
  queueTask(this._start.bind(this));
214
191
  return;
215
- } // Check if transaction complete event needs to be fired
216
-
192
+ }
217
193
 
194
+ // Check if transaction complete event needs to be fired
218
195
  if (this._state !== "finished") {
219
196
  // Either aborted or committed already
220
197
  this._state = "finished";
221
-
222
198
  if (!this.error) {
223
199
  const event = new FakeEvent("complete");
224
200
  this.dispatchEvent(event);
225
201
  }
226
202
  }
227
203
  }
228
-
229
204
  commit() {
230
205
  if (this._state !== "active") {
231
206
  throw new InvalidStateError();
232
207
  }
233
-
234
208
  this._state = "committing";
235
209
  }
236
-
237
210
  toString() {
238
211
  return "[object IDBRequest]";
239
212
  }
240
-
241
213
  }
242
-
243
214
  export default FDBTransaction;
@@ -1,16 +1,12 @@
1
1
  import FakeEvent from "./lib/FakeEvent.js";
2
-
3
2
  class FDBVersionChangeEvent extends FakeEvent {
4
3
  constructor(type, parameters = {}) {
5
4
  super(type);
6
5
  this.newVersion = parameters.newVersion !== undefined ? parameters.newVersion : null;
7
6
  this.oldVersion = parameters.oldVersion !== undefined ? parameters.oldVersion : 0;
8
7
  }
9
-
10
8
  toString() {
11
9
  return "[object IDBVersionChangeEvent]";
12
10
  }
13
-
14
11
  }
15
-
16
12
  export default FDBVersionChangeEvent;
@@ -1,38 +1,32 @@
1
- import { queueTask } from "./scheduling.js"; // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-database
1
+ import { queueTask } from "./scheduling.js";
2
2
 
3
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-database
3
4
  class Database {
4
5
  deletePending = false;
5
6
  transactions = [];
6
7
  rawObjectStores = new Map();
7
8
  connections = [];
8
-
9
9
  constructor(name, version) {
10
10
  this.name = name;
11
11
  this.version = version;
12
12
  this.processTransactions = this.processTransactions.bind(this);
13
13
  }
14
-
15
14
  processTransactions() {
16
15
  queueTask(() => {
17
16
  const anyRunning = this.transactions.some(transaction => {
18
17
  return transaction._started && transaction._state !== "finished";
19
18
  });
20
-
21
19
  if (!anyRunning) {
22
20
  const next = this.transactions.find(transaction => {
23
21
  return !transaction._started && transaction._state !== "finished";
24
22
  });
25
-
26
23
  if (next) {
27
24
  next.addEventListener("complete", this.processTransactions);
28
25
  next.addEventListener("abort", this.processTransactions);
29
-
30
26
  next._start();
31
27
  }
32
28
  }
33
29
  });
34
30
  }
35
-
36
31
  }
37
-
38
32
  export default Database;
@@ -5,29 +5,25 @@ class FakeDOMStringList extends Array {
5
5
  return true;
6
6
  }
7
7
  }
8
-
9
8
  return false;
10
9
  }
11
-
12
10
  item(i) {
13
11
  if (i < 0 || i >= this.length) {
14
12
  return null;
15
13
  }
16
-
17
14
  return this[i];
18
- } // Used internally, should not be used by others. I could maybe get rid of these and replace rather than mutate, but too lazy to check the spec.
19
-
15
+ }
20
16
 
17
+ // Used internally, should not be used by others. I could maybe get rid of these and replace rather than mutate, but too lazy to check the spec.
21
18
  _push(...values) {
22
19
  return Array.prototype.push.call(this, ...values);
23
20
  }
24
-
25
21
  _sort(...values) {
26
22
  return Array.prototype.sort.call(this, ...values);
27
23
  }
24
+ }
28
25
 
29
- } // Would be nice to remove these properties to fix https://github.com/dumbmatter/fakeIndexedDB/issues/66 but for some reason it breaks Dexie - see test/dexie.js and FakeDOMStringList tests
30
-
26
+ // Would be nice to remove these properties to fix https://github.com/dumbmatter/fakeIndexedDB/issues/66 but for some reason it breaks Dexie - see test/dexie.js and FakeDOMStringList tests
31
27
  /*
32
28
  // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
33
29
  const arrayPropertiesToDelete = ["from", "isArray", "of"];
@@ -73,5 +69,4 @@ for (const property of arrayMethodsToDelete) {
73
69
  }
74
70
  */
75
71
 
76
-
77
72
  export default FakeDOMStringList;
@@ -3,8 +3,9 @@ class Event {
3
3
  NONE = 0;
4
4
  CAPTURING_PHASE = 1;
5
5
  AT_TARGET = 2;
6
- BUBBLING_PHASE = 3; // Flags
6
+ BUBBLING_PHASE = 3;
7
7
 
8
+ // Flags
8
9
  propagationStopped = false;
9
10
  immediatePropagationStopped = false;
10
11
  canceled = false;
@@ -16,28 +17,22 @@ class Event {
16
17
  defaultPrevented = false;
17
18
  isTrusted = false;
18
19
  timeStamp = Date.now();
19
-
20
20
  constructor(type, eventInitDict = {}) {
21
21
  this.type = type;
22
22
  this.bubbles = eventInitDict.bubbles !== undefined ? eventInitDict.bubbles : false;
23
23
  this.cancelable = eventInitDict.cancelable !== undefined ? eventInitDict.cancelable : false;
24
24
  }
25
-
26
25
  preventDefault() {
27
26
  if (this.cancelable) {
28
27
  this.canceled = true;
29
28
  }
30
29
  }
31
-
32
30
  stopPropagation() {
33
31
  this.propagationStopped = true;
34
32
  }
35
-
36
33
  stopImmediatePropagation() {
37
34
  this.propagationStopped = true;
38
35
  this.immediatePropagationStopped = true;
39
36
  }
40
-
41
37
  }
42
-
43
38
  export default Event;
@@ -1,24 +1,23 @@
1
1
  import { InvalidStateError } from "./errors.js";
2
-
3
2
  const stopped = (event, listener) => {
4
3
  return event.immediatePropagationStopped || event.eventPhase === event.CAPTURING_PHASE && listener.capture === false || event.eventPhase === event.BUBBLING_PHASE && listener.capture === true;
5
- }; // http://www.w3.org/TR/dom/#concept-event-listener-invoke
6
-
4
+ };
7
5
 
6
+ // http://www.w3.org/TR/dom/#concept-event-listener-invoke
8
7
  const invokeEventListeners = (event, obj) => {
9
- event.currentTarget = obj; // The callback might cause obj.listeners to mutate as we traverse it.
8
+ event.currentTarget = obj;
9
+
10
+ // The callback might cause obj.listeners to mutate as we traverse it.
10
11
  // Take a copy of the array so that nothing sneaks in and we don't lose
11
12
  // our place.
12
-
13
13
  for (const listener of obj.listeners.slice()) {
14
14
  if (event.type !== listener.type || stopped(event, listener)) {
15
15
  continue;
16
- } // @ts-ignore
17
-
16
+ }
18
17
 
18
+ // @ts-ignore
19
19
  listener.callback.call(event.currentTarget, event);
20
20
  }
21
-
22
21
  const typeToProp = {
23
22
  abort: "onabort",
24
23
  blocked: "onblocked",
@@ -29,29 +28,26 @@ const invokeEventListeners = (event, obj) => {
29
28
  versionchange: "onversionchange"
30
29
  };
31
30
  const prop = typeToProp[event.type];
32
-
33
31
  if (prop === undefined) {
34
32
  throw new Error(`Unknown event type: "${event.type}"`);
35
33
  }
36
-
37
34
  const callback = event.currentTarget[prop];
38
-
39
35
  if (callback) {
40
36
  const listener = {
41
37
  callback,
42
38
  capture: false,
43
39
  type: event.type
44
40
  };
45
-
46
41
  if (!stopped(event, listener)) {
47
42
  // @ts-ignore
48
43
  listener.callback.call(event.currentTarget, event);
49
44
  }
50
45
  }
51
46
  };
52
-
53
47
  class FakeEventTarget {
54
- listeners = []; // These will be overridden in individual subclasses and made not readonly
48
+ listeners = [];
49
+
50
+ // These will be overridden in individual subclasses and made not readonly
55
51
 
56
52
  addEventListener(type, callback, capture = false) {
57
53
  this.listeners.push({
@@ -60,60 +56,49 @@ class FakeEventTarget {
60
56
  type
61
57
  });
62
58
  }
63
-
64
59
  removeEventListener(type, callback, capture = false) {
65
60
  const i = this.listeners.findIndex(listener => {
66
61
  return listener.type === type && listener.callback === callback && listener.capture === capture;
67
62
  });
68
63
  this.listeners.splice(i, 1);
69
- } // http://www.w3.org/TR/dom/#dispatching-events
70
-
64
+ }
71
65
 
66
+ // http://www.w3.org/TR/dom/#dispatching-events
72
67
  dispatchEvent(event) {
73
68
  if (event.dispatched || !event.initialized) {
74
69
  throw new InvalidStateError("The object is in an invalid state.");
75
70
  }
76
-
77
71
  event.isTrusted = false;
78
72
  event.dispatched = true;
79
- event.target = this; // NOT SURE WHEN THIS SHOULD BE SET event.eventPath = [];
73
+ event.target = this;
74
+ // NOT SURE WHEN THIS SHOULD BE SET event.eventPath = [];
80
75
 
81
76
  event.eventPhase = event.CAPTURING_PHASE;
82
-
83
77
  for (const obj of event.eventPath) {
84
78
  if (!event.propagationStopped) {
85
79
  invokeEventListeners(event, obj);
86
80
  }
87
81
  }
88
-
89
82
  event.eventPhase = event.AT_TARGET;
90
-
91
83
  if (!event.propagationStopped) {
92
84
  invokeEventListeners(event, event.target);
93
85
  }
94
-
95
86
  if (event.bubbles) {
96
87
  event.eventPath.reverse();
97
88
  event.eventPhase = event.BUBBLING_PHASE;
98
-
99
89
  for (const obj of event.eventPath) {
100
90
  if (!event.propagationStopped) {
101
91
  invokeEventListeners(event, obj);
102
92
  }
103
93
  }
104
94
  }
105
-
106
95
  event.dispatched = false;
107
96
  event.eventPhase = event.NONE;
108
97
  event.currentTarget = null;
109
-
110
98
  if (event.canceled) {
111
99
  return false;
112
100
  }
113
-
114
101
  return true;
115
102
  }
116
-
117
103
  }
118
-
119
104
  export default FakeEventTarget;
@@ -1,78 +1,68 @@
1
1
  import { ConstraintError } from "./errors.js";
2
2
  import extractKey from "./extractKey.js";
3
3
  import RecordStore from "./RecordStore.js";
4
- import structuredClone from "./structuredClone.js";
5
- import valueToKey from "./valueToKey.js"; // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-index
4
+ import valueToKey from "./valueToKey.js";
6
5
 
6
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-index
7
7
  class Index {
8
- deleted = false; // Initialized should be used to decide whether to throw an error or abort the versionchange transaction when there is a
8
+ deleted = false;
9
+ // Initialized should be used to decide whether to throw an error or abort the versionchange transaction when there is a
9
10
  // constraint
10
-
11
11
  initialized = false;
12
12
  records = new RecordStore();
13
-
14
13
  constructor(rawObjectStore, name, keyPath, multiEntry, unique) {
15
14
  this.rawObjectStore = rawObjectStore;
16
15
  this.name = name;
17
16
  this.keyPath = keyPath;
18
17
  this.multiEntry = multiEntry;
19
18
  this.unique = unique;
20
- } // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-retrieving-a-value-from-an-index
21
-
19
+ }
22
20
 
21
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-retrieving-a-value-from-an-index
23
22
  getKey(key) {
24
23
  const record = this.records.get(key);
25
24
  return record !== undefined ? record.value : undefined;
26
- } // http://w3c.github.io/IndexedDB/#retrieve-multiple-referenced-values-from-an-index
27
-
25
+ }
28
26
 
27
+ // http://w3c.github.io/IndexedDB/#retrieve-multiple-referenced-values-from-an-index
29
28
  getAllKeys(range, count) {
30
29
  if (count === undefined || count === 0) {
31
30
  count = Infinity;
32
31
  }
33
-
34
32
  const records = [];
35
-
36
33
  for (const record of this.records.values(range)) {
37
34
  records.push(structuredClone(record.value));
38
-
39
35
  if (records.length >= count) {
40
36
  break;
41
37
  }
42
38
  }
43
-
44
39
  return records;
45
- } // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#index-referenced-value-retrieval-operation
46
-
40
+ }
47
41
 
42
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#index-referenced-value-retrieval-operation
48
43
  getValue(key) {
49
44
  const record = this.records.get(key);
50
45
  return record !== undefined ? this.rawObjectStore.getValue(record.value) : undefined;
51
- } // http://w3c.github.io/IndexedDB/#retrieve-multiple-referenced-values-from-an-index
52
-
46
+ }
53
47
 
48
+ // http://w3c.github.io/IndexedDB/#retrieve-multiple-referenced-values-from-an-index
54
49
  getAllValues(range, count) {
55
50
  if (count === undefined || count === 0) {
56
51
  count = Infinity;
57
52
  }
58
-
59
53
  const records = [];
60
-
61
54
  for (const record of this.records.values(range)) {
62
55
  records.push(this.rawObjectStore.getValue(record.value));
63
-
64
56
  if (records.length >= count) {
65
57
  break;
66
58
  }
67
59
  }
68
-
69
60
  return records;
70
- } // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-storing-a-record-into-an-object-store (step 7)
71
-
61
+ }
72
62
 
63
+ // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-storing-a-record-into-an-object-store (step 7)
73
64
  storeRecord(newRecord) {
74
65
  let indexKey;
75
-
76
66
  try {
77
67
  indexKey = extractKey(this.keyPath, newRecord.value);
78
68
  } catch (err) {
@@ -80,10 +70,8 @@ class Index {
80
70
  // Invalid key is not an actual error, just means we do not store an entry in this index
81
71
  return;
82
72
  }
83
-
84
73
  throw err;
85
74
  }
86
-
87
75
  if (!this.multiEntry || !Array.isArray(indexKey)) {
88
76
  try {
89
77
  valueToKey(indexKey);
@@ -94,7 +82,6 @@ class Index {
94
82
  // remove any elements from index key that are not valid keys and remove any duplicate elements from index
95
83
  // key such that only one instance of the duplicate value remains.
96
84
  const keep = [];
97
-
98
85
  for (const part of indexKey) {
99
86
  if (keep.indexOf(part) < 0) {
100
87
  try {
@@ -104,14 +91,11 @@ class Index {
104
91
  }
105
92
  }
106
93
  }
107
-
108
94
  indexKey = keep;
109
95
  }
110
-
111
96
  if (!this.multiEntry || !Array.isArray(indexKey)) {
112
97
  if (this.unique) {
113
98
  const existingRecord = this.records.get(indexKey);
114
-
115
99
  if (existingRecord) {
116
100
  throw new ConstraintError();
117
101
  }
@@ -120,14 +104,12 @@ class Index {
120
104
  if (this.unique) {
121
105
  for (const individualIndexKey of indexKey) {
122
106
  const existingRecord = this.records.get(individualIndexKey);
123
-
124
107
  if (existingRecord) {
125
108
  throw new ConstraintError();
126
109
  }
127
110
  }
128
111
  }
129
112
  }
130
-
131
113
  if (!this.multiEntry || !Array.isArray(indexKey)) {
132
114
  this.records.add({
133
115
  key: indexKey,
@@ -142,12 +124,10 @@ class Index {
142
124
  }
143
125
  }
144
126
  }
145
-
146
127
  initialize(transaction) {
147
128
  if (this.initialized) {
148
129
  throw new Error("Index already initialized");
149
130
  }
150
-
151
131
  transaction._execRequestAsync({
152
132
  operation: () => {
153
133
  try {
@@ -155,7 +135,6 @@ class Index {
155
135
  for (const record of this.rawObjectStore.records.values()) {
156
136
  this.storeRecord(record);
157
137
  }
158
-
159
138
  this.initialized = true;
160
139
  } catch (err) {
161
140
  // console.error(err);
@@ -165,7 +144,5 @@ class Index {
165
144
  source: null
166
145
  });
167
146
  }
168
-
169
147
  }
170
-
171
148
  export default Index;