Skip to content

Commit 03056d3

Browse files
committed
feat: auto register messenger actions
1 parent 31b7973 commit 03056d3

File tree

3 files changed

+98
-89
lines changed

3 files changed

+98
-89
lines changed

packages/base-controller/src/Messenger.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,77 @@ 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 string,
503+
Action extends ActionConstraint,
504+
Event extends EventConstraint,
505+
Messenger extends RestrictedMessenger<
506+
Controller['name'],
507+
Action,
508+
Event,
509+
never,
510+
never
511+
>,
512+
>(
513+
controller: Controller,
514+
messenger: Messenger,
515+
methodNames: readonly MethodNames[],
516+
excludedMethods: readonly string[] = ['constructor', 'messagingSystem'],
517+
exceptions: Partial<
518+
Record<MethodNames, (...args: unknown[]) => unknown>
519+
> = {},
520+
): void {
521+
for (const methodName of methodNames) {
522+
if (excludedMethods.includes(methodName)) {
523+
continue;
524+
}
525+
526+
const handler =
527+
exceptions[methodName] ?? controller[methodName as keyof Controller];
528+
if (typeof handler === 'function') {
529+
const actionType =
530+
`${controller.name}:${methodName}` as `${Controller['name']}:${MethodNames}`;
531+
messenger.registerActionHandler(actionType, handler.bind(controller));
532+
}
533+
}
534+
}

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: 21 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import type {
33
ControllerStateChangeEvent,
44
RestrictedMessenger,
55
} from '@metamask/base-controller';
6-
import { BaseController } from '@metamask/base-controller';
6+
import {
7+
BaseController,
8+
registerMethodActionHandlers,
9+
} from '@metamask/base-controller';
710
import type { Partialize } from '@metamask/controller-utils';
811
import {
912
InfuraNetworkType,
@@ -1228,94 +1231,23 @@ export class NetworkController extends BaseController<
12281231
this.state.networkConfigurationsByChainId,
12291232
);
12301233

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),
1318-
);
1234+
registerMethodActionHandlers(this, this.messagingSystem, [
1235+
'getEthQuery',
1236+
'getNetworkClientById',
1237+
'getEIP1559Compatibility',
1238+
'getSelectedNetworkClient',
1239+
'setActiveNetwork',
1240+
'setProviderType',
1241+
'notExisting',
1242+
'findNetworkClientIdByChainId',
1243+
'getNetworkConfigurationByChainId',
1244+
'getNetworkConfigurationByNetworkClientId',
1245+
'getSelectedNetworkClient',
1246+
'getSelectedChainId',
1247+
'addNetwork',
1248+
'removeNetwork',
1249+
'updateNetwork',
1250+
]);
13191251
}
13201252

13211253
/**

0 commit comments

Comments
 (0)