1
1
'use strict'
2
2
3
3
const duplexify = require ( 'duplexify' )
4
- const { AbstractLevelDOWN } = require ( 'abstract-leveldown' )
4
+ const { AbstractLevelDOWN, AbstractIterator } = require ( 'abstract-leveldown' )
5
5
const eos = require ( 'end-of-stream' )
6
6
const ids = require ( 'numeric-id-map' )
7
7
const lpstream = require ( 'length-prefixed-stream' )
@@ -287,7 +287,7 @@ Multilevel.prototype._close = function (cb) {
287
287
this . _streaming . once ( 'close' , ( ) => cb ( ) )
288
288
this . _streaming . destroy ( )
289
289
} else {
290
- process . nextTick ( cb )
290
+ this . _nextTick ( cb )
291
291
}
292
292
}
293
293
@@ -298,9 +298,9 @@ Multilevel.prototype._iterator = function (opts) {
298
298
299
299
function noop ( ) { }
300
300
301
- // TODO: extend AbstractIterator, passing db to ctor
302
- function Iterator ( parent , opts ) {
303
- this . _parent = parent
301
+ function Iterator ( db , opts ) {
302
+ AbstractIterator . call ( this , db )
303
+
304
304
this . _keyAsBuffer = opts . keyAsBuffer
305
305
this . _valueAsBuffer = opts . valueAsBuffer
306
306
this . _options = opts
@@ -315,47 +315,73 @@ function Iterator (parent, opts) {
315
315
callback : null
316
316
}
317
317
318
- req . id = parent . _iterators . add ( req )
318
+ req . id = this . db . _iterators . add ( req )
319
319
320
320
this . _read = 0
321
321
this . _ack = Math . floor ( req . batch / 2 )
322
322
this . _req = req
323
- this . _parent . _write ( req )
323
+ this . db . _write ( req )
324
324
}
325
325
326
- Iterator . prototype . next = function ( cb ) {
326
+ Object . setPrototypeOf ( Iterator . prototype , AbstractIterator . prototype )
327
+
328
+ // TODO: implement _next() instead
329
+ Iterator . prototype . next = function ( callback ) {
330
+ // In callback mode, we return `this`
331
+ let ret = this
332
+
333
+ if ( callback === undefined ) {
334
+ ret = new Promise ( function ( resolve , reject ) {
335
+ callback = function ( err , key , value ) {
336
+ if ( err ) reject ( err )
337
+ else if ( key === undefined && value === undefined ) resolve ( )
338
+ else resolve ( [ key , value ] )
339
+ }
340
+ } )
341
+ }
342
+
327
343
this . _req . callback = null
328
344
329
345
if ( this . _req . pending . length ) {
330
346
this . _read ++
331
347
if ( this . _read >= this . _ack ) {
332
348
this . _read = 0
333
349
this . _req . options = null
334
- this . _parent . _write ( this . _req )
350
+ this . db . _write ( this . _req )
335
351
}
336
352
337
353
const next = this . _req . pending . shift ( )
338
- if ( next . error ) return cb ( decodeError ( next . error ) )
339
354
340
- if ( ! next . key && ! next . value ) return cb ( )
355
+ if ( next . error ) {
356
+ callback ( decodeError ( next . error ) )
357
+ return ret
358
+ }
359
+
360
+ if ( ! next . key && ! next . value ) {
361
+ callback ( )
362
+ return ret
363
+ }
341
364
342
365
this . _options . gt = next . key
343
366
if ( this . _options . limit > 0 ) this . _options . limit --
344
367
345
368
const key = decodeValue ( next . key , this . _keyAsBuffer )
346
369
const val = decodeValue ( next . value , this . _valueAsBuffer )
347
- return cb ( undefined , key , val )
370
+
371
+ callback ( undefined , key , val )
372
+ return ret
348
373
}
349
374
350
- this . _req . callback = cb
375
+ this . _req . callback = callback
376
+ return ret
351
377
}
352
378
353
- Iterator . prototype . end = function ( cb ) {
379
+ Iterator . prototype . _end = function ( cb ) {
354
380
this . _req . batch = 0
355
- this . _parent . _write ( this . _req )
356
- this . _parent . _iterators . remove ( this . _req . id )
357
- this . _parent . _flushMaybe ( )
358
- if ( cb ) process . nextTick ( cb )
381
+ this . db . _write ( this . _req )
382
+ this . db . _iterators . remove ( this . _req . id )
383
+ this . db . _flushMaybe ( )
384
+ this . _nextTick ( cb )
359
385
}
360
386
361
387
function decodeError ( err ) {
0 commit comments