-
Notifications
You must be signed in to change notification settings - Fork 6
/
IDBObjectStore-impl.js
396 lines (362 loc) · 15.6 KB
/
IDBObjectStore-impl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
const extractor = require('./extractor')
const { vivify } = require('./setter')
const { valuify } = require('./value')
const Verbatim = require('verbatim')
const assert = require('assert')
const convert = require('./convert')
const IDBRequest = require('./living/generated/IDBRequest')
const IDBIndex = require('./living/generated/IDBIndex')
const IDBKeyRange = require('./living/generated/IDBKeyRange')
const IDBCursor = require('./living/generated/IDBCursor')
const IDBCursorWithValue = require('./living/generated/IDBCursorWithValue')
const DOMStringList = require('./living/generated/DOMStringList')
const DOMException = require('domexception/lib/DOMException')
const webidl = require('./living/generated/utils')
const structuredClone = require('./structuredClone')
// Not sure if IndexedDB API expects the same object store returned from every
// transaction, no idea how that would work, so this isn't an object that
// implements a store, it is an object that is an interface to a store for a
// specific transaction.
class IDBObjectStoreImpl {
// The loop is the event loop for the transaction associated with this
// object store, we push messages with a request object. The work is
// performed by the loop object. The loop object is run after the locking is
// performed.
constructor (globalObject, [], { transaction, schema, name, constructing = false }) {
assert(schema, 'schema is null')
this._globalObject = globalObject
assert(typeof transaction == 'object')
this._transaction = transaction
this._schema = schema
this._store = this._schema.getObjectStore(name)
this.keyPath = JSON.parse(JSON.stringify(this._store.keyPath))
this.autoIncrement = this._store.autoIncrement != null
this._constructing = constructing
}
// Common `isDeleted` interface for inspection from an outstanding cursor.
_isDeleted () {
return this._schema.isDeleted(this._store)
}
get transaction () {
return webidl.wrapperForImpl(this._transaction)
}
set name (to) {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction.mode != 'versionchange') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (this._store.name != to) {
if (this._schema.getObjectStore(to) != null) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'ConstraintError' ], {})
}
const from = this._store.name
this._schema.rename(from, to)
this._transaction._queue.push({
method: 'rename',
type: 'store',
store: JSON.parse(JSON.stringify(this._store))
})
}
}
get name () {
return this._store.name
}
get indexNames () {
return DOMStringList.create(this._globalObject, [], { array: this._schema.getIndexNames(this._store.name) })
}
_addOrPut (value, key, overwrite) {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (this._transaction.mode == 'readonly') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'ReadOnlyError' ], {})
}
if (key != null && this._store.keyPath != null) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'DataError' ], {})
}
if (key == null && this._store.autoIncrement == null && this._store.keyPath == null) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'DataError' ], {})
}
try {
this._transaction._state = 'inactive'
value = structuredClone(this._globalObject, value)
} finally {
this._transaction._state = 'active'
}
if (this._store.keyPath != null) {
key = this._schema.getExtractor(this._store.id)(value)
}
if (this._store.autoIncrement != null) {
if (key == null) {
key = this._store.autoIncrement++
if (this._store.keyPath != null) {
vivify(this._globalObject, value, this._store.keyPath, key)
}
} else {
key = valuify(this._globalObject, key)
if (typeof key == 'number' && key >= this._store.autoIncrement) {
this._store.autoIncrement = Math.floor(key + 1)
}
}
} else {
key = valuify(this._globalObject, key)
}
const request = IDBRequest.createImpl(this._globalObject, [], { transaction: this._transaction, source: this })
this._transaction._queue.push({
method: 'set',
request,
store: JSON.parse(JSON.stringify(this._store)),
key,
value,
overwrite
})
return webidl.wrapperForImpl(request)
}
put (value, key = null) {
return this._addOrPut(value, key, true)
}
add (value, key = null) {
return this._addOrPut(value, key, false)
}
delete (query) {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (this._transaction.mode == 'readonly') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'ReadOnlyError' ], {})
}
if (query != null && ! (query instanceof this._globalObject.IDBKeyRange)) {
query = this._globalObject.IDBKeyRange.only(convert.key(this._globalObject, query))
}
const request = IDBRequest.createImpl(this._globalObject, [], {
// **TODO** parent is always transaction, so...
transaction: this._transaction, source: this
})
this._transaction._queue.push({
method: 'delete',
store: JSON.parse(JSON.stringify(this._store)),
request,
query
})
return request
}
clear () {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (this._transaction.mode == 'readonly') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'ReadOnlyError' ], {})
}
const request = IDBRequest.createImpl(this._globalObject, [], { transaction: this._transaction, source: this })
this._transaction._queue.push({
method: 'clear',
request: request,
store: JSON.parse(JSON.stringify(this._store))
})
return request
}
_get (query, keys) {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (query == null) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'DataError' ], {})
}
if (! (query instanceof this._globalObject.IDBKeyRange)) {
query = this._globalObject.IDBKeyRange.only(convert.key(this._globalObject, query))
}
const request = IDBRequest.createImpl(this._globalObject, {}, { transaction: this._transaction, source: this })
this._transaction._queue.push({
method: 'get',
type: 'store',
request: request,
store: JSON.parse(JSON.stringify(this._store)),
query: query,
keys: keys
})
return request
}
get (query) {
return this._get(query, false)
}
getKey (query) {
return this._get(query, true)
}
_getAll(query, count, keys) {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (query == null) {
query = IDBKeyRange.createImpl(this._globalObject, [ null, null ], {})
} else if (! (query instanceof this._globalObject.IDBKeyRange)) {
query = this._globalObject.IDBKeyRange.only(convert.key(this._globalObject, query))
}
const request = IDBRequest.createImpl(this._globalObject, {}, { transaction: this._transaction, source: this })
this._transaction._queue.push({
method: 'getAll',
type: 'store',
request: request,
store: JSON.parse(JSON.stringify(this._store)),
query: query,
count: count,
keys: keys
})
return request
}
getAll (query, count = 0) {
return this._getAll(query, count, false)
}
getAllKeys (query, count = null) {
return this._getAll(query, count, true)
}
count (query) {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (query == null) {
query = IDBKeyRange.createImpl(this._globalObject, [ null, null ], {})
} else if (! (query instanceof this._globalObject.IDBKeyRange)) {
query = this._globalObject.IDBKeyRange.only(convert.key(this._globalObject, query))
}
const request = IDBRequest.createImpl(this._globalObject, [], { transaction: this._transaction, source: this })
this._transaction._queue.push({
method: 'count',
type: 'store',
request: request,
store: JSON.parse(JSON.stringify(this._store)),
query: query
})
return request
}
_openCursor (Cursor, query, direction) {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (query == null) {
query = IDBKeyRange.createImpl(this._globalObject, [ null, null ], {})
} else if (! (query instanceof this._globalObject.IDBKeyRange)) {
query = this._globalObject.IDBKeyRange.only(convert.key(this._globalObject, query))
}
const request = IDBRequest.createImpl(this._globalObject, [], { transaction: this._transaction, source: this })
const cursor = Cursor.createImpl(this._globalObject, [], {
type: 'store',
transaction: this._transaction,
store: this._store,
request: request,
query: query,
direction: direction,
source: this
})
request._result = webidl.wrapperForImpl(cursor)
this._transaction._queue.push({
method: 'openCursor',
type: 'store',
request: request,
store: JSON.parse(JSON.stringify(this._store)),
cursor: cursor,
direction: direction,
query: query
})
return request
}
openCursor (query, direction = 'next') {
return this._openCursor(IDBCursorWithValue, query, direction)
}
openKeyCursor (query, direction = 'next') {
return this._openCursor(IDBCursor, query, direction)
}
index (name) {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state == 'finished') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
const index = this._schema.getIndex(this._store.name, name)
if (index == null) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'NotFoundError' ], {})
}
return IDBIndex.create(this._globalObject, [], {
transaction: this._transaction,
schema: this._schema,
store: JSON.parse(JSON.stringify(this._store)),
index: index,
objectStore: this
})
}
createIndex (name, keyPath, { unique = false, multiEntry = false } = {}) {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction.mode != 'versionchange') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (this._store.index[name] != null) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'ConstraintError' ], {})
}
extractor.verify(this._globalObject, keyPath)
// **TODO** If keyPath is not a valid key path, throw a "SyntaxError" DOMException.
if (Array.isArray(keyPath) && multiEntry) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidAccessError' ], {})
}
const index = this._schema.createIndex(this._store.name, name, keyPath, multiEntry, unique)
this._transaction._queue.push({
method: 'create',
type: 'index',
store: JSON.parse(JSON.stringify(this._store)),
index: index
})
return IDBIndex.create(this._globalObject, [], { objectStore: this, index: index })
}
deleteIndex (name) {
if (this._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction.mode != 'versionchange') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
const index = this._schema.getIndex(this._store.name, name)
if (index == null) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'NotFoundError' ], {})
}
this._schema.deleteIndex(this._store.name, index.name)
this._transaction._queue.push({
method: 'destroy',
store: JSON.parse(JSON.stringify(this._store)),
index: index
})
}
}
module.exports = { implementation: IDBObjectStoreImpl }