4
4
5
5
module . exports = Level
6
6
7
- var AbstractLevelDOWN = require ( 'abstract-leveldown' ) . AbstractLevelDOWN
8
- var inherits = require ( 'inherits' )
9
- var Iterator = require ( './iterator' )
10
- var serialize = require ( './util/serialize' )
11
- var deserialize = require ( './util/deserialize' )
12
- var support = require ( './util/support' )
13
- var clear = require ( './util/clear' )
14
- var createKeyRange = require ( './util/key-range' )
7
+ const AbstractLevelDOWN = require ( 'abstract-leveldown' ) . AbstractLevelDOWN
8
+ const inherits = require ( 'inherits' )
9
+ const Iterator = require ( './iterator' )
10
+ const serialize = require ( './util/serialize' )
11
+ const deserialize = require ( './util/deserialize' )
12
+ const support = require ( './util/support' )
13
+ const clear = require ( './util/clear' )
14
+ const createKeyRange = require ( './util/key-range' )
15
15
16
- var DEFAULT_PREFIX = 'level-js-'
16
+ const DEFAULT_PREFIX = 'level-js-'
17
17
18
18
function Level ( location , opts ) {
19
19
if ( ! ( this instanceof Level ) ) return new Level ( location , opts )
@@ -41,34 +41,33 @@ inherits(Level, AbstractLevelDOWN)
41
41
Level . prototype . type = 'level-js'
42
42
43
43
Level . prototype . _open = function ( options , callback ) {
44
- var req = indexedDB . open ( this . prefix + this . location , this . version )
45
- var self = this
44
+ const req = indexedDB . open ( this . prefix + this . location , this . version )
46
45
47
46
req . onerror = function ( ) {
48
47
callback ( req . error || new Error ( 'unknown error' ) )
49
48
}
50
49
51
- req . onsuccess = function ( ) {
52
- self . db = req . result
50
+ req . onsuccess = ( ) => {
51
+ this . db = req . result
53
52
callback ( )
54
53
}
55
54
56
- req . onupgradeneeded = function ( ev ) {
57
- var db = ev . target . result
55
+ req . onupgradeneeded = ( ev ) => {
56
+ const db = ev . target . result
58
57
59
- if ( ! db . objectStoreNames . contains ( self . location ) ) {
60
- db . createObjectStore ( self . location )
58
+ if ( ! db . objectStoreNames . contains ( this . location ) ) {
59
+ db . createObjectStore ( this . location )
61
60
}
62
61
}
63
62
}
64
63
65
64
Level . prototype . store = function ( mode ) {
66
- var transaction = this . db . transaction ( [ this . location ] , mode )
65
+ const transaction = this . db . transaction ( [ this . location ] , mode )
67
66
return transaction . objectStore ( this . location )
68
67
}
69
68
70
69
Level . prototype . await = function ( request , callback ) {
71
- var transaction = request . transaction
70
+ const transaction = request . transaction
72
71
73
72
// Take advantage of the fact that a non-canceled request error aborts
74
73
// the transaction. I.e. no need to listen for "request.onerror".
@@ -82,10 +81,11 @@ Level.prototype.await = function (request, callback) {
82
81
}
83
82
84
83
Level . prototype . _get = function ( key , options , callback ) {
85
- var store = this . store ( 'readonly' )
84
+ const store = this . store ( 'readonly' )
85
+ let req
86
86
87
87
try {
88
- var req = store . get ( key )
88
+ req = store . get ( key )
89
89
} catch ( err ) {
90
90
return this . _nextTick ( callback , err )
91
91
}
@@ -103,10 +103,11 @@ Level.prototype._get = function (key, options, callback) {
103
103
}
104
104
105
105
Level . prototype . _del = function ( key , options , callback ) {
106
- var store = this . store ( 'readwrite' )
106
+ const store = this . store ( 'readwrite' )
107
+ let req
107
108
108
109
try {
109
- var req = store . delete ( key )
110
+ req = store . delete ( key )
110
111
} catch ( err ) {
111
112
return this . _nextTick ( callback , err )
112
113
}
@@ -115,12 +116,13 @@ Level.prototype._del = function (key, options, callback) {
115
116
}
116
117
117
118
Level . prototype . _put = function ( key , value , options , callback ) {
118
- var store = this . store ( 'readwrite' )
119
+ const store = this . store ( 'readwrite' )
120
+ let req
119
121
120
122
try {
121
123
// Will throw a DataError or DataCloneError if the environment
122
124
// does not support serializing the key or value respectively.
123
- var req = store . put ( value , key )
125
+ req = store . put ( value , key )
124
126
} catch ( err ) {
125
127
return this . _nextTick ( callback , err )
126
128
}
@@ -143,10 +145,10 @@ Level.prototype._iterator = function (options) {
143
145
Level . prototype . _batch = function ( operations , options , callback ) {
144
146
if ( operations . length === 0 ) return this . _nextTick ( callback )
145
147
146
- var store = this . store ( 'readwrite' )
147
- var transaction = store . transaction
148
- var index = 0
149
- var error
148
+ const store = this . store ( 'readwrite' )
149
+ const transaction = store . transaction
150
+ let index = 0
151
+ let error
150
152
151
153
transaction . onabort = function ( ) {
152
154
callback ( error || transaction . error || new Error ( 'aborted by user' ) )
@@ -158,11 +160,13 @@ Level.prototype._batch = function (operations, options, callback) {
158
160
159
161
// Wait for a request to complete before making the next, saving CPU.
160
162
function loop ( ) {
161
- var op = operations [ index ++ ]
162
- var key = op . key
163
+ const op = operations [ index ++ ]
164
+ const key = op . key
165
+
166
+ let req
163
167
164
168
try {
165
- var req = op . type === 'del' ? store . delete ( key ) : store . put ( op . value , key )
169
+ req = op . type === 'del' ? store . delete ( key ) : store . put ( op . value , key )
166
170
} catch ( err ) {
167
171
error = err
168
172
transaction . abort ( )
@@ -178,8 +182,11 @@ Level.prototype._batch = function (operations, options, callback) {
178
182
}
179
183
180
184
Level . prototype . _clear = function ( options , callback ) {
185
+ let keyRange
186
+ let req
187
+
181
188
try {
182
- var keyRange = createKeyRange ( options )
189
+ keyRange = createKeyRange ( options )
183
190
} catch ( e ) {
184
191
// The lower key is greater than the upper key.
185
192
// IndexedDB throws an error, but we'll just do nothing.
@@ -193,8 +200,8 @@ Level.prototype._clear = function (options, callback) {
193
200
}
194
201
195
202
try {
196
- var store = this . store ( 'readwrite' )
197
- var req = keyRange ? store . delete ( keyRange ) : store . clear ( )
203
+ const store = this . store ( 'readwrite' )
204
+ req = keyRange ? store . delete ( keyRange ) : store . clear ( )
198
205
} catch ( err ) {
199
206
return this . _nextTick ( callback , err )
200
207
}
@@ -213,9 +220,9 @@ Level.prototype.upgrade = function (callback) {
213
220
return this . _nextTick ( callback , new Error ( 'cannot upgrade() before open()' ) )
214
221
}
215
222
216
- var it = this . iterator ( )
217
- var batchOptions = { }
218
- var self = this
223
+ const it = this . iterator ( )
224
+ const batchOptions = { }
225
+ const self = this
219
226
220
227
it . _deserializeKey = it . _deserializeValue = identity
221
228
next ( )
@@ -230,8 +237,8 @@ Level.prototype.upgrade = function (callback) {
230
237
return finish ( err )
231
238
}
232
239
233
- var newKey = self . _serializeKey ( deserialize ( key , true ) )
234
- var newValue = self . _serializeValue ( deserialize ( value , true ) )
240
+ const newKey = self . _serializeKey ( deserialize ( key , true ) )
241
+ const newValue = self . _serializeValue ( deserialize ( value , true ) )
235
242
236
243
// To bypass serialization on the old key, use _batch() instead of batch().
237
244
// NOTE: if we disable snapshotting (#86) this could lead to a loop of
@@ -259,7 +266,7 @@ Level.destroy = function (location, prefix, callback) {
259
266
callback = prefix
260
267
prefix = DEFAULT_PREFIX
261
268
}
262
- var request = indexedDB . deleteDatabase ( prefix + location )
269
+ const request = indexedDB . deleteDatabase ( prefix + location )
263
270
request . onsuccess = function ( ) {
264
271
callback ( )
265
272
}
0 commit comments