Skip to content
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

chore(docs): Rename ControllerMessenger to Messenger #5089

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
70 changes: 32 additions & 38 deletions docs/writing-controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ If the recipient controller supports the messaging system, however, the callback

const name = 'FooController';

type FooControllerMessenger = RestrictedControllerMessenger<
type FooControllerMessenger = RestrictedMessenger<
typeof name,
never,
never,
Expand All @@ -247,10 +247,7 @@ class FooController extends BaseController<

// === Client repo ===

const rootMessenger = new ControllerMessenger<
'BarController:stateChange',
never
>();
const rootMessenger = new Messenger<'BarController:stateChange', never>();
const barControllerMessenger = rootMessenger.getRestricted({
name: 'BarController',
});
Expand Down Expand Up @@ -307,7 +304,7 @@ However, this pattern can be replaced with the use of the messenger:

const name = 'FooController';

type FooControllerMessenger = RestrictedControllerMessenger<
type FooControllerMessenger = RestrictedMessenger<
typeof name,
never,
never,
Expand All @@ -331,10 +328,7 @@ class FooController extends BaseController<

// === Client repo ===

const rootMessenger = new ControllerMessenger<
'FooController:someEvent',
never
>();
const rootMessenger = new Messenger<'FooController:someEvent', never>();
const fooControllerMessenger = rootMessenger.getRestricted({
name: 'FooController',
});
Expand Down Expand Up @@ -505,7 +499,7 @@ A controller should define and export a type union that holds all of its actions

The name of this type should be `${ControllerName}Actions`.

This type should be only passed to `RestrictedControllerMessenger` as the 2nd type parameter. It should _not_ be included in its 4th type parameter, as that is is used for external actions.
This type should be only passed to `RestrictedMessenger` as the 2nd type parameter. It should _not_ be included in its 4th type parameter, as that is is used for external actions.

🚫 **`FooController['type']` is passed as the 4th type parameter**

Expand All @@ -514,7 +508,7 @@ export type FooControllerActions =
| FooControllerUpdateCurrencyAction
| FooControllerUpdateRatesAction;

export type FooControllerMessenger = RestrictedControllerMessenger<
export type FooControllerMessenger = RestrictedMessenger<
'FooController',
FooControllerActions,
never,
Expand All @@ -530,7 +524,7 @@ export type FooControllerActions =
| FooControllerUpdateCurrencyAction
| FooControllerUpdateRatesAction;

export type FooControllerMessenger = RestrictedControllerMessenger<
export type FooControllerMessenger = RestrictedMessenger<
'FooController',
FooControllerActions,
never,
Expand All @@ -545,7 +539,7 @@ A controller should define and export a type union that holds all of its events.

The name of this type should be `${ControllerName}Events`.

This type should be only passed to `RestrictedControllerMessenger` as the 3rd type parameter. It should _not_ be included in its 5th type parameter, as that is is used for external events.
This type should be only passed to `RestrictedMessenger` as the 3rd type parameter. It should _not_ be included in its 5th type parameter, as that is is used for external events.

🚫 **`FooControllerEvents['type']` is passed as the 5th type parameter**

Expand All @@ -554,7 +548,7 @@ export type FooControllerEvents =
| FooControllerMessageReceivedEvent
| FooControllerNotificationAddedEvent;

export type FooControllerMessenger = RestrictedControllerMessenger<
export type FooControllerMessenger = RestrictedMessenger<
'FooController',
never,
FooControllerEvents,
Expand All @@ -570,7 +564,7 @@ export type FooControllerEvents =
| FooControllerMessageReceivedEvent
| FooControllerNotificationAddedEvent;

export type FooControllerMessenger = RestrictedControllerMessenger<
export type FooControllerMessenger = RestrictedMessenger<
'FooController',
never,
FooControllerEvents,
Expand All @@ -583,11 +577,11 @@ export type FooControllerMessenger = RestrictedControllerMessenger<

A controller may wish to call actions defined by other controllers, and therefore will need to define them in the messenger's allowlist.

In this case, the controller should group these types into a type union so that they can be easily passed to the `RestrictedControllerMessenger` type. However, it should not export this type, as it would then be re-exporting types that another package has already exported.
In this case, the controller should group these types into a type union so that they can be easily passed to the `RestrictedMessenger` type. However, it should not export this type, as it would then be re-exporting types that another package has already exported.

The name of this type should be `AllowedActions`.

This type should not only be passed to `RestrictedControllerMessenger` as the 2nd type parameter, but should also be included in its 4th type parameter.
This type should not only be passed to `RestrictedMessenger` as the 2nd type parameter, but should also be included in its 4th type parameter.

🚫 **`never` is passed as the 4th type parameter**

Expand All @@ -596,7 +590,7 @@ export type AllowedActions =
| BarControllerDoSomethingAction
| BarControllerDoSomethingElseAction;

export type FooControllerMessenger = RestrictedControllerMessenger<
export type FooControllerMessenger = RestrictedMessenger<
'FooController',
AllowedActions,
never,
Expand All @@ -614,7 +608,7 @@ export type AllowedActions =
| BarControllerDoSomethingAction
| BarControllerDoSomethingElseAction;

export type FooControllerMessenger = RestrictedControllerMessenger<
export type FooControllerMessenger = RestrictedMessenger<
'FooController',
AllowedActions,
never,
Expand All @@ -634,7 +628,7 @@ type AllowedActions =
| BarControllerDoSomethingAction
| BarControllerDoSomethingElseAction;

export type FooControllerMessenger = RestrictedControllerMessenger<
export type FooControllerMessenger = RestrictedMessenger<
'FooController',
AllowedActions,
never,
Expand All @@ -649,7 +643,7 @@ If, in a test, you need to access all of the actions included in a controller's
// NOTE: You may need to adjust the path depending on where you are
import { ExtractAvailableAction } from '../../base-controller/tests/helpers';

const messenger = new ControllerMessenger<
const messenger = new Messenger<
ExtractAvailableAction<FooControllerMessenger>,
never
>();
Expand All @@ -659,11 +653,11 @@ const messenger = new ControllerMessenger<

A controller may wish to subscribe to events defined by other controllers, and therefore will need to define them in the messenger's allowlist.

In this case, the controller should group these types into a type union so that they can be easily passed to the `RestrictedControllerMessenger` type. However, it should not export this type, as it would then be re-exporting types that another package has already exported.
In this case, the controller should group these types into a type union so that they can be easily passed to the `RestrictedMessenger` type. However, it should not export this type, as it would then be re-exporting types that another package has already exported.

The name of this type should be `AllowedEvents`.

This type should not only be passed to `RestrictedControllerMessenger` as the 3rd type parameter, but should also be included in its 5th type parameter.
This type should not only be passed to `RestrictedMessenger` as the 3rd type parameter, but should also be included in its 5th type parameter.

🚫 **`never` is passed as the 5th type parameter**

Expand All @@ -672,7 +666,7 @@ export type AllowedEvents =
| BarControllerSomethingHappenedEvent
| BarControllerSomethingElseHappenedEvent;

export type FooControllerMessenger = RestrictedControllerMessenger<
export type FooControllerMessenger = RestrictedMessenger<
'FooController',
never,
AllowedEvents,
Expand All @@ -690,7 +684,7 @@ export type AllowedEvents =
| BarControllerSomethingHappenedEvent
| BarControllerSomethingElseHappenedEvent;

export type FooControllerMessenger = RestrictedControllerMessenger<
export type FooControllerMessenger = RestrictedMessenger<
'FooController',
never,
AllowedEvents,
Expand All @@ -710,7 +704,7 @@ type AllowedEvents =
| BarControllerSomethingHappenedEvent
| BarControllerSomethingElseHappenedEvent;

export type FooControllerMessenger = RestrictedControllerMessenger<
export type FooControllerMessenger = RestrictedMessenger<
'FooController',
never,
AllowedEvents,
Expand All @@ -725,7 +719,7 @@ If, in a test, you need to access all of the events included in a controller's m
// NOTE: You may need to adjust the path depending on where you are
import { ExtractAvailableEvent } from '../../base-controller/tests/helpers';

const messenger = new ControllerMessenger<
const messenger = new Messenger<
never,
ExtractAvailableEvent<FooControllerMessenger>
>();
Expand Down Expand Up @@ -790,7 +784,7 @@ export type AllowedEvents =
| ApprovalControllerApprovalRequestApprovedEvent
| ApprovalControllerApprovalRequestRejectedEvent;

export type SwapsControllerMessenger = RestrictedControllerMessenger<
export type SwapsControllerMessenger = RestrictedMessenger<
'SwapsController',
SwapsControllerActions | AllowedActions,
SwapsControllerEvents | AllowedEvents,
Expand All @@ -802,7 +796,7 @@ export type SwapsControllerMessenger = RestrictedControllerMessenger<
A messenger that allows no actions or events (whether internal or external) looks like this:

```typescript
export type SwapsControllerMessenger = RestrictedControllerMessenger<
export type SwapsControllerMessenger = RestrictedMessenger<
'SwapsController',
never,
never,
Expand Down Expand Up @@ -946,7 +940,7 @@ class TokensController extends BaseController</* ... */> {
messenger,
}: // ...
{
messenger: TokensControllerMessenger;
messenger: TokensMessenger;
// ...
}) {
// ...
Expand Down Expand Up @@ -1175,7 +1169,7 @@ import { AccountsControllerGetStateAction } from '@metamask/accounts-controller'

type AllowedActions = AccountsControllerGetStateAction;

type PreferencesControllerMessenger = RestrictedControllerMessenger<
type PreferencesControllerMessenger = RestrictedMessenger<
'PreferencesController',
AllowedActions,
never,
Expand Down Expand Up @@ -1273,7 +1267,7 @@ type AccountsControllerActions =
| AccountsControllerGetActiveAccountAction
| AccountsControllerGetInactiveAccountsAction;

export type AccountsControllerMessenger = RestrictedControllerMessenger<
export type AccountsControllerMessenger = RestrictedMessenger<
'AccountsController',
AccountsControllerActions,
never,
Expand Down Expand Up @@ -1302,7 +1296,7 @@ type AllowedActions =
| AccountsControllerGetActiveAccountsAction
| AccountsControllerGetInactiveAccountsAction;

export type TokensControllerMessenger = RestrictedControllerMessenger<
export type TokensMessenger = RestrictedMessenger<
'TokensController',
AllowedActions,
never,
Expand All @@ -1315,7 +1309,7 @@ class TokensController extends BaseController</* ... */> {
messenger,
}: // ...
{
messenger: TokensControllerMessenger;
messenger: TokensMessenger;
// ...
}) {
// ...
Expand Down Expand Up @@ -1383,7 +1377,7 @@ export type AccountsControllerGetStateAction = ControllerGetStateAction<

type AccountsControllerActions = AccountsControllerGetStateAccountAction;

export type AccountsControllerMessenger = RestrictedControllerMessenger<
export type AccountsControllerMessenger = RestrictedMessenger<
'AccountsController',
AccountsControllerActions,
never,
Expand All @@ -1400,7 +1394,7 @@ import { accountsControllerSelectors } from '@metamask/accounts-controller';

type AllowedActions = AccountsControllerGetStateAction;

export type TokensControllerMessenger = RestrictedControllerMessenger<
export type TokensMessenger = RestrictedMessenger<
'TokensController',
AllowedActions,
never,
Expand All @@ -1413,7 +1407,7 @@ class TokensController extends BaseController</* ... */> {
messenger,
// ...
}: {
messenger: TokensControllerMessenger,
messenger: TokensMessenger,
// ...
}) {
// ...
Expand Down
20 changes: 10 additions & 10 deletions examples/example-controllers/src/gas-prices-controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ControllerMessenger } from '@metamask/base-controller';
import { Messenger } from '@metamask/base-controller';
import { GasPricesController } from '@metamask/example-controllers';
import type { GasPricesControllerMessenger } from '@metamask/example-controllers';

Expand Down Expand Up @@ -27,7 +27,7 @@ describe('GasPricesController', () => {
},
};
const controller = new GasPricesController({
messenger: getControllerMessenger(),
messenger: getMessenger(),
state: givenState,
gasPricesService,
});
Expand All @@ -38,7 +38,7 @@ describe('GasPricesController', () => {
it('fills in missing state properties with default values', () => {
const gasPricesService = buildGasPricesService();
const controller = new GasPricesController({
messenger: getControllerMessenger(),
messenger: getMessenger(),
gasPricesService,
});

Expand Down Expand Up @@ -66,14 +66,14 @@ describe('GasPricesController', () => {
average: 10,
high: 15,
});
const rootMessenger = getRootControllerMessenger({
const rootMessenger = getRootMessenger({
networkControllerGetStateActionHandler: () => ({
...getDefaultNetworkControllerState(),
chainId: '0x42',
}),
});
const controller = new GasPricesController({
messenger: getControllerMessenger(rootMessenger),
messenger: getMessenger(rootMessenger),
gasPricesService,
});

Expand Down Expand Up @@ -112,7 +112,7 @@ type RootEvent = ExtractAvailableEvent<GasPricesControllerMessenger>;
* `NetworkController:getState` action on the messenger.
* @returns The unrestricted messenger suited for GasPricesController.
*/
function getRootControllerMessenger({
function getRootMessenger({
networkControllerGetStateActionHandler = jest
.fn<
ReturnType<NetworkControllerGetStateAction['handler']>,
Expand All @@ -121,8 +121,8 @@ function getRootControllerMessenger({
.mockReturnValue(getDefaultNetworkControllerState()),
}: {
networkControllerGetStateActionHandler?: NetworkControllerGetStateAction['handler'];
} = {}): ControllerMessenger<RootAction, RootEvent> {
const rootMessenger = new ControllerMessenger<RootAction, RootEvent>();
} = {}): Messenger<RootAction, RootEvent> {
const rootMessenger = new Messenger<RootAction, RootEvent>();
rootMessenger.registerActionHandler(
'NetworkController:getState',
networkControllerGetStateActionHandler,
Expand All @@ -137,8 +137,8 @@ function getRootControllerMessenger({
* @param rootMessenger - The root messenger to restrict.
* @returns The restricted messenger.
*/
function getControllerMessenger(
rootMessenger = getRootControllerMessenger(),
function getMessenger(
rootMessenger = getRootMessenger(),
): GasPricesControllerMessenger {
return rootMessenger.getRestricted({
name: 'GasPricesController',
Expand Down
8 changes: 4 additions & 4 deletions examples/example-controllers/src/gas-prices-controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
ControllerGetStateAction,
ControllerStateChangeEvent,
RestrictedControllerMessenger,
RestrictedMessenger,
StateMetadata,
} from '@metamask/base-controller';
import { BaseController } from '@metamask/base-controller';
Expand Down Expand Up @@ -121,7 +121,7 @@ type AllowedEvents = never;
* The messenger which is restricted to actions and events accessed by
* {@link GasPricesController}.
*/
export type GasPricesControllerMessenger = RestrictedControllerMessenger<
export type GasPricesControllerMessenger = RestrictedMessenger<
typeof controllerName,
GasPricesControllerActions | AllowedActions,
GasPricesControllerEvents | AllowedEvents,
Expand Down Expand Up @@ -151,7 +151,7 @@ export function getDefaultGasPricesControllerState(): GasPricesControllerState {
* @example
*
* ``` ts
* import { ControllerMessenger } from '@metamask/base-controller';
* import { Messenger } from '@metamask/base-controller';
* import {
* GasPricesController,
* GasPricesService
Expand All @@ -164,7 +164,7 @@ export function getDefaultGasPricesControllerState(): GasPricesControllerState {
*
* // Assuming that you're using this in the browser
* const gasPricesService = new GasPricesService({ fetch });
* const rootMessenger = new ControllerMessenger<
* const rootMessenger = new Messenger<
* GasPricesControllerActions | NetworkControllerGetStateAction,
* GasPricesControllerEvents
* >();
Expand Down
Loading
Loading