Skip to content

Commit 5dec2b5

Browse files
committed
feat: auto register Messenger actions
1 parent 31b7973 commit 5dec2b5

File tree

3 files changed

+107
-165
lines changed

3 files changed

+107
-165
lines changed

packages/base-controller/src/Messenger.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,78 @@ export class Messenger<
458458
});
459459
}
460460
}
461+
462+
/**
463+
* Creates an action type for a method on a controller class
464+
*
465+
* @template Controller - The controller class type
466+
* @template MethodName - The name of the method to create an action for
467+
*/
468+
export type MessengerMethodAction<
469+
Controller extends { name: string },
470+
MethodName extends keyof Controller & string,
471+
> = {
472+
type: `${Controller['name']}:${MethodName}`;
473+
handler: Controller[MethodName] extends (...args: unknown[]) => unknown
474+
? Controller[MethodName]
475+
: never;
476+
};
477+
478+
/**
479+
* Creates a union type of action types for multiple methods on a controller class
480+
*
481+
* @template Controller - The controller class type
482+
* @template MethodNames - A union of method names to create actions for
483+
*/
484+
export type MessengerMethodActions<
485+
Controller extends { name: string },
486+
MethodNames extends keyof Controller & string,
487+
> = {
488+
[K in MethodNames]: MessengerMethodAction<Controller, K>;
489+
}[MethodNames];
490+
491+
/**
492+
* Registers action handlers for a list of methods on a controller
493+
*
494+
* @param controller - The controller instance
495+
* @param messenger - The messenger to register the handlers with
496+
* @param methodNames - The names of the methods to register as action handlers
497+
* @param excludedMethods - Optional list of method names to exclude from registration
498+
* @param exceptions - Optional map of method names to custom handlers
499+
*/
500+
export function registerMethodActionHandlers<
501+
Controller extends { name: string },
502+
MethodNames extends keyof Controller & string,
503+
Action extends ActionConstraint,
504+
Event extends EventConstraint,
505+
AllowedAction extends string,
506+
AllowedEvent extends string,
507+
Messenger extends RestrictedMessenger<
508+
Controller['name'],
509+
Action,
510+
Event,
511+
AllowedAction,
512+
AllowedEvent
513+
>,
514+
>(
515+
controller: Controller,
516+
messenger: Messenger,
517+
methodNames: readonly MethodNames[],
518+
excludedMethods: readonly string[] = ['constructor', 'messagingSystem'],
519+
exceptions: Partial<
520+
Record<MethodNames, (...args: unknown[]) => unknown>
521+
> = {},
522+
): void {
523+
for (const methodName of methodNames) {
524+
if (excludedMethods.includes(methodName)) {
525+
continue;
526+
}
527+
528+
const handler = exceptions[methodName] ?? controller[methodName];
529+
if (typeof handler === 'function') {
530+
const actionType =
531+
`${controller.name}:${methodName}` as `${Controller['name']}:${MethodNames}`;
532+
messenger.registerActionHandler(actionType, handler.bind(controller));
533+
}
534+
}
535+
}

packages/base-controller/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export type {
3030
NamespacedBy,
3131
NotNamespacedBy,
3232
NamespacedName,
33+
MessengerMethodAction,
34+
MessengerMethodActions,
3335
} from './Messenger';
3436
export { Messenger } from './Messenger';
3537
export type { RestrictedMessengerConstraint } from './RestrictedMessenger';
3638
export { RestrictedMessenger } from './RestrictedMessenger';
39+
export { registerMethodActionHandlers } from './Messenger';

packages/network-controller/src/NetworkController.ts

Lines changed: 29 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import type {
22
ControllerGetStateAction,
33
ControllerStateChangeEvent,
4+
MessengerMethodAction,
45
RestrictedMessenger,
56
} from '@metamask/base-controller';
6-
import { BaseController } from '@metamask/base-controller';
7+
import {
8+
BaseController,
9+
registerMethodActionHandlers,
10+
} from '@metamask/base-controller';
711
import type { Partialize } from '@metamask/controller-utils';
812
import {
913
InfuraNetworkType,
@@ -515,87 +519,30 @@ export type NetworkControllerGetEthQueryAction = {
515519
handler: () => EthQuery | undefined;
516520
};
517521

518-
export type NetworkControllerGetNetworkClientByIdAction = {
519-
type: `NetworkController:getNetworkClientById`;
520-
handler: NetworkController['getNetworkClientById'];
521-
};
522-
523-
export type NetworkControllerGetSelectedNetworkClientAction = {
524-
type: `NetworkController:getSelectedNetworkClient`;
525-
handler: NetworkController['getSelectedNetworkClient'];
526-
};
527-
528-
export type NetworkControllerGetSelectedChainIdAction = {
529-
type: 'NetworkController:getSelectedChainId';
530-
handler: NetworkController['getSelectedChainId'];
531-
};
532-
533-
export type NetworkControllerGetEIP1559CompatibilityAction = {
534-
type: `NetworkController:getEIP1559Compatibility`;
535-
handler: NetworkController['getEIP1559Compatibility'];
536-
};
537-
538-
export type NetworkControllerFindNetworkClientIdByChainIdAction = {
539-
type: `NetworkController:findNetworkClientIdByChainId`;
540-
handler: NetworkController['findNetworkClientIdByChainId'];
541-
};
542-
543-
/**
544-
* Change the currently selected network to the given built-in network type.
545-
*
546-
* @deprecated This action has been replaced by `setActiveNetwork`, and will be
547-
* removed in a future release.
548-
*/
549-
export type NetworkControllerSetProviderTypeAction = {
550-
type: `NetworkController:setProviderType`;
551-
handler: NetworkController['setProviderType'];
552-
};
553-
554-
export type NetworkControllerSetActiveNetworkAction = {
555-
type: `NetworkController:setActiveNetwork`;
556-
handler: NetworkController['setActiveNetwork'];
557-
};
558-
559-
export type NetworkControllerGetNetworkConfigurationByChainId = {
560-
type: `NetworkController:getNetworkConfigurationByChainId`;
561-
handler: NetworkController['getNetworkConfigurationByChainId'];
562-
};
563-
564-
export type NetworkControllerGetNetworkConfigurationByNetworkClientId = {
565-
type: `NetworkController:getNetworkConfigurationByNetworkClientId`;
566-
handler: NetworkController['getNetworkConfigurationByNetworkClientId'];
567-
};
568-
569-
export type NetworkControllerAddNetworkAction = {
570-
type: 'NetworkController:addNetwork';
571-
handler: NetworkController['addNetwork'];
572-
};
573-
574-
export type NetworkControllerRemoveNetworkAction = {
575-
type: 'NetworkController:removeNetwork';
576-
handler: NetworkController['removeNetwork'];
577-
};
578-
579-
export type NetworkControllerUpdateNetworkAction = {
580-
type: 'NetworkController:updateNetwork';
581-
handler: NetworkController['updateNetwork'];
582-
};
522+
const MESSENGER_EXPOSED_METHODS = [
523+
'getNetworkClientById',
524+
'getSelectedNetworkClient',
525+
'getSelectedChainId',
526+
'getEIP1559Compatibility',
527+
'findNetworkClientIdByChainId',
528+
'setProviderType',
529+
'setActiveNetwork',
530+
'getNetworkConfigurationByChainId',
531+
'getNetworkConfigurationByNetworkClientId',
532+
'addNetwork',
533+
'removeNetwork',
534+
'updateNetwork',
535+
] as const;
536+
537+
export type NetworkControllerMethodAction = MessengerMethodAction<
538+
NetworkController,
539+
(typeof MESSENGER_EXPOSED_METHODS)[number]
540+
>;
583541

584542
export type NetworkControllerActions =
585543
| NetworkControllerGetStateAction
586544
| NetworkControllerGetEthQueryAction
587-
| NetworkControllerGetNetworkClientByIdAction
588-
| NetworkControllerGetSelectedNetworkClientAction
589-
| NetworkControllerGetSelectedChainIdAction
590-
| NetworkControllerGetEIP1559CompatibilityAction
591-
| NetworkControllerFindNetworkClientIdByChainIdAction
592-
| NetworkControllerSetActiveNetworkAction
593-
| NetworkControllerSetProviderTypeAction
594-
| NetworkControllerGetNetworkConfigurationByChainId
595-
| NetworkControllerGetNetworkConfigurationByNetworkClientId
596-
| NetworkControllerAddNetworkAction
597-
| NetworkControllerRemoveNetworkAction
598-
| NetworkControllerUpdateNetworkAction;
545+
| NetworkControllerMethodAction;
599546

600547
/**
601548
* All actions that {@link NetworkController} calls internally.
@@ -1228,93 +1175,10 @@ export class NetworkController extends BaseController<
12281175
this.state.networkConfigurationsByChainId,
12291176
);
12301177

1231-
this.messagingSystem.registerActionHandler(
1232-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
1233-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1234-
`${this.name}:getEthQuery`,
1235-
() => {
1236-
return this.#ethQuery;
1237-
},
1238-
);
1239-
1240-
this.messagingSystem.registerActionHandler(
1241-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
1242-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1243-
`${this.name}:getNetworkClientById`,
1244-
this.getNetworkClientById.bind(this),
1245-
);
1246-
1247-
this.messagingSystem.registerActionHandler(
1248-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
1249-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1250-
`${this.name}:getEIP1559Compatibility`,
1251-
this.getEIP1559Compatibility.bind(this),
1252-
);
1253-
1254-
this.messagingSystem.registerActionHandler(
1255-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
1256-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1257-
`${this.name}:setActiveNetwork`,
1258-
this.setActiveNetwork.bind(this),
1259-
);
1260-
1261-
this.messagingSystem.registerActionHandler(
1262-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
1263-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1264-
`${this.name}:setProviderType`,
1265-
this.setProviderType.bind(this),
1266-
);
1267-
1268-
this.messagingSystem.registerActionHandler(
1269-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
1270-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1271-
`${this.name}:findNetworkClientIdByChainId`,
1272-
this.findNetworkClientIdByChainId.bind(this),
1273-
);
1274-
1275-
this.messagingSystem.registerActionHandler(
1276-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
1277-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1278-
`${this.name}:getNetworkConfigurationByChainId`,
1279-
this.getNetworkConfigurationByChainId.bind(this),
1280-
);
1281-
1282-
this.messagingSystem.registerActionHandler(
1283-
// ESLint is mistaken here; `name` is a string.
1284-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1285-
`${this.name}:getNetworkConfigurationByNetworkClientId`,
1286-
this.getNetworkConfigurationByNetworkClientId.bind(this),
1287-
);
1288-
1289-
this.messagingSystem.registerActionHandler(
1290-
`${this.name}:getSelectedNetworkClient`,
1291-
this.getSelectedNetworkClient.bind(this),
1292-
);
1293-
1294-
this.messagingSystem.registerActionHandler(
1295-
`${this.name}:getSelectedChainId`,
1296-
this.getSelectedChainId.bind(this),
1297-
);
1298-
1299-
this.messagingSystem.registerActionHandler(
1300-
// ESLint is mistaken here; `name` is a string.
1301-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1302-
`${this.name}:addNetwork`,
1303-
this.addNetwork.bind(this),
1304-
);
1305-
1306-
this.messagingSystem.registerActionHandler(
1307-
// ESLint is mistaken here; `name` is a string.
1308-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1309-
`${this.name}:removeNetwork`,
1310-
this.removeNetwork.bind(this),
1311-
);
1312-
1313-
this.messagingSystem.registerActionHandler(
1314-
// ESLint is mistaken here; `name` is a string.
1315-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1316-
`${this.name}:updateNetwork`,
1317-
this.updateNetwork.bind(this),
1178+
registerMethodActionHandlers(
1179+
this,
1180+
this.messagingSystem,
1181+
MESSENGER_EXPOSED_METHODS,
13181182
);
13191183
}
13201184

0 commit comments

Comments
 (0)