Skip to content

Commit

Permalink
chore(suite-desktop-connect-popup): typed call per method
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Feb 3, 2025
1 parent 7894005 commit 45ef5b0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/connect/src/events/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type CallApi = {

export type CallMethodUnion = CallApi[keyof CallApi];
export type CallMethodPayload = Parameters<CallMethodUnion>[0];
export type CallMethodParams<M extends keyof CallApi> = Parameters<CallApi[M]>[0];
export type CallMethodResponse<M extends keyof CallApi> = UnwrappedResponse<ReturnType<CallApi[M]>>;
export type CallMethodAnyResponse = ReturnType<CallMethodUnion>;

Expand Down
45 changes: 30 additions & 15 deletions packages/suite-desktop-connect-popup/src/connectPopupThunks.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { createThunk } from '@suite-common/redux-utils';
import { AsyncThunkAction } from '@reduxjs/toolkit';

import { CustomThunkAPI, createThunk } from '@suite-common/redux-utils';
import { selectSelectedDevice } from '@suite-common/wallet-core';
import TrezorConnect, { ERRORS } from '@trezor/connect';
import TrezorConnect, { CallMethodParams, CallMethodResponse, ERRORS } from '@trezor/connect';
import { serializeError } from '@trezor/connect/src/constants/errors';
import { desktopApi } from '@trezor/suite-desktop-api';
import { createDeferred } from '@trezor/utils';

const CONNECT_POPUP_MODULE = '@common/connect-popup';

export const connectPopupCallThunk = createThunk<
Promise<{
id: number;
success: boolean;
payload: any;
}>,
{
id: number;
method: string;
payload: any;
processName?: string;
origin?: string;
}
type ConnectPopupCallThunkResponse<M extends keyof typeof TrezorConnect> = Promise<{
id: number;
success: boolean;
payload: CallMethodResponse<M>;
}>;

type ConnectPopupCallThunkParams<M extends keyof typeof TrezorConnect> = {
id: number;
processName?: string;
origin?: string;
method: M;
payload: Omit<CallMethodParams<M>, 'method'>;
};

export const connectPopupCallThunkInner = createThunk<
ConnectPopupCallThunkResponse<keyof typeof TrezorConnect>,
ConnectPopupCallThunkParams<keyof typeof TrezorConnect>
>(
`${CONNECT_POPUP_MODULE}/callThunk`,
async ({ id, method, payload, processName, origin }, { dispatch, getState, extra }) => {
Expand Down Expand Up @@ -86,11 +92,20 @@ export const connectPopupCallThunk = createThunk<
},
);

export const connectPopupCallThunk = <M extends keyof typeof TrezorConnect>(
params: ConnectPopupCallThunkParams<M>,
): AsyncThunkAction<
ConnectPopupCallThunkResponse<M>,
ConnectPopupCallThunkParams<M>,
CustomThunkAPI
> => connectPopupCallThunk(params);

export const connectPopupInitThunk = createThunk(
`${CONNECT_POPUP_MODULE}/initPopupThunk`,
async (_, { dispatch }) => {
if (desktopApi.available && (await desktopApi.connectPopupEnabled())) {
desktopApi.on('connect-popup/call', async params => {
// @ts-expect-error: params in desktopApi are not fully typed
const response = await dispatch(connectPopupCallThunk(params)).unwrap();
desktopApi.connectPopupResponse(response);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/suite/src/middlewares/wallet/discoveryMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '@suite-common/wallet-core';
import * as discoveryActions from '@suite-common/wallet-core';
import { UI } from '@trezor/connect';
import { connectPopupCallThunk } from '@trezor/suite-desktop-connect-popup';
import { connectPopupCallThunkInner } from '@trezor/suite-desktop-connect-popup';

import * as walletSettingsActions from 'src/actions/settings/walletSettingsActions';
import { MODAL, ROUTER, SUITE } from 'src/actions/suite/constants';
Expand Down Expand Up @@ -153,7 +153,7 @@ export const prepareDiscoveryMiddleware = createMiddlewareWithExtraDeps(
if (
becomesConnected ||
action.type === SUITE.APP_CHANGED ||
connectPopupCallThunk.fulfilled.match(action) ||
connectPopupCallThunkInner.fulfilled.match(action) ||
deviceActions.selectDevice.match(action) ||
authorizeDeviceThunk.fulfilled.match(action) ||
walletSettingsActions.changeNetworks.match(action) ||
Expand Down

0 comments on commit 45ef5b0

Please sign in to comment.