Skip to content

feat: auto register Messenger actions #5927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eslint-warning-thresholds.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@
"jsdoc/check-tag-names": 2
},
"packages/base-controller/src/Messenger.test.ts": {
"import-x/namespace": 33
"import-x/namespace": 34
},
"packages/base-controller/src/RestrictedMessenger.test.ts": {
"import-x/namespace": 31
"import-x/namespace": 32
},
"packages/build-utils/src/transforms/remove-fenced-code.test.ts": {
"import-x/order": 1
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"changelog:update": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run changelog:update",
"changelog:validate": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run changelog:validate",
"create-package": "ts-node scripts/create-package",
"generate-method-action-types": "ts-node scripts/generate-method-action-types.ts",
"lint": "yarn lint:eslint && echo && yarn lint:misc --check && yarn constraints && yarn lint:dependencies && yarn lint:teams",
"lint:dependencies": "depcheck && yarn dedupe --check",
"lint:dependencies:fix": "depcheck && yarn dedupe",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* This file is auto generated by `scripts/generate-method-action-types.ts`.
* Do not edit manually.
*/
import type { AddressBookController } from './AddressBookController';

/**
* Returns all address book entries as an array.
*
* @returns Array of all address book entries.
*/
export type AddressBookControllerListAction = {
type: `AddressBookController:list`;
handler: AddressBookController['list'];
};

/**
* Remove a contract entry by address.
*
* @param chainId - Chain id identifies the current chain.
* @param address - Recipient address to delete.
* @returns Whether the entry was deleted.
*/
export type AddressBookControllerDeleteAction = {
type: `AddressBookController:delete`;
handler: AddressBookController['delete'];
};

/**
* Add or update a contact entry by address.
*
* @param address - Recipient address to add or update.
* @param name - Nickname to associate with this address.
* @param chainId - Chain id identifies the current chain.
* @param memo - User's note about address.
* @param addressType - Contact's address type.
* @returns Boolean indicating if the address was successfully set.
*/
export type AddressBookControllerSetAction = {
type: `AddressBookController:set`;
handler: AddressBookController['set'];
};
60 changes: 14 additions & 46 deletions packages/address-book-controller/src/AddressBookController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
ControllerGetStateAction,
ControllerStateChangeEvent,
MessengerMethodActions,
RestrictedMessenger,
} from '@metamask/base-controller';
import { BaseController } from '@metamask/base-controller';
Expand Down Expand Up @@ -81,30 +82,6 @@ export type AddressBookControllerGetStateAction = ControllerGetStateAction<
AddressBookControllerState
>;

/**
* The action that can be performed to list contacts from the {@link AddressBookController}.
*/
export type AddressBookControllerListAction = {
type: `${typeof controllerName}:list`;
handler: AddressBookController['list'];
};

/**
* The action that can be performed to set a contact in the {@link AddressBookController}.
*/
export type AddressBookControllerSetAction = {
type: `${typeof controllerName}:set`;
handler: AddressBookController['set'];
};

/**
* The action that can be performed to delete a contact from the {@link AddressBookController}.
*/
export type AddressBookControllerDeleteAction = {
type: `${typeof controllerName}:delete`;
handler: AddressBookController['delete'];
};

/**
* Event emitted when a contact is added or updated
*/
Expand All @@ -121,14 +98,20 @@ export type AddressBookControllerContactDeletedEvent = {
payload: [AddressBookEntry];
};

// Define the methods we want to expose via the messenger
const MESSENGER_EXPOSED_METHODS = ['list', 'set', 'delete'] as const;

type AddressBookControllerMethodActions = MessengerMethodActions<
AddressBookController,
(typeof MESSENGER_EXPOSED_METHODS)[number]
>;

/**
* The actions that can be performed using the {@link AddressBookController}.
*/
export type AddressBookControllerActions =
| AddressBookControllerGetStateAction
| AddressBookControllerListAction
| AddressBookControllerSetAction
| AddressBookControllerDeleteAction;
| AddressBookControllerMethodActions;

/**
* The event that {@link AddressBookController} can emit.
Expand Down Expand Up @@ -203,7 +186,10 @@ export class AddressBookController extends BaseController<
state: mergedState,
});

this.#registerMessageHandlers();
this.messagingSystem.registerActionHandlers(
this,
MESSENGER_EXPOSED_METHODS,
);
}

/**
Expand Down Expand Up @@ -338,24 +324,6 @@ export class AddressBookController extends BaseController<

return true;
}

/**
* Registers message handlers for the AddressBookController.
*/
#registerMessageHandlers() {
this.messagingSystem.registerActionHandler(
`${controllerName}:list`,
this.list.bind(this),
);
this.messagingSystem.registerActionHandler(
`${controllerName}:set`,
this.set.bind(this),
);
this.messagingSystem.registerActionHandler(
`${controllerName}:delete`,
this.delete.bind(this),
);
}
}

export default AddressBookController;
8 changes: 5 additions & 3 deletions packages/address-book-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ export type {
AddressBookEntry,
AddressBookControllerState,
AddressBookControllerGetStateAction,
AddressBookControllerListAction,
AddressBookControllerSetAction,
AddressBookControllerDeleteAction,
AddressBookControllerActions,
AddressBookControllerStateChangeEvent,
AddressBookControllerContactUpdatedEvent,
Expand All @@ -18,3 +15,8 @@ export {
getDefaultAddressBookControllerState,
AddressBookController,
} from './AddressBookController';
export type {
AddressBookControllerListAction,
AddressBookControllerSetAction,
AddressBookControllerDeleteAction,
} from './AddressBookController-method-action-types';
2 changes: 1 addition & 1 deletion packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type {
NetworkClientId,
NetworkControllerGetNetworkClientByIdAction,
} from '@metamask/network-controller';
import type { NetworkControllerFindNetworkClientIdByChainIdAction } from '@metamask/network-controller';
import type { BulkPhishingDetectionScanResponse } from '@metamask/phishing-controller';
import { RecommendedAction } from '@metamask/phishing-controller';
import type {
Expand Down Expand Up @@ -69,7 +70,6 @@ import type {
GetCollectionsResponse,
TopBid,
} from './NftDetectionController';
import type { NetworkControllerFindNetworkClientIdByChainIdAction } from '../../network-controller/src/NetworkController';

export type NFTStandardType = 'ERC721' | 'ERC1155';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import type {
NetworkClient,
NetworkControllerGetNetworkClientByIdAction,
NetworkControllerFindNetworkClientIdByChainIdAction,
NetworkControllerStateChangeEvent,
NetworkControllerGetStateAction,
} from '@metamask/network-controller';
Expand All @@ -33,7 +34,6 @@ import {
type NftControllerState,
type NftMetadata,
} from './NftController';
import type { NetworkControllerFindNetworkClientIdByChainIdAction } from '../../network-controller/src/NetworkController';

const controllerName = 'NftDetectionController';

Expand Down
9 changes: 9 additions & 0 deletions packages/base-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `registerActionHandlers` method to `Messenger`, and `RestrictedMessenger` for simplified bulk action handler registration ([#5927](https://github.com/MetaMask/core/pull/5927))
- Allows registering multiple action handlers at once by passing an array of method names
- Supports automatic exclusion of specified methods (defaults to `['constructor', 'messagingSystem']`)
- Provides custom handler exceptions for specific methods
- Automatically binds methods to the controller instance
- Reduces boilerplate code when registering multiple action handlers

### Changed

- Bump `@metamask/utils` from `^11.2.0` to `^11.4.2` ([#6054](https://github.com/MetaMask/core/pull/6054))
Expand Down
Loading
Loading