@@ -194,14 +194,33 @@ var Cursor = function(bson, ns, cmd, options, topology, topologyOptions) {
194
194
inherits ( Cursor , Readable ) ;
195
195
196
196
// Map core cursor _next method so we can apply mapping
197
- CoreCursor . prototype . _next = CoreCursor . prototype . next ;
197
+ Cursor . prototype . _next = function ( ) {
198
+ if ( this . _initImplicitSession ) {
199
+ this . _initImplicitSession ( ) ;
200
+ }
201
+ return CoreCursor . prototype . next . apply ( this , arguments ) ;
202
+ } ;
198
203
199
204
for ( var name in CoreCursor . prototype ) {
200
205
Cursor . prototype [ name ] = CoreCursor . prototype [ name ] ;
201
206
}
202
207
203
208
var define = ( Cursor . define = new Define ( 'Cursor' , Cursor , true ) ) ;
204
209
210
+ Cursor . prototype . _initImplicitSession = function ( ) {
211
+ if ( ! this . s . session && this . s . topology . hasSessionSupport ( ) ) {
212
+ this . s . session = this . s . topology . startSession ( { owner : this } ) ;
213
+ this . cursorState . session = this . s . session ;
214
+ }
215
+ } ;
216
+
217
+ Cursor . prototype . _endSession = function ( ) {
218
+ const didCloseCursor = CoreCursor . prototype . _endSession . apply ( this , arguments ) ;
219
+ if ( didCloseCursor ) {
220
+ this . s . session = undefined ;
221
+ }
222
+ } ;
223
+
205
224
/**
206
225
* Check if there is any document still available in the cursor
207
226
* @method
@@ -929,7 +948,11 @@ var toArray = function(self, callback) {
929
948
// Fetch all the documents
930
949
var fetchDocs = function ( ) {
931
950
self . _next ( function ( err , doc ) {
932
- if ( err ) return handleCallback ( callback , err ) ;
951
+ if ( err ) {
952
+ return self . _endSession
953
+ ? self . _endSession ( ( ) => handleCallback ( callback , err ) )
954
+ : handleCallback ( callback , err ) ;
955
+ }
933
956
if ( doc == null ) {
934
957
return self . close ( { skipKillCursors : true } , ( ) => handleCallback ( callback , null , items ) ) ;
935
958
}
@@ -985,17 +1008,21 @@ Cursor.prototype.count = function(applySkipLimit, opts, callback) {
985
1008
if ( typeof opts === 'function' ) ( callback = opts ) , ( opts = { } ) ;
986
1009
opts = opts || { } ;
987
1010
988
- return executeOperation ( this . s . topology , count , [ this , applySkipLimit , opts , callback ] , {
989
- skipSessions : true
990
- } ) ;
991
- } ;
992
-
993
- var count = function ( self , applySkipLimit , opts , callback ) {
994
1011
if ( typeof applySkipLimit === 'function' ) {
995
1012
callback = applySkipLimit ;
996
1013
applySkipLimit = true ;
997
1014
}
998
1015
1016
+ if ( this . s . session ) {
1017
+ opts = Object . assign ( { } , opts , { session : this . s . session } ) ;
1018
+ }
1019
+
1020
+ return executeOperation ( this . s . topology , count , [ this , applySkipLimit , opts , callback ] , {
1021
+ skipSessions : ! ! this . s . session
1022
+ } ) ;
1023
+ } ;
1024
+
1025
+ var count = function ( self , applySkipLimit , opts , callback ) {
999
1026
if ( applySkipLimit ) {
1000
1027
if ( typeof self . cursorSkip ( ) === 'number' ) opts . skip = self . cursorSkip ( ) ;
1001
1028
if ( typeof self . cursorLimit ( ) === 'number' ) opts . limit = self . cursorLimit ( ) ;
@@ -1080,7 +1107,7 @@ Cursor.prototype.close = function(options, callback) {
1080
1107
} ;
1081
1108
1082
1109
if ( this . s . session ) {
1083
- return this . s . session . endSession ( ( ) => completeClose ( ) ) ;
1110
+ return this . _endSession ( ( ) => completeClose ( ) ) ;
1084
1111
}
1085
1112
1086
1113
return completeClose ( ) ;
0 commit comments