-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathIDBCursor-impl.js
231 lines (217 loc) · 8.83 KB
/
IDBCursor-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
const IDBRequest = require('./living/generated/IDBRequest')
const DOMException = require('domexception/lib/DOMException')
const Verbatim = require('verbatim')
const { valuify } = require('./value')
const compare = require('./compare')
const convert = require('./convert')
const structuredClone = require('./structuredClone')
class IDBCursorImpl {
constructor (globalObject, [], { type, transaction, store, request, direction, source, query, index }) {
this._globalObject = globalObject
this._type = type
this._store = store
this.request = request
this._query = query
this._index = index
this._transaction = transaction
this._direction = direction
this._source = source
this._gotValue = false
this._keyOnly = true
}
get source () {
return this._source
}
get direction () {
return this._direction
}
get key () {
return this._key
}
get primaryKey () {
return this._value.key
}
advance (count) {
if (count == 0) {
throw new TypeError('count must not be zero')
}
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (this.source._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (! this._gotValue) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
this._gotValue = false
// TODO If the transaction is not active, throw a
// 'TransactionInactiveError' `DOMExecption`.
this._transaction._queue.push({
method: 'advance',
count: count,
cursor: this,
request: this.request,
// TODO Probably do not need this defensive copy.
store: JSON.parse(JSON.stringify(this._store))
})
}
continue (key) {
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (this.source._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (! this._gotValue) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
key = key == null ? null : convert.key(this._globalObject, key)
if (key != null) {
switch (this._direction) {
case 'next':
case 'nextunique': {
if (compare(this._globalObject, key, this._key) <= 0) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'DataError' ], {})
}
}
break
case 'prev':
case 'prevunique': {
if (compare(this._globalObject, key, this._key) >= 0) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'DataError' ], {})
}
}
break
}
}
this._gotValue = false
this._transaction._queue.push({
method: 'continue',
type: this._type,
key: key,
primaryKey: null,
cursor: this,
request: this.request,
store: JSON.parse(JSON.stringify(this._store))
})
}
continuePrimaryKey (key, primaryKey) {
if (this._transaction._state != 'active') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'TransactionInactiveError' ], {})
}
if (this.source._isDeleted()) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
if (this._type != 'index') {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidAccessError' ], {})
}
if (this._direction.endsWith('unique')) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidAccessError' ], {})
}
if (! this._gotValue) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
key = key == null ? null : convert.key(this._globalObject, key)
primaryKey = primaryKey == null ? null : convert.key(this._globalObject, primaryKey)
switch (this._direction) {
case 'next': {
const test = compare(this._globalObject, key, this._key)
if (test < 0) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'DataError' ], {})
} else if (test == 0) {
if (compare(this._globalObject, primaryKey, this._value.key) <= 0) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'DataError' ], {})
}
}
}
break
case 'prev': {
const test = compare(this._globalObject, key, this._key)
if (test > 0) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'DataError' ], {})
} else if (test == 0) {
if (compare(this._globalObject, primaryKey, this._value.key) >= 0) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'DataError' ], {})
}
}
}
break
default: {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidAccessError' ], {})
}
break
}
this._gotValue = false
this._transaction._queue.push({
method: 'item',
type: this._type,
key: key,
primaryKey: primaryKey,
cursor: this,
request: this.request,
store: JSON.parse(JSON.stringify(this._store))
})
}
update (value) {
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 (this.source._isDeleted() || ! this._gotValue || this._keyOnly) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
const request = IDBRequest.createImpl(this._globalObject, [], {
transaction: this._transaction, source: this
})
// Transaction must not be active during a structured clone.
try {
this._transaction._state = 'inactive'
value = structuredClone(this._globalObject, value)
} finally {
this._transaction._state = 'active'
}
if (this._store.keyPath != null) {
const key = valuify(this._globalObject, (this.source.objectStore || this.source)._schema.getExtractor(this._store.id)(value))
if (compare(this._globalObject, key, this._value.key) != 0) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'DataError' ], {})
}
}
this._transaction._queue.push({
method: 'set',
overwrite: true,
request: request,
type: this._type,
key: this._value.key,
value: value,
store: JSON.parse(JSON.stringify(this._store))
})
return request
}
delete () {
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 (this.source._isDeleted() || ! this._gotValue || this._keyOnly) {
throw DOMException.create(this._globalObject, [ 'TODO: message', 'InvalidStateError' ], {})
}
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: request,
query:
this._globalObject.IDBKeyRange.only(convert.key(this._globalObject, this._value.key))
})
return request
}
}
module.exports = { implementation: IDBCursorImpl }