Skip to content

Commit 658cdc8

Browse files
committed
new device id
1 parent bdf1272 commit 658cdc8

File tree

1 file changed

+22
-61
lines changed

1 file changed

+22
-61
lines changed

src/helpers/deviceId.ts

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { LogId, LoggerBase } from "../common/logger.js";
44

55
export const DEVICE_ID_TIMEOUT = 3000;
66

7-
/**
8-
* Singleton class for managing device ID retrieval and caching.
9-
* Starts device ID calculation early and is shared across all services.
10-
*/
117
export class DeviceIdService {
128
private static instance: DeviceIdService | undefined = undefined;
139
private deviceId: string | undefined = undefined;
@@ -21,55 +17,37 @@ export class DeviceIdService {
2117
this.logger = logger;
2218
this.timeout = timeout;
2319
this.getMachineId = (): Promise<string> => nodeMachineId.machineId(true);
24-
// Start device ID calculation immediately
25-
this.startDeviceIdCalculation();
2620
}
2721

28-
/**
29-
* Initializes the DeviceIdService singleton with a logger.
30-
* A separated init method is used to use a single instance of the logger.
31-
* @param logger - The logger instance to use
32-
* @returns The DeviceIdService instance
33-
*/
34-
public static init(logger: LoggerBase, timeout?: number): DeviceIdService {
35-
if (DeviceIdService.instance) {
36-
return DeviceIdService.instance;
37-
}
38-
DeviceIdService.instance = new DeviceIdService(logger, timeout ?? DEVICE_ID_TIMEOUT);
39-
return DeviceIdService.instance;
40-
}
22+
static create(
23+
logger: LoggerBase,
24+
{ timeout = DEVICE_ID_TIMEOUT }: { timeout?: number } = {},
25+
): DeviceIdService {
26+
const instance = new DeviceIdService(logger, timeout);
4127

42-
/**
43-
* Checks if the DeviceIdService is initialized.
44-
* @returns True if the DeviceIdService is initialized, false otherwise
45-
*/
46-
public static isInitialized(): boolean {
47-
return DeviceIdService.instance !== undefined;
28+
void instance.setup();
29+
return instance;
4830
}
4931

50-
/**
51-
* Gets the singleton instance of DeviceIdService.
52-
* @returns The DeviceIdService instance
53-
*/
54-
public static getInstance(): DeviceIdService {
55-
if (!DeviceIdService.instance) {
56-
throw new Error("DeviceIdService not initialized");
57-
}
58-
return DeviceIdService.instance;
32+
private async setup(): Promise<void> {
33+
this.abortController = new AbortController();
34+
35+
this.deviceIdPromise = this.calculateDeviceId();
36+
const deviceId = await this.deviceIdPromise;
37+
this.deviceId = deviceId;
5938
}
6039

61-
/**
62-
* Starts the device ID calculation process.
63-
* This method is called automatically in the constructor.
64-
*/
65-
private startDeviceIdCalculation(): void {
66-
if (this.deviceIdPromise) {
67-
return;
40+
41+
public close(): void {
42+
if (this.abortController) {
43+
this.abortController.abort();
44+
this.abortController = undefined;
6845
}
69-
70-
this.abortController = new AbortController();
71-
this.deviceIdPromise = this.calculateDeviceId();
46+
this.deviceId = undefined;
47+
this.deviceIdPromise = undefined;
48+
DeviceIdService.instance = undefined;
7249
}
50+
7351

7452
/**
7553
* Gets the device ID, waiting for the calculation to complete if necessary.
@@ -86,18 +64,6 @@ export class DeviceIdService {
8664

8765
return this.deviceIdPromise;
8866
}
89-
/**
90-
* Aborts any ongoing device ID calculation.
91-
*/
92-
public close(): void {
93-
if (this.abortController) {
94-
this.abortController.abort();
95-
this.abortController = undefined;
96-
}
97-
this.deviceId = undefined;
98-
this.deviceIdPromise = undefined;
99-
DeviceIdService.instance = undefined;
100-
}
10167

10268
/**
10369
* Internal method that performs the actual device ID calculation.
@@ -140,11 +106,6 @@ export class DeviceIdService {
140106
}
141107
}
142108

143-
/**
144-
* Handles device ID error.
145-
* @param reason - The reason for the error
146-
* @param error - The error object
147-
*/
148109
private handleDeviceIdError(reason: string, error: string): void {
149110
switch (reason) {
150111
case "resolutionError":

0 commit comments

Comments
 (0)