@@ -71,6 +71,41 @@ for (const deferred of [false, true]) {
71
71
if ( deferred ) await db . open ( )
72
72
} )
73
73
74
+ test ( `${ mode } ().next() skips _next() if it previously signaled end (deferred: ${ deferred } , default implementation: ${ def } )` , async function ( t ) {
75
+ class MockLevel extends AbstractLevel {
76
+ [ privateMethod ] ( options ) {
77
+ return new MockIterator ( this , options )
78
+ }
79
+ }
80
+
81
+ let calls = 0
82
+
83
+ class MockIterator extends Ctor {
84
+ async _next ( ) {
85
+ if ( calls ++ ) return undefined
86
+
87
+ if ( mode === 'iterator' || def ) {
88
+ return [ 'a' , 'a' ]
89
+ } else {
90
+ return 'a'
91
+ }
92
+ }
93
+ }
94
+
95
+ const db = new MockLevel ( utf8Manifest )
96
+ if ( ! deferred ) await db . open ( )
97
+ const it = db [ publicMethod ] ( )
98
+
99
+ t . same ( await it . next ( ) , mode === 'iterator' ? [ 'a' , 'a' ] : 'a' )
100
+ t . is ( calls , 1 , 'got one _next() call' )
101
+
102
+ t . is ( await it . next ( ) , undefined )
103
+ t . is ( calls , 2 , 'got another _next() call' )
104
+
105
+ t . is ( await it . next ( ) , undefined )
106
+ t . is ( calls , 2 , 'not called again' )
107
+ } )
108
+
74
109
for ( const limit of [ 2 , 0 ] ) {
75
110
test ( `${ mode } ().next() skips _next() when limit ${ limit } is reached (deferred: ${ deferred } , default implementation: ${ def } )` , async function ( t ) {
76
111
class MockLevel extends AbstractLevel {
@@ -184,6 +219,41 @@ for (const deferred of [false, true]) {
184
219
} )
185
220
}
186
221
222
+ test ( `${ mode } ().nextv() skips _nextv() if it previously signaled end (deferred: ${ deferred } , default implementation: ${ def } )` , async function ( t ) {
223
+ class MockLevel extends AbstractLevel {
224
+ [ privateMethod ] ( options ) {
225
+ return new MockIterator ( this , options )
226
+ }
227
+ }
228
+
229
+ let calls = 0
230
+
231
+ class MockIterator extends Ctor {
232
+ async _nextv ( ) {
233
+ if ( calls ++ ) return [ ]
234
+
235
+ if ( mode === 'iterator' || def ) {
236
+ return [ [ 'a' , 'a' ] ]
237
+ } else {
238
+ return [ 'a' ]
239
+ }
240
+ }
241
+ }
242
+
243
+ const db = new MockLevel ( utf8Manifest )
244
+ if ( ! deferred ) await db . open ( )
245
+ const it = db [ publicMethod ] ( )
246
+
247
+ t . same ( await it . nextv ( 100 ) , [ mode === 'iterator' ? [ 'a' , 'a' ] : 'a' ] )
248
+ t . is ( calls , 1 , 'got one _nextv() call' )
249
+
250
+ t . same ( await it . nextv ( 100 ) , [ ] )
251
+ t . is ( calls , 2 , 'got another _nextv() call' )
252
+
253
+ t . same ( await it . nextv ( 100 ) , [ ] )
254
+ t . is ( calls , 2 , 'not called again' )
255
+ } )
256
+
187
257
test ( `${ mode } ().nextv() reduces size for _nextv() when near limit (deferred: ${ deferred } , default implementation: ${ def } )` , async function ( t ) {
188
258
class MockLevel extends AbstractLevel {
189
259
[ privateMethod ] ( options ) {
@@ -615,6 +685,38 @@ for (const deferred of [false, true]) {
615
685
}
616
686
} )
617
687
688
+ test ( `${ mode } () default nextv() stops when natural end is reached (deferred: ${ deferred } , default implementation: ${ def } )` , async function ( t ) {
689
+ let calls = 0
690
+
691
+ class MockLevel extends AbstractLevel {
692
+ [ privateMethod ] ( options ) {
693
+ return new MockIterator ( this , options )
694
+ }
695
+ }
696
+
697
+ class MockIterator extends Ctor {
698
+ async _next ( ) {
699
+ if ( calls ++ ) return undefined
700
+
701
+ if ( mode === 'iterator' || def ) {
702
+ return [ 'a' , 'a' ]
703
+ } else {
704
+ return 'a'
705
+ }
706
+ }
707
+ }
708
+
709
+ const db = new MockLevel ( utf8Manifest )
710
+ if ( ! deferred ) await db . open ( )
711
+ const it = await db [ publicMethod ] ( )
712
+
713
+ t . same ( await it . nextv ( 10 ) , [ mode === 'iterator' ? [ 'a' , 'a' ] : 'a' ] )
714
+ t . is ( calls , 2 )
715
+
716
+ t . same ( await it . nextv ( 10 ) , [ ] , 'ended' )
717
+ t . is ( calls , 2 , 'not called again' )
718
+ } )
719
+
618
720
test ( `${ mode } () has default all() (deferred: ${ deferred } , default implementation: ${ def } )` , async function ( t ) {
619
721
t . plan ( 8 )
620
722
@@ -685,6 +787,35 @@ for (const deferred of [false, true]) {
685
787
}
686
788
} )
687
789
790
+ test ( `${ mode } () default all() stops when limit is reached (deferred: ${ deferred } , default implementation: ${ def } )` , async function ( t ) {
791
+ t . plan ( 2 )
792
+ let calls = 0
793
+
794
+ class MockLevel extends AbstractLevel {
795
+ [ privateMethod ] ( options ) {
796
+ return new MockIterator ( this , options )
797
+ }
798
+ }
799
+
800
+ class MockIterator extends Ctor {
801
+ async _nextv ( size , options ) {
802
+ calls ++
803
+ if ( mode === 'iterator' || def ) {
804
+ return [ [ String ( calls ) , String ( calls ) ] ]
805
+ } else {
806
+ return [ String ( calls ) ]
807
+ }
808
+ }
809
+ }
810
+
811
+ const db = new MockLevel ( utf8Manifest )
812
+ if ( ! deferred ) await db . open ( )
813
+
814
+ const items = await db [ publicMethod ] ( { limit : 2 } ) . all ( )
815
+ t . is ( items . length , 2 )
816
+ t . is ( calls , 2 )
817
+ } )
818
+
688
819
test ( `${ mode } () custom all() (deferred: ${ deferred } , default implementation: ${ def } )` , async function ( t ) {
689
820
t . plan ( 3 )
690
821
0 commit comments