Skip to content

Commit

Permalink
feat(connect): dispatch call in progress event - needed for immediate…
Browse files Browse the repository at this point in the history
… feedback in suite when called from outside
  • Loading branch information
mroz22 committed Oct 2, 2024
1 parent dbdfa54 commit 10a19b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 6 additions & 2 deletions packages/connect/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion packages/connect/src/events/ui-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -292,7 +299,8 @@ export type UiEvent =
| FirmwareException
| FirmwareReconnect
| UiRequestAddressValidation
| UiRequestSetOperation;
| UiRequestSetOperation
| UiRequestCallInProgress;

export type UiEventMessage = UiEvent & { event: typeof UI_EVENT };

Expand Down
6 changes: 4 additions & 2 deletions suite-common/connect-init/src/connectInitThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export const connectInitThunk = createThunk(
TrezorConnect.on(UI_EVENT, ({ event: _, ...action }) => {
// dispatch event as action
dispatch(action);

if (action.type === 'ui-call_in_progress') {
dispatch(lockDevice(action.payload));
}
});

TrezorConnect.on(TRANSPORT_EVENT, ({ event: _, ...action }) => {
Expand Down Expand Up @@ -100,9 +104,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;
};
Expand Down

0 comments on commit 10a19b9

Please sign in to comment.