Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ‘” (webhid) [NO-ISSUE]: Update reconnection error sending #631

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-spies-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-transport-kit-web-hid": patch
---

Update reconnection event to trigger error only after a specific time
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type WebHidDeviceConnectionConstructorArgs = {
onConnectionTerminated: () => void;
};

type Timer = ReturnType<typeof setTimeout>;

/**
* Class to manage the connection with a USB HID device.
* It sends APDU commands to the device and receives the responses.
Expand All @@ -44,7 +46,9 @@ export class WebHidDeviceConnection implements DeviceConnection {
/** Flag to indicate if the connection is waiting for a reconnection */
private waitingForReconnection = false;
/** Timeout to wait for the device to reconnect */
private lostConnectionTimeout: NodeJS.Timeout | null = null;
private lostConnectionTimeout: Timer | null = null;
/** Time since disconnection */
private timeSinceDisconnection: Maybe<number> = Nothing;
/** Flag to indicate if the connection is terminated */
private terminated = false;

Expand Down Expand Up @@ -194,6 +198,7 @@ export class WebHidDeviceConnection implements DeviceConnection {
* */
public lostConnection() {
this._logger.info("⏱️ Lost connection, starting timer");
this.timeSinceDisconnection = Maybe.of(Date.now());
this.waitingForReconnection = true;
this.lostConnectionTimeout = setTimeout(() => {
this._logger.info("❌ Disconnection timeout, terminating connection");
Expand All @@ -214,7 +219,16 @@ export class WebHidDeviceConnection implements DeviceConnection {
await device.open();

if (this._pendingApdu.isJust()) {
this._sendApduSubject.error(new WebHidSendReportError());
if (this.timeSinceDisconnection.isJust()) {
const now = Date.now();
const timeSinceDisconnection =
now - this.timeSinceDisconnection.extract();
// 3 seconds timeout
if (timeSinceDisconnection > RECONNECT_DEVICE_TIMEOUT / 2) {
this._sendApduSubject.error(new WebHidSendReportError());
}
}
this.timeSinceDisconnection = Nothing;
}

this.waitingForReconnection = false;
Expand Down
Loading