@@ -78,9 +78,17 @@ export function executeOperation<
78
78
return maybeCallback ( ( ) => executeOperationAsync ( client , operation ) , callback ) ;
79
79
}
80
80
81
- async function maybeConnect ( client : MongoClient ) : Promise < Topology > {
82
- const { topology } = client ;
83
- if ( topology == null ) {
81
+ async function executeOperationAsync <
82
+ T extends AbstractOperation < TResult > ,
83
+ TResult = ResultTypeFromOperation < T >
84
+ > ( client : MongoClient , operation : T ) : Promise < TResult > {
85
+ if ( ! ( operation instanceof AbstractOperation ) ) {
86
+ // TODO(NODE-3483): Extend MongoRuntimeError
87
+ throw new MongoRuntimeError ( 'This method requires a valid operation instance' ) ;
88
+ }
89
+
90
+ if ( client . topology == null ) {
91
+ // Auto connect on operation
84
92
if ( client . s . hasBeenClosed ) {
85
93
throw new MongoNotConnectedError ( 'Client must be connected before running operations' ) ;
86
94
}
@@ -91,23 +99,12 @@ async function maybeConnect(client: MongoClient): Promise<Topology> {
91
99
delete client . s . options [ Symbol . for ( '@@mdb.skipPingOnConnect' ) ] ;
92
100
}
93
101
}
94
- if ( client . topology == null ) {
95
- throw new MongoRuntimeError ( 'client.connect did not create a topology but also did not throw' ) ;
96
- }
97
- return client . topology ;
98
- }
99
102
100
- async function executeOperationAsync <
101
- T extends AbstractOperation < TResult > ,
102
- TResult = ResultTypeFromOperation < T >
103
- > ( client : MongoClient , operation : T ) : Promise < TResult > {
104
- if ( ! ( operation instanceof AbstractOperation ) ) {
105
- // TODO(NODE-3483): Extend MongoRuntimeError
106
- throw new MongoRuntimeError ( 'This method requires a valid operation instance' ) ;
103
+ const { topology } = client ;
104
+ if ( topology == null ) {
105
+ throw new MongoRuntimeError ( 'client.connect did not create a topology but also did not throw' ) ;
107
106
}
108
107
109
- const topology = await maybeConnect ( client ) ;
110
-
111
108
if ( topology . shouldCheckForSessionSupport ( ) ) {
112
109
await topology . selectServerAsync ( ReadPreference . primaryPreferred , { } ) ;
113
110
}
@@ -195,23 +192,22 @@ async function executeOperationAsync<
195
192
196
193
const hasReadAspect = operation . hasAspect ( Aspect . READ_OPERATION ) ;
197
194
const hasWriteAspect = operation . hasAspect ( Aspect . WRITE_OPERATION ) ;
195
+ const retryable = ( hasReadAspect && willRetryRead ) || ( hasWriteAspect && willRetryWrite ) ;
198
196
199
- if ( hasWriteAspect && willRetryWrite ) {
197
+ if ( retryable ) {
200
198
operation . options . willRetryWrite = true ;
201
199
session . incrementTransactionNumber ( ) ;
202
200
}
203
201
204
202
try {
205
203
return await operation . executeAsync ( server , session ) ;
206
204
} catch ( operationError ) {
207
- if ( ( hasReadAspect && willRetryRead ) || ( hasWriteAspect && willRetryWrite ) ) {
208
- if ( operationError instanceof MongoError ) {
209
- return await retryOperation ( operation , operationError , {
210
- session,
211
- topology,
212
- selector
213
- } ) ;
214
- }
205
+ if ( retryable && operationError instanceof MongoError ) {
206
+ return await retryOperation ( operation , operationError , {
207
+ session,
208
+ topology,
209
+ selector
210
+ } ) ;
215
211
}
216
212
throw operationError ;
217
213
} finally {
0 commit comments