1
- var EventEmitter = require ( 'events' ) . EventEmitter
2
- var inherits = require ( 'util' ) . inherits
3
- var extend = require ( 'xtend' )
4
- var DeferredLevelDOWN = require ( 'deferred-leveldown' )
5
- var IteratorStream = require ( 'level-iterator-stream' )
6
- var Batch = require ( './batch' )
7
- var errors = require ( 'level-errors' )
8
- var supports = require ( 'level-supports' )
9
- var assert = require ( 'assert' )
10
- var catering = require ( 'catering' )
11
- var getCallback = require ( './common' ) . getCallback
12
- var getOptions = require ( './common' ) . getOptions
13
-
14
- var WriteError = errors . WriteError
15
- var ReadError = errors . ReadError
16
- var NotFoundError = errors . NotFoundError
17
- var OpenError = errors . OpenError
18
- var InitializationError = errors . InitializationError
1
+ 'use strict'
2
+
3
+ const EventEmitter = require ( 'events' ) . EventEmitter
4
+ const inherits = require ( 'util' ) . inherits
5
+ const extend = require ( 'xtend' )
6
+ const DeferredLevelDOWN = require ( 'deferred-leveldown' )
7
+ const IteratorStream = require ( 'level-iterator-stream' )
8
+ const Batch = require ( './batch' )
9
+ const errors = require ( 'level-errors' )
10
+ const supports = require ( 'level-supports' )
11
+ const assert = require ( 'assert' )
12
+ const catering = require ( 'catering' )
13
+ const getCallback = require ( './common' ) . getCallback
14
+ const getOptions = require ( './common' ) . getOptions
15
+
16
+ const WriteError = errors . WriteError
17
+ const ReadError = errors . ReadError
18
+ const NotFoundError = errors . NotFoundError
19
+ const OpenError = errors . OpenError
20
+ const InitializationError = errors . InitializationError
19
21
20
22
// Possible AbstractLevelDOWN#status values:
21
23
// - 'new' - newly created, not opened or closed
@@ -30,8 +32,7 @@ function LevelUP (db, options, callback) {
30
32
return new LevelUP ( db , options , callback )
31
33
}
32
34
33
- var error
34
- var self = this
35
+ let error
35
36
36
37
EventEmitter . call ( this )
37
38
this . setMaxListeners ( Infinity )
@@ -56,9 +57,9 @@ function LevelUP (db, options, callback) {
56
57
this . options = getOptions ( options )
57
58
this . _db = db
58
59
this . db = new DeferredLevelDOWN ( db )
59
- this . open ( callback || function ( err ) {
60
- if ( err ) self . emit ( 'error' , err )
61
- } )
60
+ this . open ( callback || ( ( err ) => {
61
+ if ( err ) this . emit ( 'error' , err )
62
+ } ) )
62
63
63
64
// Create manifest based on deferred-leveldown's
64
65
this . supports = supports ( this . db . supports , {
@@ -70,23 +71,21 @@ function LevelUP (db, options, callback) {
70
71
} )
71
72
72
73
// Experimental: enrich levelup interface
73
- Object . keys ( this . supports . additionalMethods ) . forEach ( function ( method ) {
74
- if ( this [ method ] != null ) return
74
+ for ( const method of Object . keys ( this . supports . additionalMethods ) ) {
75
+ if ( this [ method ] != null ) continue
75
76
76
77
// Don't do this.db[method].bind() because this.db is dynamic.
77
- this [ method ] = function ( ) {
78
- return this . db [ method ] . apply ( this . db , arguments )
78
+ this [ method ] = function ( ... args ) {
79
+ return this . db [ method ] ( ... args )
79
80
}
80
- } , this )
81
+ }
81
82
}
82
83
83
84
LevelUP . prototype . emit = EventEmitter . prototype . emit
84
85
LevelUP . prototype . once = EventEmitter . prototype . once
85
86
inherits ( LevelUP , EventEmitter )
86
87
87
88
LevelUP . prototype . open = function ( opts , callback ) {
88
- var self = this
89
-
90
89
if ( typeof opts === 'function' ) {
91
90
callback = opts
92
91
opts = null
@@ -99,39 +98,37 @@ LevelUP.prototype.open = function (opts, callback) {
99
98
}
100
99
101
100
if ( this . isOpen ( ) ) {
102
- process . nextTick ( callback , null , self )
101
+ process . nextTick ( callback , null , this )
103
102
return callback . promise
104
103
}
105
104
106
105
if ( this . _isOpening ( ) ) {
107
- this . once ( 'open' , function ( ) { callback ( null , self ) } )
106
+ this . once ( 'open' , ( ) => { callback ( null , this ) } )
108
107
return callback . promise
109
108
}
110
109
111
110
this . emit ( 'opening' )
112
111
113
- this . db . open ( opts , function ( err ) {
112
+ this . db . open ( opts , ( err ) => {
114
113
if ( err ) {
115
114
return callback ( new OpenError ( err ) )
116
115
}
117
- self . db = self . _db
118
- callback ( null , self )
119
- self . emit ( 'open' )
120
- self . emit ( 'ready' )
116
+ this . db = this . _db
117
+ callback ( null , this )
118
+ this . emit ( 'open' )
119
+ this . emit ( 'ready' )
121
120
} )
122
121
123
122
return callback . promise
124
123
}
125
124
126
125
LevelUP . prototype . close = function ( callback ) {
127
- var self = this
128
-
129
126
callback = catering . fromCallback ( callback )
130
127
131
128
if ( this . isOpen ( ) ) {
132
- this . db . close ( function ( ) {
133
- self . emit ( 'closed' )
134
- callback . apply ( null , arguments )
129
+ this . db . close ( ( err , ... rest ) => {
130
+ this . emit ( 'closed' )
131
+ callback ( err , ... rest )
135
132
} )
136
133
this . emit ( 'closing' )
137
134
this . db = new DeferredLevelDOWN ( this . _db )
@@ -140,8 +137,8 @@ LevelUP.prototype.close = function (callback) {
140
137
} else if ( this . db . status === 'closing' ) {
141
138
this . once ( 'closed' , callback )
142
139
} else if ( this . _isOpening ( ) ) {
143
- this . once ( 'open' , function ( ) {
144
- self . close ( callback )
140
+ this . once ( 'open' , ( ) => {
141
+ this . close ( callback )
145
142
} )
146
143
}
147
144
@@ -186,8 +183,6 @@ LevelUP.prototype.get = function (key, options, callback) {
186
183
}
187
184
188
185
LevelUP . prototype . put = function ( key , value , options , callback ) {
189
- var self = this
190
-
191
186
callback = getCallback ( options , callback )
192
187
callback = catering . fromCallback ( callback )
193
188
@@ -197,20 +192,18 @@ LevelUP.prototype.put = function (key, value, options, callback) {
197
192
198
193
options = getOptions ( options )
199
194
200
- this . db . put ( key , value , options , function ( err ) {
195
+ this . db . put ( key , value , options , ( err ) => {
201
196
if ( err ) {
202
197
return callback ( new WriteError ( err ) )
203
198
}
204
- self . emit ( 'put' , key , value )
199
+ this . emit ( 'put' , key , value )
205
200
callback ( )
206
201
} )
207
202
208
203
return callback . promise
209
204
}
210
205
211
206
LevelUP . prototype . del = function ( key , options , callback ) {
212
- var self = this
213
-
214
207
callback = getCallback ( options , callback )
215
208
callback = catering . fromCallback ( callback )
216
209
@@ -220,11 +213,11 @@ LevelUP.prototype.del = function (key, options, callback) {
220
213
221
214
options = getOptions ( options )
222
215
223
- this . db . del ( key , options , function ( err ) {
216
+ this . db . del ( key , options , ( err ) => {
224
217
if ( err ) {
225
218
return callback ( new WriteError ( err ) )
226
219
}
227
- self . emit ( 'del' , key )
220
+ this . emit ( 'del' , key )
228
221
callback ( )
229
222
} )
230
223
@@ -236,8 +229,6 @@ LevelUP.prototype.batch = function (arr, options, callback) {
236
229
return new Batch ( this )
237
230
}
238
231
239
- var self = this
240
-
241
232
if ( typeof arr === 'function' ) callback = arr
242
233
else callback = getCallback ( options , callback )
243
234
@@ -249,11 +240,11 @@ LevelUP.prototype.batch = function (arr, options, callback) {
249
240
250
241
options = getOptions ( options )
251
242
252
- this . db . batch ( arr , options , function ( err ) {
243
+ this . db . batch ( arr , options , ( err ) => {
253
244
if ( err ) {
254
245
return callback ( new WriteError ( err ) )
255
246
}
256
- self . emit ( 'batch' , arr )
247
+ this . emit ( 'batch' , arr )
257
248
callback ( )
258
249
} )
259
250
@@ -265,8 +256,6 @@ LevelUP.prototype.iterator = function (options) {
265
256
}
266
257
267
258
LevelUP . prototype . clear = function ( options , callback ) {
268
- var self = this
269
-
270
259
callback = getCallback ( options , callback )
271
260
options = getOptions ( options )
272
261
callback = catering . fromCallback ( callback )
@@ -275,11 +264,11 @@ LevelUP.prototype.clear = function (options, callback) {
275
264
return callback . promise
276
265
}
277
266
278
- this . db . clear ( options , function ( err ) {
267
+ this . db . clear ( options , ( err ) => {
279
268
if ( err ) {
280
269
return callback ( new WriteError ( err ) )
281
270
}
282
- self . emit ( 'clear' , options )
271
+ this . emit ( 'clear' , options )
283
272
callback ( )
284
273
} )
285
274
0 commit comments