Skip to content

Commit 6b152ad

Browse files
W-A-Jamesnbbeeken
authored andcommitted
WIP - connect logic refactor, topology
1 parent 2248dfc commit 6b152ad

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/sdam/topology.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
452452
selector: string | ReadPreference | ServerSelector,
453453
options: SelectServerOptions
454454
): Promise<Server> {
455-
if (!this.isConnected()) {
455+
const shouldInitialize = !this.isConnected();
456+
if (shouldInitialize) {
456457
this.stateTransition(STATE_CONNECTING);
457458
// emit SDAM monitoring events
458459
this.emitAndLog(Topology.TOPOLOGY_OPENING, new TopologyOpeningEvent(this.s.id));
@@ -544,6 +545,11 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
544545
);
545546
}
546547
if (options.timeoutContext?.clearServerSelectionTimeout) timeout?.clear();
548+
if (shouldInitialize) {
549+
this.stateTransition(STATE_CONNECTED);
550+
this.emit(Topology.OPEN, this);
551+
this.emit(Topology.CONNECT, this);
552+
}
547553
return transaction.server;
548554
}
549555

@@ -571,6 +577,11 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
571577
if (options.timeoutContext?.csotEnabled() && server.description.minRoundTripTime !== 0) {
572578
options.timeoutContext.minRoundTripTime = server.description.minRoundTripTime;
573579
}
580+
if (shouldInitialize) {
581+
this.stateTransition(STATE_CONNECTED);
582+
this.emit(Topology.OPEN, this);
583+
this.emit(Topology.CONNECT, this);
584+
}
574585
return server;
575586
} catch (error) {
576587
if (TimeoutError.is(error)) {
@@ -604,6 +615,10 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
604615
}
605616
throw timeoutError;
606617
}
618+
619+
if (shouldInitialize) {
620+
this.close();
621+
}
607622
// Other server selection error
608623
throw error;
609624
} finally {

0 commit comments

Comments
 (0)