@@ -52,6 +52,8 @@ export interface ClientSessionOptions {
52
52
/** @public */
53
53
export type WithTransactionCallback = ( session : ClientSession ) => Promise < any > | void ;
54
54
55
+ const kServerSession = Symbol ( 'serverSession' ) ;
56
+
55
57
/**
56
58
* A class representing a client session on the server
57
59
*
@@ -62,7 +64,6 @@ class ClientSession extends EventEmitter {
62
64
topology : Topology ;
63
65
sessionPool : ServerSessionPool ;
64
66
hasEnded : boolean ;
65
- serverSession ?: ServerSession ;
66
67
clientOptions ?: MongoClientOptions ;
67
68
supports : { causalConsistency : boolean } ;
68
69
clusterTime ?: ClusterTime ;
@@ -71,6 +72,7 @@ class ClientSession extends EventEmitter {
71
72
owner : symbol | CoreCursor ;
72
73
defaultTransactionOptions : TransactionOptions ;
73
74
transaction : Transaction ;
75
+ [ kServerSession ] ?: ServerSession ;
74
76
75
77
/**
76
78
* Create a client session.
@@ -102,8 +104,8 @@ class ClientSession extends EventEmitter {
102
104
this . topology = topology ;
103
105
this . sessionPool = sessionPool ;
104
106
this . hasEnded = false ;
105
- this . serverSession = sessionPool . acquire ( ) ;
106
107
this . clientOptions = clientOptions ;
108
+ this [ kServerSession ] = undefined ;
107
109
108
110
this . supports = {
109
111
causalConsistency :
@@ -124,41 +126,61 @@ class ClientSession extends EventEmitter {
124
126
return this . serverSession ?. id ;
125
127
}
126
128
129
+ get serverSession ( ) : ServerSession {
130
+ if ( this [ kServerSession ] == null ) {
131
+ this [ kServerSession ] = this . sessionPool . acquire ( ) ;
132
+ }
133
+
134
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
135
+ return this [ kServerSession ] ! ;
136
+ }
137
+
127
138
/**
128
139
* Ends this session on the server
129
140
*
130
141
* @param options - Optional settings. Currently reserved for future use
131
142
* @param callback - Optional callback for completion of this operation
132
143
*/
133
- endSession ( ) : void ;
144
+ endSession ( ) : Promise < void > ;
134
145
endSession ( callback : Callback < void > ) : void ;
146
+ endSession ( options : Record < string , unknown > ) : Promise < void > ;
135
147
endSession ( options : Record < string , unknown > , callback : Callback < void > ) : void ;
136
- endSession ( options ?: Record < string , unknown > | Callback < void > , callback ?: Callback < void > ) : void {
137
- if ( typeof options === 'function' ) ( callback = options as Callback ) , ( options = { } ) ;
148
+ endSession (
149
+ options ?: Record < string , unknown > | Callback < void > ,
150
+ callback ?: Callback < void >
151
+ ) : void | Promise < void > {
152
+ if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
138
153
options = options || { } ;
139
154
140
- if ( this . hasEnded ) {
141
- if ( typeof callback === 'function' ) callback ( ) ;
142
- return ;
143
- }
155
+ return maybePromise ( callback , done => {
156
+ if ( this . hasEnded ) {
157
+ return done ( ) ;
158
+ }
144
159
145
- if ( this . serverSession && this . inTransaction ( ) ) {
146
- this . abortTransaction ( ) ; // pass in callback?
147
- }
160
+ const completeEndSession = ( ) => {
161
+ // release the server session back to the pool
162
+ this . sessionPool . release ( this . serverSession ) ;
163
+ this [ kServerSession ] = undefined ;
148
164
149
- // mark the session as ended, and emit a signal
150
- this . hasEnded = true ;
151
- this . emit ( 'ended' , this ) ;
165
+ // mark the session as ended, and emit a signal
166
+ this . hasEnded = true ;
167
+ this . emit ( 'ended' , this ) ;
152
168
153
- // release the server session back to the pool
154
- if ( this . serverSession ) {
155
- this . sessionPool . release ( this . serverSession ) ;
156
- }
169
+ // spec indicates that we should ignore all errors for `endSessions`
170
+ done ( ) ;
171
+ } ;
157
172
158
- this . serverSession = undefined ;
173
+ if ( this . serverSession && this . inTransaction ( ) ) {
174
+ this . abortTransaction ( err => {
175
+ if ( err ) return done ( err ) ;
176
+ completeEndSession ( ) ;
177
+ } ) ;
178
+
179
+ return ;
180
+ }
159
181
160
- // spec indicates that we should ignore all errors for `endSessions`
161
- if ( typeof callback === 'function' ) callback ( ) ;
182
+ completeEndSession ( ) ;
183
+ } ) ;
162
184
}
163
185
164
186
/**
0 commit comments