Skip to content

Commit 22c9991

Browse files
committed
refactor: remove timeout and deferred promise logic
1 parent a73de1f commit 22c9991

File tree

2 files changed

+11
-82
lines changed

2 files changed

+11
-82
lines changed

src/deferred-promise.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/telemetry/telemetry.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { ApiClient } from "../common/atlas/apiClient.js";
66
import { MACHINE_METADATA } from "./constants.js";
77
import { EventCache } from "./eventCache.js";
88
import nodeMachineId from "node-machine-id";
9-
import { DeferredPromise } from "../deferred-promise.js";
10-
import { getDeviceId as extractDeviceId } from "@mongodb-js/device-id";
9+
import { getDeviceId } from "@mongodb-js/device-id";
1110

1211
type EventResult = {
1312
success: boolean;
@@ -19,7 +18,8 @@ export const DEVICE_ID_TIMEOUT = 3000;
1918
export class Telemetry {
2019
private isBufferingEvents: boolean = true;
2120
/** Resolves when the device ID is retrieved or timeout occurs */
22-
public deviceIdPromise: DeferredPromise<string> | undefined;
21+
public deviceIdPromise: Promise<string> | undefined;
22+
public resolveDeviceId: ((value: string) => void) | undefined;
2323
private eventCache: EventCache;
2424
private getRawMachineId: () => Promise<string>;
2525

@@ -58,37 +58,24 @@ export class Telemetry {
5858
if (!this.isTelemetryEnabled()) {
5959
return;
6060
}
61-
this.deviceIdPromise = DeferredPromise.fromPromise(this.getDeviceId(), {
62-
timeout: DEVICE_ID_TIMEOUT,
63-
onTimeout: (resolve) => {
64-
resolve("unknown");
65-
logger.debug(LogId.telemetryDeviceIdTimeout, "telemetry", "Device ID retrieval timed out");
66-
},
61+
const { value: deviceId, resolve: resolveDeviceId } = getDeviceId({
62+
getMachineId: () => this.getRawMachineId(),
63+
isNodeMachineId: true,
64+
onError: (error) => logger.debug(LogId.telemetryDeviceIdFailure, "telemetry", String(error)),
6765
});
68-
this.commonProperties.device_id = await this.deviceIdPromise;
66+
67+
this.resolveDeviceId = resolveDeviceId;
68+
this.commonProperties.device_id = await deviceId;
6969

7070
this.isBufferingEvents = false;
7171
}
7272

7373
public async close(): Promise<void> {
74-
this.deviceIdPromise?.resolve("unknown");
74+
this.resolveDeviceId?.("unknown");
7575
this.isBufferingEvents = false;
7676
await this.emitEvents(this.eventCache.getEvents());
7777
}
7878

79-
/**
80-
* @returns A hashed, unique identifier for the running device or `"unknown"` if not known.
81-
*/
82-
private async getDeviceId(): Promise<string> {
83-
if (this.commonProperties.device_id) {
84-
return this.commonProperties.device_id;
85-
}
86-
return extractDeviceId({
87-
getMachineId: () => this.getRawMachineId(),
88-
isNodeMachineId: true,
89-
}).value;
90-
}
91-
9279
/**
9380
* Emits events through the telemetry pipeline
9481
* @param events - The events to emit

0 commit comments

Comments
 (0)