@@ -113,6 +113,9 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
113113 /** @internal */
114114 public setupTelemetryPromise : Promise < void > = Promise . resolve ( ) ;
115115
116+ // eslint-disable-next-line @typescript-eslint/no-empty-function
117+ private resolveDeviceId : ( value : string ) => void = ( ) => { } ;
118+
116119 constructor ( {
117120 bus,
118121 analytics,
@@ -132,19 +135,34 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
132135 if ( this . isSetup ) {
133136 throw new Error ( 'Setup can only be called once.' ) ;
134137 }
135- this . isBufferingBusEvents = true ;
136138 this . isBufferingTelemetryEvents = true ;
139+ this . isBufferingBusEvents = true ;
137140
138141 this . setupTelemetryPromise = this . setupTelemetry ( ) ;
139142 this . setupBusEventListeners ( ) ;
140143
141144 this . isSetup = true ;
142145 }
143146
147+ public flush ( ) : void {
148+ // Run any telemetry events even if device ID hasn't been resolved yet
149+ this . runAndClearPendingTelemetryEvents ( ) ;
150+
151+ // Run any other pending events with the set or dummy log for telemetry purposes.
152+ this . runAndClearPendingBusEvents ( ) ;
153+
154+ this . resolveDeviceId ( 'unknown' ) ;
155+ }
156+
144157 private async setupTelemetry ( ) : Promise < void > {
145158 if ( ! this . deviceId ) {
146159 try {
147- this . deviceId ??= await getDeviceId ( ) ;
160+ this . deviceId ??= await Promise . race ( [
161+ getDeviceId ( ) ,
162+ new Promise < string > ( ( resolve ) => {
163+ this . resolveDeviceId = resolve ;
164+ } ) ,
165+ ] ) ;
148166 } catch ( error ) {
149167 this . bus . emit ( 'mongosh:error' , error as Error , 'telemetry' ) ;
150168 this . deviceId = 'unknown' ;
@@ -173,8 +191,8 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
173191
174192 public detachLogger ( ) {
175193 this . log = LoggingAndTelemetry . dummyLogger ;
176- // Still run any remaining pending events with the dummy log for telemetry purposes.
177- this . runAndClearPendingBusEvents ( ) ;
194+
195+ this . flush ( ) ;
178196 }
179197
180198 private runAndClearPendingBusEvents ( ) {
0 commit comments