@@ -21,7 +21,7 @@ export class DeviceId {
2121
2222 public static create ( logger : LoggerBase , timeout ?: number ) : DeviceId {
2323 if ( this . instance ) {
24- return this . instance ;
24+ throw new Error ( "DeviceId instance already exists, use get() to retrieve the device ID" ) ;
2525 }
2626
2727 const instance = new DeviceId ( logger , timeout ?? DEVICE_ID_TIMEOUT ) ;
@@ -32,14 +32,8 @@ export class DeviceId {
3232 return instance ;
3333 }
3434
35- /**
36- * Sets up the device ID calculation promise and abort controller.
37- */
3835 private setup ( ) : void {
39- this . abortController = new AbortController ( ) ;
4036 this . deviceIdPromise = this . calculateDeviceId ( ) ;
41- // start the promise upon setup
42- void this . deviceIdPromise ;
4337 }
4438
4539 /**
@@ -50,6 +44,7 @@ export class DeviceId {
5044 this . abortController . abort ( ) ;
5145 this . abortController = undefined ;
5246 }
47+
5348 this . deviceId = undefined ;
5449 this . deviceIdPromise = undefined ;
5550 DeviceId . instance = undefined ;
@@ -59,60 +54,41 @@ export class DeviceId {
5954 * Gets the device ID, waiting for the calculation to complete if necessary.
6055 * @returns Promise that resolves to the device ID string
6156 */
62- public async get ( ) : Promise < string > {
63- if ( this . deviceId !== undefined ) {
64- return this . deviceId ;
57+ public get ( ) : Promise < string > {
58+ if ( this . deviceId ) {
59+ return Promise . resolve ( this . deviceId ) ;
6560 }
6661
67- if ( ! this . deviceIdPromise ) {
68- this . deviceIdPromise = this . calculateDeviceId ( ) ;
62+ if ( this . deviceIdPromise ) {
63+ return this . deviceIdPromise ;
6964 }
7065
71- return this . deviceIdPromise ;
66+ return this . calculateDeviceId ( ) ;
7267 }
7368
7469 /**
7570 * Internal method that performs the actual device ID calculation.
7671 */
7772 private async calculateDeviceId ( ) : Promise < string > {
7873 if ( ! this . abortController ) {
79- throw new Error ( "Device ID calculation not started" ) ;
74+ this . abortController = new AbortController ( ) ;
8075 }
8176
82- try {
83- const deviceId = await getDeviceId ( {
84- getMachineId : this . getMachineId ,
85- onError : ( reason , error ) => {
86- this . handleDeviceIdError ( reason , String ( error ) ) ;
87- } ,
88- timeout : this . timeout ,
89- abortSignal : this . abortController . signal ,
90- } ) ;
91-
92- // Cache the result
93- this . deviceId = deviceId ;
94- return deviceId ;
95- } catch ( error ) {
96- // Check if this was an abort error
97- if ( error instanceof Error && error . name === "AbortError" ) {
98- throw error ; // Re-throw abort errors
99- }
100-
101- this . logger . debug ( {
102- id : LogId . deviceIdResolutionError ,
103- context : "deviceId" ,
104- message : `Failed to get device ID: ${ String ( error ) } ` ,
105- } ) ;
106-
107- // Cache the fallback value
108- this . deviceId = "unknown" ;
109- return "unknown" ;
110- } finally {
111- this . abortController = undefined ;
112- }
77+ this . deviceIdPromise = getDeviceId ( {
78+ getMachineId : this . getMachineId ,
79+ onError : ( reason , error ) => {
80+ this . handleDeviceIdError ( reason , String ( error ) ) ;
81+ } ,
82+ timeout : this . timeout ,
83+ abortSignal : this . abortController . signal ,
84+ } ) ;
85+
86+ return this . deviceIdPromise ;
11387 }
11488
11589 private handleDeviceIdError ( reason : string , error : string ) : void {
90+ this . deviceIdPromise = Promise . resolve ( "unknown" ) ;
91+
11692 switch ( reason ) {
11793 case "resolutionError" :
11894 this . logger . debug ( {
0 commit comments