@@ -148,7 +148,6 @@ export abstract class AbstractCursor<
148
148
[ kDocuments ] : {
149
149
length : number ;
150
150
shift ( bsonOptions ?: any ) : TSchema | null ;
151
- unshift ( doc : TSchema ) : void ;
152
151
clear ( ) : void ;
153
152
pushMany ( many : Iterable < TSchema > ) : void ;
154
153
push ( item : TSchema ) : void ;
@@ -390,14 +389,7 @@ export abstract class AbstractCursor<
390
389
return true ;
391
390
}
392
391
393
- const doc = await next < TSchema > ( this , { blocking : true , transform : false } ) ;
394
-
395
- if ( doc ) {
396
- this [ kDocuments ] . unshift ( doc ) ;
397
- return true ;
398
- }
399
-
400
- return false ;
392
+ return await next ( this , { blocking : true , transform : false , hasNext : true } ) ;
401
393
}
402
394
403
395
/** Get the next available document from the cursor, returns null if no more documents are available. */
@@ -406,7 +398,7 @@ export abstract class AbstractCursor<
406
398
throw new MongoCursorExhaustedError ( ) ;
407
399
}
408
400
409
- return await next ( this , { blocking : true , transform : true } ) ;
401
+ return await next ( this , { blocking : true , transform : true , hasNext : false } ) ;
410
402
}
411
403
412
404
/**
@@ -417,7 +409,7 @@ export abstract class AbstractCursor<
417
409
throw new MongoCursorExhaustedError ( ) ;
418
410
}
419
411
420
- return await next ( this , { blocking : false , transform : true } ) ;
412
+ return await next ( this , { blocking : false , transform : true , hasNext : false } ) ;
421
413
}
422
414
423
415
/**
@@ -726,13 +718,42 @@ async function next<T>(
726
718
cursor : AbstractCursor < T > ,
727
719
{
728
720
blocking,
729
- transform
721
+ transform,
722
+ hasNext
723
+ } : {
724
+ blocking : boolean ;
725
+ transform : boolean ;
726
+ hasNext : true ;
727
+ }
728
+ ) : Promise < boolean > ;
729
+
730
+ async function next < T > (
731
+ cursor : AbstractCursor < T > ,
732
+ {
733
+ blocking,
734
+ transform,
735
+ hasNext
736
+ } : {
737
+ blocking : boolean ;
738
+ transform : boolean ;
739
+ hasNext : false ;
740
+ }
741
+ ) : Promise < T | null > ;
742
+
743
+ async function next < T > (
744
+ cursor : AbstractCursor < T > ,
745
+ {
746
+ blocking,
747
+ transform,
748
+ hasNext
730
749
} : {
731
750
blocking : boolean ;
732
751
transform : boolean ;
752
+ hasNext : boolean ;
733
753
}
734
- ) : Promise < T | null > {
754
+ ) : Promise < boolean | T | null > {
735
755
if ( cursor . closed ) {
756
+ if ( hasNext ) return false ;
736
757
return null ;
737
758
}
738
759
@@ -743,6 +764,7 @@ async function next<T>(
743
764
}
744
765
745
766
if ( cursor [ kDocuments ] . length !== 0 ) {
767
+ if ( hasNext ) return true ;
746
768
const doc = cursor [ kDocuments ] . shift ( cursor [ kOptions ] ) ;
747
769
748
770
if ( doc != null && transform && cursor [ kTransform ] ) {
@@ -767,6 +789,7 @@ async function next<T>(
767
789
// cleanupCursor should never throw, but if it does it indicates a bug in the driver
768
790
// and we should surface the error
769
791
await cleanupCursor ( cursor , { } ) ;
792
+ if ( hasNext ) return false ;
770
793
return null ;
771
794
}
772
795
@@ -811,10 +834,12 @@ async function next<T>(
811
834
}
812
835
813
836
if ( cursor [ kDocuments ] . length === 0 && blocking === false ) {
837
+ if ( hasNext ) return false ;
814
838
return null ;
815
839
}
816
840
} while ( ! cursor . isDead || cursor [ kDocuments ] . length !== 0 ) ;
817
841
842
+ if ( hasNext ) return false ;
818
843
return null ;
819
844
}
820
845
0 commit comments