@@ -194,14 +194,33 @@ var Cursor = function(bson, ns, cmd, options, topology, topologyOptions) {
194194inherits ( Cursor , Readable ) ;
195195
196196// 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+ } ;
198203
199204for ( var name in CoreCursor . prototype ) {
200205 Cursor . prototype [ name ] = CoreCursor . prototype [ name ] ;
201206}
202207
203208var define = ( Cursor . define = new Define ( 'Cursor' , Cursor , true ) ) ;
204209
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+
205224/**
206225 * Check if there is any document still available in the cursor
207226 * @method
@@ -929,7 +948,11 @@ var toArray = function(self, callback) {
929948 // Fetch all the documents
930949 var fetchDocs = function ( ) {
931950 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+ }
933956 if ( doc == null ) {
934957 return self . close ( { skipKillCursors : true } , ( ) => handleCallback ( callback , null , items ) ) ;
935958 }
@@ -985,17 +1008,21 @@ Cursor.prototype.count = function(applySkipLimit, opts, callback) {
9851008 if ( typeof opts === 'function' ) ( callback = opts ) , ( opts = { } ) ;
9861009 opts = opts || { } ;
9871010
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 ) {
9941011 if ( typeof applySkipLimit === 'function' ) {
9951012 callback = applySkipLimit ;
9961013 applySkipLimit = true ;
9971014 }
9981015
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 ) {
9991026 if ( applySkipLimit ) {
10001027 if ( typeof self . cursorSkip ( ) === 'number' ) opts . skip = self . cursorSkip ( ) ;
10011028 if ( typeof self . cursorLimit ( ) === 'number' ) opts . limit = self . cursorLimit ( ) ;
@@ -1080,7 +1107,7 @@ Cursor.prototype.close = function(options, callback) {
10801107 } ;
10811108
10821109 if ( this . s . session ) {
1083- return this . s . session . endSession ( ( ) => completeClose ( ) ) ;
1110+ return this . _endSession ( ( ) => completeClose ( ) ) ;
10841111 }
10851112
10861113 return completeClose ( ) ;
0 commit comments