Skip to content

Commit 01ec27d

Browse files
committed
feat: Migrate controller guidelines and examples to new Messenger
Migrate the controller guidelines and the `sample-controllers` package to the `next` export of the `@metamask/base-controller` package and the new `Messenger` class from `@metamask/messenger`.
1 parent b9634cf commit 01ec27d

11 files changed

+232
-233
lines changed

docs/controller-guidelines.md

Lines changed: 142 additions & 164 deletions
Large diffs are not rendered by default.

docs/data-services.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Next we'll define the messenger. We give the messenger a namespace, and we expos
7878
```typescript
7979
// (top of file)
8080

81-
import type { RestrictedMessenger } from '@metamask/base-controller';
81+
import type { Messenger } from '@metamask/base-controller';
8282

8383
const SERVICE_NAME = 'GasPricesService';
8484

packages/sample-controllers/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111

12+
- **BREAKING:** Migrate to new `Messenger` class ([#6335](https://github.com/MetaMask/core/pull/6335))
1213
- Bump `@metamask/utils` from `^11.2.0` to `^11.4.2` ([#6054](https://github.com/MetaMask/core/pull/6054))
1314
- Bump `@metamask/base-controller` from `^8.0.1` to `^8.1.0` ([#6284](https://github.com/MetaMask/core/pull/6284))
1415

packages/sample-controllers/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"dependencies": {
5050
"@metamask/base-controller": "^8.1.0",
51+
"@metamask/messenger": "^0.0.0",
5152
"@metamask/utils": "^11.4.2"
5253
},
5354
"devDependencies": {

packages/sample-controllers/src/sample-gas-prices-controller.test.ts

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { Messenger } from '@metamask/base-controller';
2-
import { SampleGasPricesController } from '@metamask/sample-controllers';
3-
import type { SampleGasPricesControllerMessenger } from '@metamask/sample-controllers';
1+
import {
2+
Messenger,
3+
type MessengerActions,
4+
type MessengerEvents,
5+
} from '@metamask/messenger';
46

57
import {
68
getDefaultNetworkControllerState,
79
type NetworkControllerGetStateAction,
810
} from './network-controller-types';
11+
import { SampleGasPricesController } from './sample-gas-prices-controller';
12+
import type { SampleGasPricesControllerMessenger } from './sample-gas-prices-controller';
913
import type { SampleAbstractGasPricesService } from './sample-gas-prices-service';
10-
import type {
11-
ExtractAvailableAction,
12-
ExtractAvailableEvent,
13-
} from '../../base-controller/tests/helpers';
1414

1515
describe('SampleGasPricesController', () => {
1616
describe('constructor', () => {
@@ -94,23 +94,26 @@ describe('SampleGasPricesController', () => {
9494
});
9595

9696
/**
97-
* The union of actions that the root messenger allows.
97+
* The union of all SampleGasPricesController actions.
9898
*/
99-
type RootAction = ExtractAvailableAction<SampleGasPricesControllerMessenger>;
99+
type AllSampleGasPricesControllerActions =
100+
MessengerActions<SampleGasPricesControllerMessenger>;
100101

101102
/**
102-
* The union of events that the root messenger allows.
103+
* The union of all SampleGasPricesController events.
103104
*/
104-
type RootEvent = ExtractAvailableEvent<SampleGasPricesControllerMessenger>;
105+
type AllSampleGasPricesControllerEvents =
106+
MessengerEvents<SampleGasPricesControllerMessenger>;
105107

106108
/**
107-
* Constructs the unrestricted messenger. This can be used to call actions and
108-
* publish events within the tests for this controller.
109+
* Construct the root messenger that is populated with all external actions/events that the
110+
* SampleGasPricesController needs.
109111
*
110112
* @param args - The arguments to this function.
111113
* @param args.networkControllerGetStateActionHandler - Used to mock the
112114
* `NetworkController:getState` action on the messenger.
113-
* @returns The unrestricted messenger suited for SampleGasPricesController.
115+
* @returns A root messenger that is populated with all external actions/events that the
116+
* SampleGasPricesController needs.
114117
*/
115118
function getRootMessenger({
116119
networkControllerGetStateActionHandler = jest
@@ -121,30 +124,57 @@ function getRootMessenger({
121124
.mockReturnValue(getDefaultNetworkControllerState()),
122125
}: {
123126
networkControllerGetStateActionHandler?: NetworkControllerGetStateAction['handler'];
124-
} = {}): Messenger<RootAction, RootEvent> {
125-
const rootMessenger = new Messenger<RootAction, RootEvent>();
126-
rootMessenger.registerActionHandler(
127+
} = {}): Messenger<
128+
'Root',
129+
AllSampleGasPricesControllerActions,
130+
AllSampleGasPricesControllerEvents
131+
> {
132+
const rootMessenger = new Messenger<
133+
'Root',
134+
AllSampleGasPricesControllerActions,
135+
AllSampleGasPricesControllerEvents
136+
>({ namespace: 'Root' });
137+
// Create NetworkController messenger just so that it can delegate to root
138+
const networkControllerMessenger = new Messenger<
139+
'NetworkController',
140+
NetworkControllerGetStateAction,
141+
never,
142+
typeof rootMessenger
143+
>({ namespace: 'NetworkController', parent: rootMessenger });
144+
// Register stubs for required action handlers
145+
networkControllerMessenger.registerActionHandler(
127146
'NetworkController:getState',
128147
networkControllerGetStateActionHandler,
129148
);
149+
130150
return rootMessenger;
131151
}
132152

133153
/**
134-
* Constructs the messenger which is restricted to relevant SampleGasPricesController
135-
* actions and events.
154+
* Constructs the SampleGasPricesController messenger.
136155
*
137-
* @param rootMessenger - The root messenger to restrict.
156+
* @param rootMessenger - The root messenger, with all external actions/events required by the
157+
* SampleGasPricesController messenger.
138158
* @returns The restricted messenger.
139159
*/
140160
function getMessenger(
141161
rootMessenger = getRootMessenger(),
142162
): SampleGasPricesControllerMessenger {
143-
return rootMessenger.getRestricted({
144-
name: 'SampleGasPricesController',
145-
allowedActions: ['NetworkController:getState'],
146-
allowedEvents: [],
163+
const messenger = new Messenger<
164+
'SampleGasPricesController',
165+
AllSampleGasPricesControllerActions,
166+
AllSampleGasPricesControllerEvents,
167+
typeof rootMessenger
168+
>({
169+
namespace: 'SampleGasPricesController',
170+
parent: rootMessenger,
171+
});
172+
// Delegate external actions/events
173+
rootMessenger.delegate({
174+
actions: ['NetworkController:getState'],
175+
messenger,
147176
});
177+
return messenger;
148178
}
149179

150180
/**

packages/sample-controllers/src/sample-gas-prices-controller.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type {
22
ControllerGetStateAction,
33
ControllerStateChangeEvent,
4-
RestrictedMessenger,
54
StateMetadata,
6-
} from '@metamask/base-controller';
7-
import { BaseController } from '@metamask/base-controller';
5+
} from '@metamask/base-controller/next';
6+
import { BaseController } from '@metamask/base-controller/next';
7+
import type { Messenger } from '@metamask/messenger';
88
import type { Hex } from '@metamask/utils';
99

1010
import type { NetworkControllerGetStateAction } from './network-controller-types';
@@ -123,12 +123,10 @@ type AllowedEvents = never;
123123
* The messenger which is restricted to actions and events accessed by
124124
* {@link SampleGasPricesController}.
125125
*/
126-
export type SampleGasPricesControllerMessenger = RestrictedMessenger<
126+
export type SampleGasPricesControllerMessenger = Messenger<
127127
typeof controllerName,
128128
SampleGasPricesControllerActions | AllowedActions,
129-
SampleGasPricesControllerEvents | AllowedEvents,
130-
AllowedActions['type'],
131-
AllowedEvents['type']
129+
SampleGasPricesControllerEvents | AllowedEvents
132130
>;
133131

134132
/**

packages/sample-controllers/src/sample-petnames-controller.test.ts

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Messenger } from '@metamask/base-controller';
1+
import {
2+
Messenger,
3+
type MessengerActions,
4+
type MessengerEvents,
5+
} from '@metamask/messenger';
26

37
import type { SamplePetnamesControllerMessenger } from './sample-petnames-controller';
48
import { SamplePetnamesController } from './sample-petnames-controller';
5-
import type {
6-
ExtractAvailableAction,
7-
ExtractAvailableEvent,
8-
} from '../../base-controller/tests/helpers';
99
import { PROTOTYPE_POLLUTION_BLOCKLIST } from '../../controller-utils/src/util';
1010

1111
describe('SamplePetnamesController', () => {
@@ -143,38 +143,28 @@ describe('SamplePetnamesController', () => {
143143
});
144144

145145
/**
146-
* The union of actions that the root messenger allows.
146+
* The union of all SamplePetnamesController actions.
147147
*/
148-
type RootAction = ExtractAvailableAction<SamplePetnamesControllerMessenger>;
148+
type AllSamplePetnamesControllerActions =
149+
MessengerActions<SamplePetnamesControllerMessenger>;
149150

150151
/**
151-
* The union of events that the root messenger allows.
152+
* The union of all SamplePetnamesController events.
152153
*/
153-
type RootEvent = ExtractAvailableEvent<SamplePetnamesControllerMessenger>;
154+
type AllSamplePetnamesControllerEvents =
155+
MessengerEvents<SamplePetnamesControllerMessenger>;
154156

155157
/**
156-
* Constructs the unrestricted messenger. This can be used to call actions and
157-
* publish events within the tests for this controller.
158+
* Constructs the SamplePetnamesController messenger.
158159
*
159-
* @returns The unrestricted messenger suited for SamplePetnamesController.
160+
* @returns The SamplePetnamesController messenger.
160161
*/
161-
function getRootMessenger(): Messenger<RootAction, RootEvent> {
162-
return new Messenger<RootAction, RootEvent>();
163-
}
164-
165-
/**
166-
* Constructs the messenger which is restricted to relevant SamplePetnamesController
167-
* actions and events.
168-
*
169-
* @param rootMessenger - The root messenger to restrict.
170-
* @returns The restricted messenger.
171-
*/
172-
function getMessenger(
173-
rootMessenger = getRootMessenger(),
174-
): SamplePetnamesControllerMessenger {
175-
return rootMessenger.getRestricted({
176-
name: 'SamplePetnamesController',
177-
allowedActions: [],
178-
allowedEvents: [],
162+
function getMessenger(): SamplePetnamesControllerMessenger {
163+
return new Messenger<
164+
'SamplePetnamesController',
165+
AllSamplePetnamesControllerActions,
166+
AllSamplePetnamesControllerEvents
167+
>({
168+
namespace: 'SamplePetnamesController',
179169
});
180170
}

packages/sample-controllers/src/sample-petnames-controller.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type {
22
ControllerGetStateAction,
33
ControllerStateChangeEvent,
4-
RestrictedMessenger,
54
StateMetadata,
6-
} from '@metamask/base-controller';
7-
import { BaseController } from '@metamask/base-controller';
5+
} from '@metamask/base-controller/next';
6+
import { BaseController } from '@metamask/base-controller/next';
87
import { isSafeDynamicKey } from '@metamask/controller-utils';
8+
import type { Messenger } from '@metamask/messenger';
99
import type { Hex } from '@metamask/utils';
1010

1111
// === GENERAL ===
@@ -92,12 +92,10 @@ type AllowedEvents = never;
9292
* The messenger which is restricted to actions and events accessed by
9393
* {@link SamplePetnamesController}.
9494
*/
95-
export type SamplePetnamesControllerMessenger = RestrictedMessenger<
95+
export type SamplePetnamesControllerMessenger = Messenger<
9696
typeof controllerName,
9797
SamplePetnamesControllerActions | AllowedActions,
98-
SamplePetnamesControllerEvents | AllowedEvents,
99-
AllowedActions['type'],
100-
AllowedEvents['type']
98+
SamplePetnamesControllerEvents | AllowedEvents
10199
>;
102100

103101
/**

packages/sample-controllers/tsconfig.build.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"references": [
99
{ "path": "../../packages/base-controller/tsconfig.build.json" },
10+
{ "path": "../../packages/messenger/tsconfig.build.json" },
1011
{ "path": "../../packages/network-controller/tsconfig.build.json" }
1112
],
1213
"include": ["../../types", "./src"]

packages/sample-controllers/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"references": [
77
{ "path": "../../packages/base-controller" },
88
{ "path": "../../packages/controller-utils" },
9+
{ "path": "../../packages/messenger" },
910
{ "path": "../../packages/network-controller" }
1011
],
1112
"include": ["../../types", "./src"],

0 commit comments

Comments
 (0)