@@ -62,19 +62,19 @@ class MemoryIterator extends AbstractIterator {
62
62
this [ kInit ] ( db [ kTree ] , options )
63
63
}
64
64
65
- _next ( callback ) {
66
- if ( ! this [ kIterator ] . valid ) return this . nextTick ( callback )
65
+ async _next ( ) {
66
+ if ( ! this [ kIterator ] . valid ) return undefined
67
67
68
68
const key = this [ kIterator ] . key
69
69
const value = this [ kIterator ] . value
70
70
71
- if ( ! this [ kTest ] ( key ) ) return this . nextTick ( callback )
71
+ if ( ! this [ kTest ] ( key ) ) return undefined
72
72
73
73
this [ kIterator ] [ this [ kAdvance ] ] ( )
74
- this . nextTick ( callback , null , key , value )
74
+ return [ key , value ]
75
75
}
76
76
77
- _nextv ( size , options , callback ) {
77
+ async _nextv ( size , options ) {
78
78
const it = this [ kIterator ]
79
79
const entries = [ ]
80
80
@@ -83,10 +83,10 @@ class MemoryIterator extends AbstractIterator {
83
83
it [ this [ kAdvance ] ] ( )
84
84
}
85
85
86
- this . nextTick ( callback , null , entries )
86
+ return entries
87
87
}
88
88
89
- _all ( options , callback ) {
89
+ async _all ( options ) {
90
90
const size = this . limit - this . count
91
91
const it = this [ kIterator ]
92
92
const entries = [ ]
@@ -96,7 +96,7 @@ class MemoryIterator extends AbstractIterator {
96
96
it [ this [ kAdvance ] ] ( )
97
97
}
98
98
99
- this . nextTick ( callback , null , entries )
99
+ return entries
100
100
}
101
101
}
102
102
@@ -106,17 +106,17 @@ class MemoryKeyIterator extends AbstractKeyIterator {
106
106
this [ kInit ] ( db [ kTree ] , options )
107
107
}
108
108
109
- _next ( callback ) {
110
- if ( ! this [ kIterator ] . valid ) return this . nextTick ( callback )
109
+ async _next ( ) {
110
+ if ( ! this [ kIterator ] . valid ) return undefined
111
111
112
112
const key = this [ kIterator ] . key
113
- if ( ! this [ kTest ] ( key ) ) return this . nextTick ( callback )
113
+ if ( ! this [ kTest ] ( key ) ) return undefined
114
114
115
115
this [ kIterator ] [ this [ kAdvance ] ] ( )
116
- this . nextTick ( callback , null , key )
116
+ return key
117
117
}
118
118
119
- _nextv ( size , options , callback ) {
119
+ async _nextv ( size , options ) {
120
120
const it = this [ kIterator ]
121
121
const keys = [ ]
122
122
@@ -125,10 +125,10 @@ class MemoryKeyIterator extends AbstractKeyIterator {
125
125
it [ this [ kAdvance ] ] ( )
126
126
}
127
127
128
- this . nextTick ( callback , null , keys )
128
+ return keys
129
129
}
130
130
131
- _all ( options , callback ) {
131
+ async _all ( options ) {
132
132
const size = this . limit - this . count
133
133
const it = this [ kIterator ]
134
134
const keys = [ ]
@@ -138,7 +138,7 @@ class MemoryKeyIterator extends AbstractKeyIterator {
138
138
it [ this [ kAdvance ] ] ( )
139
139
}
140
140
141
- this . nextTick ( callback , null , keys )
141
+ return keys
142
142
}
143
143
}
144
144
@@ -148,19 +148,19 @@ class MemoryValueIterator extends AbstractValueIterator {
148
148
this [ kInit ] ( db [ kTree ] , options )
149
149
}
150
150
151
- _next ( callback ) {
152
- if ( ! this [ kIterator ] . valid ) return this . nextTick ( callback )
151
+ async _next ( options ) {
152
+ if ( ! this [ kIterator ] . valid ) return undefined
153
153
154
154
const key = this [ kIterator ] . key
155
155
const value = this [ kIterator ] . value
156
156
157
- if ( ! this [ kTest ] ( key ) ) return this . nextTick ( callback )
157
+ if ( ! this [ kTest ] ( key ) ) return undefined
158
158
159
159
this [ kIterator ] [ this [ kAdvance ] ] ( )
160
- this . nextTick ( callback , null , value )
160
+ return value
161
161
}
162
162
163
- _nextv ( size , options , callback ) {
163
+ async _nextv ( size , options ) {
164
164
const it = this [ kIterator ]
165
165
const values = [ ]
166
166
@@ -169,10 +169,10 @@ class MemoryValueIterator extends AbstractValueIterator {
169
169
it [ this [ kAdvance ] ] ( )
170
170
}
171
171
172
- this . nextTick ( callback , null , values )
172
+ return values
173
173
}
174
174
175
- _all ( options , callback ) {
175
+ async _all ( options ) {
176
176
const size = this . limit - this . count
177
177
const it = this [ kIterator ]
178
178
const values = [ ]
@@ -182,7 +182,7 @@ class MemoryValueIterator extends AbstractValueIterator {
182
182
it [ this [ kAdvance ] ] ( )
183
183
}
184
184
185
- this . nextTick ( callback , null , values )
185
+ return values
186
186
}
187
187
}
188
188
@@ -270,6 +270,7 @@ class MemoryLevel extends AbstractLevel {
270
270
}
271
271
272
272
// To help migrating from level-mem to abstract-level
273
+ // TODO (v2): remove
273
274
if ( typeof location === 'function' || typeof options === 'function' || typeof _ === 'function' ) {
274
275
throw new ModuleError ( 'The levelup-style callback argument has been removed' , {
275
276
code : 'LEVEL_LEGACY'
@@ -291,45 +292,40 @@ class MemoryLevel extends AbstractLevel {
291
292
permanence : false ,
292
293
createIfMissing : false ,
293
294
errorIfExists : false ,
294
- encodings : { [ storeEncoding ] : true }
295
+ encodings : { [ storeEncoding ] : true } ,
296
+ signals : {
297
+ // Would have no value here because the operations are synchronous
298
+ iterators : false
299
+ }
295
300
} , forward )
296
301
297
302
this [ kTree ] = createRBT ( compare )
298
303
}
299
304
300
- _put ( key , value , options , callback ) {
305
+ async _put ( key , value , options ) {
301
306
const it = this [ kTree ] . find ( key )
302
307
303
308
if ( it . valid ) {
304
309
this [ kTree ] = it . update ( value )
305
310
} else {
306
311
this [ kTree ] = this [ kTree ] . insert ( key , value )
307
312
}
308
-
309
- this . nextTick ( callback )
310
313
}
311
314
312
- _get ( key , options , callback ) {
313
- const value = this [ kTree ] . get ( key )
314
-
315
- if ( typeof value === 'undefined' ) {
316
- // TODO: use error code (not urgent, abstract-level normalizes this)
317
- return this . nextTick ( callback , new Error ( 'NotFound' ) )
318
- }
319
-
320
- this . nextTick ( callback , null , value )
315
+ async _get ( key , options ) {
316
+ // Is undefined if not found
317
+ return this [ kTree ] . get ( key )
321
318
}
322
319
323
- _getMany ( keys , options , callback ) {
324
- this . nextTick ( callback , null , keys . map ( key => this [ kTree ] . get ( key ) ) )
320
+ async _getMany ( keys , options ) {
321
+ return keys . map ( key => this [ kTree ] . get ( key ) )
325
322
}
326
323
327
- _del ( key , options , callback ) {
324
+ async _del ( key , options ) {
328
325
this [ kTree ] = this [ kTree ] . remove ( key )
329
- this . nextTick ( callback )
330
326
}
331
327
332
- _batch ( operations , options , callback ) {
328
+ async _batch ( operations , options ) {
333
329
let tree = this [ kTree ]
334
330
335
331
for ( const op of operations ) {
@@ -344,38 +340,35 @@ class MemoryLevel extends AbstractLevel {
344
340
}
345
341
346
342
this [ kTree ] = tree
347
- this . nextTick ( callback )
348
343
}
349
344
350
- _clear ( options , callback ) {
345
+ async _clear ( options ) {
351
346
if ( options . limit === - 1 && ! Object . keys ( options ) . some ( isRangeOption ) ) {
352
347
// Delete everything by creating a new empty tree.
353
348
this [ kTree ] = createRBT ( compare )
354
- return this . nextTick ( callback )
349
+ return
355
350
}
356
351
357
352
const iterator = this . _keys ( { ...options } )
358
353
const limit = iterator . limit
359
354
360
355
let count = 0
361
356
362
- const loop = ( ) => {
357
+ while ( true ) {
363
358
// TODO: add option to control "batch size"
364
359
for ( let i = 0 ; i < 500 ; i ++ ) {
365
- if ( ++ count > limit ) return callback ( )
366
- if ( ! iterator [ kIterator ] . valid ) return callback ( )
367
- if ( ! iterator [ kTest ] ( iterator [ kIterator ] . key ) ) return callback ( )
360
+ if ( ++ count > limit ) return
361
+ if ( ! iterator [ kIterator ] . valid ) return
362
+ if ( ! iterator [ kTest ] ( iterator [ kIterator ] . key ) ) return
368
363
369
364
// Must also include changes made in parallel to clear()
370
365
this [ kTree ] = this [ kTree ] . remove ( iterator [ kIterator ] . key )
371
366
iterator [ kIterator ] [ iterator [ kAdvance ] ] ( )
372
367
}
373
368
374
369
// Some time to breathe
375
- this . nextTick ( loop )
370
+ await breathe ( )
376
371
}
377
-
378
- this . nextTick ( loop )
379
372
}
380
373
381
374
_iterator ( options ) {
@@ -393,18 +386,17 @@ class MemoryLevel extends AbstractLevel {
393
386
394
387
exports . MemoryLevel = MemoryLevel
395
388
396
- // Use setImmediate() in Node.js to allow IO in between our callbacks
389
+ let breathe
390
+
391
+ // Use setImmediate() in Node.js to allow IO in between work
397
392
if ( typeof process !== 'undefined' && ! process . browser && typeof global !== 'undefined' && typeof global . setImmediate === 'function' ) {
398
393
const setImmediate = global . setImmediate
399
394
400
- // Automatically applies to iterators, sublevels and chained batches as well
401
- MemoryLevel . prototype . nextTick = function ( fn , ...args ) {
402
- if ( args . length === 0 ) {
403
- setImmediate ( fn )
404
- } else {
405
- setImmediate ( ( ) => fn ( ...args ) )
406
- }
395
+ breathe = function ( ) {
396
+ return new Promise ( setImmediate )
407
397
}
398
+ } else {
399
+ breathe = async function ( ) { }
408
400
}
409
401
410
402
function isRangeOption ( k ) {
0 commit comments