From 813585f1510f6db470f09ac022c5c16216fbd3ca Mon Sep 17 00:00:00 2001 From: Martin Varmuza Date: Wed, 2 Oct 2024 17:32:55 +0200 Subject: [PATCH] feat(connect): dispatch call in progress event - needed for immediate feedback in suite when called from outside --- packages/connect/src/core/index.ts | 8 ++++++-- packages/connect/src/events/ui-request.ts | 10 +++++++++- suite-common/connect-init/src/connectInitThunks.ts | 6 +++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/connect/src/core/index.ts b/packages/connect/src/core/index.ts index 561767f337c0..409bd1d1898e 100644 --- a/packages/connect/src/core/index.ts +++ b/packages/connect/src/core/index.ts @@ -518,7 +518,12 @@ const onCall = async (context: CoreContext, message: IFrameCallMessage) => { return Promise.resolve(); } - return await onCallDevice(context, message, method); + // it totally does not belong here. todo: discuss with marek + sendCoreMessage(createUiMessage(UI.CALL_IN_PROGRESS, true)); + + return await onCallDevice(context, message, method).finally(() => { + sendCoreMessage(createUiMessage(UI.CALL_IN_PROGRESS, false)); + }); }; const onCallDevice = async ( @@ -570,7 +575,6 @@ const onCallDevice = async ( const device = tempDevice; method.setDevice(device); - // find pending calls to this device const previousCall = callMethods.filter( call => call && call !== method && call.devicePath === method.devicePath, diff --git a/packages/connect/src/events/ui-request.ts b/packages/connect/src/events/ui-request.ts index 12d797b63c27..e6990d7a255f 100644 --- a/packages/connect/src/events/ui-request.ts +++ b/packages/connect/src/events/ui-request.ts @@ -24,6 +24,7 @@ export const UI_REQUEST = { FIRMWARE_NOT_COMPATIBLE: 'ui-device_firmware_not_compatible', FIRMWARE_NOT_INSTALLED: 'ui-device_firmware_not_installed', FIRMWARE_PROGRESS: 'ui-firmware-progress', + CALL_IN_PROGRESS: 'ui-call_in_progress', /** connect is waiting for device to be automatically reconnected */ FIRMWARE_RECONNECT: 'ui-firmware_reconnect', @@ -157,6 +158,12 @@ export interface UiRequestSetOperation { payload: string; } +// todo: is it request? not really. at least naming, ( if not category) is misleading here +export interface UiRequestCallInProgress { + type: typeof UI_REQUEST.CALL_IN_PROGRESS; + payload: boolean; +} + export interface UiRequestPermission { type: typeof UI_REQUEST.REQUEST_PERMISSION; payload: { @@ -292,7 +299,8 @@ export type UiEvent = | FirmwareException | FirmwareReconnect | UiRequestAddressValidation - | UiRequestSetOperation; + | UiRequestSetOperation + | UiRequestCallInProgress; export type UiEventMessage = UiEvent & { event: typeof UI_EVENT }; diff --git a/suite-common/connect-init/src/connectInitThunks.ts b/suite-common/connect-init/src/connectInitThunks.ts index 02c760d59e6a..5147ebcc3ac2 100644 --- a/suite-common/connect-init/src/connectInitThunks.ts +++ b/suite-common/connect-init/src/connectInitThunks.ts @@ -45,7 +45,9 @@ export const connectInitThunk = createThunk( }); TrezorConnect.on(UI_EVENT, ({ event: _, ...action }) => { - // dispatch event as action + if (action.type === 'ui-call_in_progress') { + dispatch(lockDevice(action.payload)); + } dispatch(action); }); @@ -100,9 +102,7 @@ export const connectInitThunk = createThunk( const original: any = TrezorConnect[key]; if (!original) return; (TrezorConnect[key] as any) = async (params: any) => { - dispatch(lockDevice(true)); const result = await synchronize(() => original(params)); - dispatch(lockDevice(false)); return result; };