Skip to content

Commit ed19525

Browse files
committed
✨ (dmk): Improve refresher after new intent queue impl
1 parent 4c1498b commit ed19525

File tree

19 files changed

+129
-82
lines changed

19 files changed

+129
-82
lines changed

.changeset/cute-hands-sin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ledgerhq/device-management-kit": patch
3+
---
4+
5+
Update refresher to don't ping when an intent is being processed

.changeset/odd-ducks-find.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ledgerhq/device-signer-kit-solana": patch
3+
---
4+
5+
Update InternalApi type in test

.changeset/vast-papayas-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ledgerhq/device-signer-kit-bitcoin": patch
3+
---
4+
5+
Update InternalApi type in test

.changeset/wild-adults-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ledgerhq/device-signer-kit-ethereum": patch
3+
---
4+
5+
Update InternalApi type in test

.changeset/wild-loops-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ledgerhq/device-management-kit": minor
3+
---
4+
5+
Cleanup InternalApi to remove disableRefresher function

packages/device-management-kit/src/api/device-action/DeviceAction.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export type InternalApi = {
2626
readonly setDeviceSessionState: (
2727
state: DeviceSessionState,
2828
) => DeviceSessionState;
29-
readonly disableRefresher: (blockerId: string) => () => void;
3029
readonly getManagerApiService: () => ManagerApiService;
3130
readonly getSecureChannelService: () => SecureChannelService;
3231
};

packages/device-management-kit/src/api/device-action/__test-utils__/makeInternalApi.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const apiGetDeviceSessionStateObservableMock = vi.fn();
1010
const setDeviceSessionStateMock = vi.fn();
1111
const getManagerApiServiceMock = vi.fn();
1212
const getSecureChannelServiceMock = vi.fn();
13-
const disableRefresherMock = vi.fn();
1413

1514
export function makeDeviceActionInternalApiMock(): Mocked<InternalApi> {
1615
return {
@@ -22,6 +21,5 @@ export function makeDeviceActionInternalApiMock(): Mocked<InternalApi> {
2221
setDeviceSessionState: setDeviceSessionStateMock,
2322
getManagerApiService: getManagerApiServiceMock,
2423
getSecureChannelService: getSecureChannelServiceMock,
25-
disableRefresher: disableRefresherMock,
2624
};
2725
}

packages/device-management-kit/src/api/secure-channel/task/ConnectToSecureChannelTask.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ export class ConnectToSecureChannelTask {
4141
}
4242

4343
run(): Observable<SecureChannelEvent> {
44-
const reenableRefresher = this._api.disableRefresher(
45-
"connectToSecureChannel",
46-
);
47-
4844
const obs = new Observable<SecureChannelEvent>((subscriber) => {
4945
let unsubscribed: boolean = false;
5046
let ignoreNetworkEvents = false;
@@ -318,7 +314,6 @@ export class ConnectToSecureChannelTask {
318314
};
319315

320316
return () => {
321-
reenableRefresher();
322317
unsubscribed = true;
323318
// Close the connection if it is open when unsubscribing
324319
if (this._connection.readyState === WebSocket.OPEN) {

packages/device-management-kit/src/internal/device-session/model/DeviceSession.test.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe("DeviceSession", () => {
3333
let mockManagerApi: Mocked<ManagerApiService>;
3434
let mockSecureChannel: Mocked<SecureChannelService>;
3535
let mockIntentQueueService: Mocked<IntentQueueService>;
36+
let mockIntentQueueServiceFactory: () => IntentQueueService;
3637
let refresherOptions: DeviceSessionRefresherOptions;
3738
const mockConnectedDevice = connectedDeviceStubBuilder();
3839

@@ -66,6 +67,8 @@ describe("DeviceSession", () => {
6667
}),
6768
} as unknown as Mocked<IntentQueueService>;
6869

70+
mockIntentQueueServiceFactory = () => mockIntentQueueService;
71+
6972
refresherOptions = {
7073
isRefresherDisabled: true,
7174
};
@@ -84,7 +87,7 @@ describe("DeviceSession", () => {
8487
mockManagerApi,
8588
mockSecureChannel,
8689
refresherOptions,
87-
mockIntentQueueService,
90+
mockIntentQueueServiceFactory,
8891
);
8992

9093
// then
@@ -104,7 +107,7 @@ describe("DeviceSession", () => {
104107
mockManagerApi,
105108
mockSecureChannel,
106109
refresherOptions,
107-
mockIntentQueueService,
110+
mockIntentQueueServiceFactory,
108111
);
109112

110113
// then
@@ -131,7 +134,7 @@ describe("DeviceSession", () => {
131134
mockManagerApi,
132135
mockSecureChannel,
133136
refresherOptions,
134-
mockIntentQueueService,
137+
mockIntentQueueServiceFactory,
135138
);
136139

137140
// when
@@ -157,7 +160,7 @@ describe("DeviceSession", () => {
157160
mockManagerApi,
158161
mockSecureChannel,
159162
refresherOptions,
160-
mockIntentQueueService,
163+
mockIntentQueueServiceFactory,
161164
);
162165

163166
// when/then
@@ -175,7 +178,7 @@ describe("DeviceSession", () => {
175178
mockManagerApi,
176179
mockSecureChannel,
177180
refresherOptions,
178-
mockIntentQueueService,
181+
mockIntentQueueServiceFactory,
179182
);
180183
});
181184

@@ -208,7 +211,7 @@ describe("DeviceSession", () => {
208211
mockManagerApi,
209212
mockSecureChannel,
210213
refresherOptions,
211-
mockIntentQueueService,
214+
mockIntentQueueServiceFactory,
212215
);
213216
});
214217

@@ -271,7 +274,7 @@ describe("DeviceSession", () => {
271274
mockManagerApi,
272275
mockSecureChannel,
273276
refresherOptions,
274-
mockIntentQueueService,
277+
mockIntentQueueServiceFactory,
275278
);
276279
});
277280

@@ -377,7 +380,7 @@ describe("DeviceSession", () => {
377380
mockManagerApi,
378381
mockSecureChannel,
379382
refresherOptions,
380-
mockIntentQueueService,
383+
mockIntentQueueServiceFactory,
381384
);
382385
});
383386

@@ -486,7 +489,7 @@ describe("DeviceSession", () => {
486489
mockManagerApi,
487490
mockSecureChannel,
488491
refresherOptions,
489-
mockIntentQueueService,
492+
mockIntentQueueServiceFactory,
490493
);
491494
});
492495

@@ -594,7 +597,7 @@ describe("DeviceSession", () => {
594597
mockManagerApi,
595598
mockSecureChannel,
596599
refresherOptions,
597-
mockIntentQueueService,
600+
mockIntentQueueServiceFactory,
598601
);
599602

600603
let isComplete = false;
@@ -633,7 +636,7 @@ describe("DeviceSession", () => {
633636
mockManagerApi,
634637
mockSecureChannel,
635638
{ isRefresherDisabled: false, pollingInterval: 1000 },
636-
mockIntentQueueService,
639+
mockIntentQueueServiceFactory,
637640
);
638641
});
639642

packages/device-management-kit/src/internal/device-session/model/DeviceSession.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,18 @@ export class DeviceSession {
8484
managerApiService: ManagerApiService,
8585
secureChannelService: SecureChannelService,
8686
deviceSessionRefresherOptions: DeviceSessionRefresherOptions | undefined,
87-
intentQueueService: IntentQueueService = new IntentQueueService(
88-
loggerModuleFactory,
89-
),
87+
intentQueueServiceFactory: (
88+
sessionEventDispatcher: DeviceSessionEventDispatcher,
89+
) => IntentQueueService = (sessionEventDispatcher) =>
90+
new IntentQueueService(loggerModuleFactory, sessionEventDispatcher),
9091
) {
9192
this._id = id;
9293
this._connectedDevice = connectedDevice;
9394
this._logger = loggerModuleFactory("device-session");
94-
this._intentQueueService = intentQueueService;
9595
this._managerApiService = managerApiService;
96+
this._intentQueueService = intentQueueServiceFactory(
97+
this._sessionEventDispatcher,
98+
);
9699
this._secureChannelService = secureChannelService;
97100
this._refresherOptions = {
98101
...DEVICE_SESSION_REFRESHER_DEFAULT_OPTIONS,
@@ -130,19 +133,8 @@ export class DeviceSession {
130133
});
131134
}
132135

133-
public async initialiseSession(): Promise<void> {
134-
try {
135-
await this._pinger.ping();
136-
} catch (error) {
137-
this._logger.error("Error while initialising session", {
138-
data: { error },
139-
});
140-
throw error;
141-
} finally {
142-
if (!this._refresherOptions.isRefresherDisabled) {
143-
this._deviceSessionRefresher.startRefresher();
144-
}
145-
}
136+
public initialiseSession(): void {
137+
this._deviceSessionRefresher.startRefresher();
146138
}
147139

148140
public get id(): DeviceSessionId {
@@ -201,10 +193,6 @@ export class DeviceSession {
201193
triggersDisconnection: false,
202194
},
203195
): Promise<Either<DmkError, ApduResponse>> {
204-
this._sessionEventDispatcher.dispatch({
205-
eventName: SessionEvents.DEVICE_STATE_UPDATE_BUSY,
206-
});
207-
208196
this._logger.debug(`[exchange] => ${bufferToHexaString(rawApdu, false)}`);
209197
const result = await this._connectedDevice.sendApdu(
210198
rawApdu,
@@ -228,7 +216,7 @@ export class DeviceSession {
228216
})
229217
.ifLeft(() => {
230218
this._sessionEventDispatcher.dispatch({
231-
eventName: SessionEvents.DEVICE_STATE_UPDATE_CONNECTED,
219+
eventName: SessionEvents.DEVICE_STATE_UPDATE_UNKNOWN,
232220
});
233221
});
234222
return result;
@@ -308,8 +296,6 @@ export class DeviceSession {
308296
this.setDeviceSessionState(state);
309297
return this._deviceState.getValue();
310298
},
311-
disableRefresher: (blockerId: string) =>
312-
this._refresherService.disableRefresher(blockerId),
313299
getManagerApiService: () => this._managerApiService,
314300
getSecureChannelService: () => this._secureChannelService,
315301
});
@@ -328,6 +314,7 @@ export class DeviceSession {
328314
this._updateDeviceStatus(DeviceStatus.NOT_CONNECTED);
329315
this._deviceState.complete();
330316
this._deviceSessionRefresher.stopRefresher();
317+
this._pinger.unsubscribe();
331318
}
332319

333320
public disableRefresher(id: string): () => void {

0 commit comments

Comments
 (0)