@@ -78,9 +78,17 @@ export function executeOperation<
7878 return maybeCallback ( ( ) => executeOperationAsync ( client , operation ) , callback ) ;
7979}
8080
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
8492 if ( client . s . hasBeenClosed ) {
8593 throw new MongoNotConnectedError ( 'Client must be connected before running operations' ) ;
8694 }
@@ -91,23 +99,12 @@ async function maybeConnect(client: MongoClient): Promise<Topology> {
9199 delete client . s . options [ Symbol . for ( '@@mdb.skipPingOnConnect' ) ] ;
92100 }
93101 }
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- }
99102
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' ) ;
107106 }
108107
109- const topology = await maybeConnect ( client ) ;
110-
111108 if ( topology . shouldCheckForSessionSupport ( ) ) {
112109 await topology . selectServerAsync ( ReadPreference . primaryPreferred , { } ) ;
113110 }
@@ -195,23 +192,22 @@ async function executeOperationAsync<
195192
196193 const hasReadAspect = operation . hasAspect ( Aspect . READ_OPERATION ) ;
197194 const hasWriteAspect = operation . hasAspect ( Aspect . WRITE_OPERATION ) ;
195+ const retryable = ( hasReadAspect && willRetryRead ) || ( hasWriteAspect && willRetryWrite ) ;
198196
199- if ( hasWriteAspect && willRetryWrite ) {
197+ if ( retryable ) {
200198 operation . options . willRetryWrite = true ;
201199 session . incrementTransactionNumber ( ) ;
202200 }
203201
204202 try {
205203 return await operation . executeAsync ( server , session ) ;
206204 } 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+ } ) ;
215211 }
216212 throw operationError ;
217213 } finally {
0 commit comments