Skip to content

Commit 815bf50

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 513bb4b commit 815bf50

11 files changed

+201
-269
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: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
import { Messenger } from '@metamask/base-controller';
2-
import { SampleGasPricesController } from '@metamask/sample-controllers';
3-
import type { SampleGasPricesControllerMessenger } from '@metamask/sample-controllers';
4-
51
import {
6-
getDefaultNetworkControllerState,
7-
type NetworkControllerGetStateAction,
8-
} from './network-controller-types';
2+
Messenger,
3+
type MessengerActions,
4+
type MessengerEvents,
5+
} from '@metamask/messenger';
6+
97
import type { SampleAbstractGasPricesService } from './sample-gas-prices-service';
10-
import type {
11-
ExtractAvailableAction,
12-
ExtractAvailableEvent,
13-
} from '../../base-controller/tests/helpers';
8+
import { SampleGasPricesController } from '@metamask/sample-controllers';
9+
import type { SampleGasPricesControllerMessenger } from '@metamask/sample-controllers';
1410

1511
describe('SampleGasPricesController', () => {
1612
describe('constructor', () => {
@@ -66,14 +62,8 @@ describe('SampleGasPricesController', () => {
6662
average: 10,
6763
high: 15,
6864
});
69-
const rootMessenger = getRootMessenger({
70-
networkControllerGetStateActionHandler: () => ({
71-
...getDefaultNetworkControllerState(),
72-
chainId: '0x42',
73-
}),
74-
});
7565
const controller = new SampleGasPricesController({
76-
messenger: getMessenger(rootMessenger),
66+
messenger: getMessenger(),
7767
gasPricesService,
7868
});
7969

@@ -94,56 +84,29 @@ describe('SampleGasPricesController', () => {
9484
});
9585

9686
/**
97-
* The union of actions that the root messenger allows.
98-
*/
99-
type RootAction = ExtractAvailableAction<SampleGasPricesControllerMessenger>;
100-
101-
/**
102-
* The union of events that the root messenger allows.
87+
* The union of all SampleGasPricesController actions.
10388
*/
104-
type RootEvent = ExtractAvailableEvent<SampleGasPricesControllerMessenger>;
89+
type AllSampleGasPricesControllerActions =
90+
MessengerActions<SampleGasPricesControllerMessenger>;
10591

10692
/**
107-
* Constructs the unrestricted messenger. This can be used to call actions and
108-
* publish events within the tests for this controller.
109-
*
110-
* @param args - The arguments to this function.
111-
* @param args.networkControllerGetStateActionHandler - Used to mock the
112-
* `NetworkController:getState` action on the messenger.
113-
* @returns The unrestricted messenger suited for SampleGasPricesController.
93+
* The union of all SampleGasPricesController events.
11494
*/
115-
function getRootMessenger({
116-
networkControllerGetStateActionHandler = jest
117-
.fn<
118-
ReturnType<NetworkControllerGetStateAction['handler']>,
119-
Parameters<NetworkControllerGetStateAction['handler']>
120-
>()
121-
.mockReturnValue(getDefaultNetworkControllerState()),
122-
}: {
123-
networkControllerGetStateActionHandler?: NetworkControllerGetStateAction['handler'];
124-
} = {}): Messenger<RootAction, RootEvent> {
125-
const rootMessenger = new Messenger<RootAction, RootEvent>();
126-
rootMessenger.registerActionHandler(
127-
'NetworkController:getState',
128-
networkControllerGetStateActionHandler,
129-
);
130-
return rootMessenger;
131-
}
95+
type AllSampleGasPricesControllerEvents =
96+
MessengerEvents<SampleGasPricesControllerMessenger>;
13297

13398
/**
134-
* Constructs the messenger which is restricted to relevant SampleGasPricesController
135-
* actions and events.
99+
* Constructs the SampleGasPricesController messenger.
136100
*
137-
* @param rootMessenger - The root messenger to restrict.
138-
* @returns The restricted messenger.
101+
* @returns The SampleGasPricesController messenger.
139102
*/
140-
function getMessenger(
141-
rootMessenger = getRootMessenger(),
142-
): SampleGasPricesControllerMessenger {
143-
return rootMessenger.getRestricted({
144-
name: 'SampleGasPricesController',
145-
allowedActions: ['NetworkController:getState'],
146-
allowedEvents: [],
103+
function getMessenger(): SampleGasPricesControllerMessenger {
104+
return new Messenger<
105+
'SampleGasPricesController',
106+
AllSampleGasPricesControllerActions,
107+
AllSampleGasPricesControllerEvents
108+
>({
109+
namespace: 'SampleGasPricesController',
147110
});
148111
}
149112

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)