diff --git a/docs/writing-controllers.md b/docs/writing-controllers.md index 14be710aa6..387ae4d507 100644 --- a/docs/writing-controllers.md +++ b/docs/writing-controllers.md @@ -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, @@ -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', }); @@ -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, @@ -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', }); @@ -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** @@ -514,7 +508,7 @@ export type FooControllerActions = | FooControllerUpdateCurrencyAction | FooControllerUpdateRatesAction; -export type FooControllerMessenger = RestrictedControllerMessenger< +export type FooControllerMessenger = RestrictedMessenger< 'FooController', FooControllerActions, never, @@ -530,7 +524,7 @@ export type FooControllerActions = | FooControllerUpdateCurrencyAction | FooControllerUpdateRatesAction; -export type FooControllerMessenger = RestrictedControllerMessenger< +export type FooControllerMessenger = RestrictedMessenger< 'FooController', FooControllerActions, never, @@ -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** @@ -554,7 +548,7 @@ export type FooControllerEvents = | FooControllerMessageReceivedEvent | FooControllerNotificationAddedEvent; -export type FooControllerMessenger = RestrictedControllerMessenger< +export type FooControllerMessenger = RestrictedMessenger< 'FooController', never, FooControllerEvents, @@ -570,7 +564,7 @@ export type FooControllerEvents = | FooControllerMessageReceivedEvent | FooControllerNotificationAddedEvent; -export type FooControllerMessenger = RestrictedControllerMessenger< +export type FooControllerMessenger = RestrictedMessenger< 'FooController', never, FooControllerEvents, @@ -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** @@ -596,7 +590,7 @@ export type AllowedActions = | BarControllerDoSomethingAction | BarControllerDoSomethingElseAction; -export type FooControllerMessenger = RestrictedControllerMessenger< +export type FooControllerMessenger = RestrictedMessenger< 'FooController', AllowedActions, never, @@ -614,7 +608,7 @@ export type AllowedActions = | BarControllerDoSomethingAction | BarControllerDoSomethingElseAction; -export type FooControllerMessenger = RestrictedControllerMessenger< +export type FooControllerMessenger = RestrictedMessenger< 'FooController', AllowedActions, never, @@ -634,7 +628,7 @@ type AllowedActions = | BarControllerDoSomethingAction | BarControllerDoSomethingElseAction; -export type FooControllerMessenger = RestrictedControllerMessenger< +export type FooControllerMessenger = RestrictedMessenger< 'FooController', AllowedActions, never, @@ -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, never >(); @@ -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** @@ -672,7 +666,7 @@ export type AllowedEvents = | BarControllerSomethingHappenedEvent | BarControllerSomethingElseHappenedEvent; -export type FooControllerMessenger = RestrictedControllerMessenger< +export type FooControllerMessenger = RestrictedMessenger< 'FooController', never, AllowedEvents, @@ -690,7 +684,7 @@ export type AllowedEvents = | BarControllerSomethingHappenedEvent | BarControllerSomethingElseHappenedEvent; -export type FooControllerMessenger = RestrictedControllerMessenger< +export type FooControllerMessenger = RestrictedMessenger< 'FooController', never, AllowedEvents, @@ -710,7 +704,7 @@ type AllowedEvents = | BarControllerSomethingHappenedEvent | BarControllerSomethingElseHappenedEvent; -export type FooControllerMessenger = RestrictedControllerMessenger< +export type FooControllerMessenger = RestrictedMessenger< 'FooController', never, AllowedEvents, @@ -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 >(); @@ -790,7 +784,7 @@ export type AllowedEvents = | ApprovalControllerApprovalRequestApprovedEvent | ApprovalControllerApprovalRequestRejectedEvent; -export type SwapsControllerMessenger = RestrictedControllerMessenger< +export type SwapsControllerMessenger = RestrictedMessenger< 'SwapsController', SwapsControllerActions | AllowedActions, SwapsControllerEvents | AllowedEvents, @@ -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, @@ -946,7 +940,7 @@ class TokensController extends BaseController { messenger, }: // ... { - messenger: TokensControllerMessenger; + messenger: TokensMessenger; // ... }) { // ... @@ -1175,7 +1169,7 @@ import { AccountsControllerGetStateAction } from '@metamask/accounts-controller' type AllowedActions = AccountsControllerGetStateAction; -type PreferencesControllerMessenger = RestrictedControllerMessenger< +type PreferencesControllerMessenger = RestrictedMessenger< 'PreferencesController', AllowedActions, never, @@ -1273,7 +1267,7 @@ type AccountsControllerActions = | AccountsControllerGetActiveAccountAction | AccountsControllerGetInactiveAccountsAction; -export type AccountsControllerMessenger = RestrictedControllerMessenger< +export type AccountsControllerMessenger = RestrictedMessenger< 'AccountsController', AccountsControllerActions, never, @@ -1302,7 +1296,7 @@ type AllowedActions = | AccountsControllerGetActiveAccountsAction | AccountsControllerGetInactiveAccountsAction; -export type TokensControllerMessenger = RestrictedControllerMessenger< +export type TokensMessenger = RestrictedMessenger< 'TokensController', AllowedActions, never, @@ -1315,7 +1309,7 @@ class TokensController extends BaseController { messenger, }: // ... { - messenger: TokensControllerMessenger; + messenger: TokensMessenger; // ... }) { // ... @@ -1383,7 +1377,7 @@ export type AccountsControllerGetStateAction = ControllerGetStateAction< type AccountsControllerActions = AccountsControllerGetStateAccountAction; -export type AccountsControllerMessenger = RestrictedControllerMessenger< +export type AccountsControllerMessenger = RestrictedMessenger< 'AccountsController', AccountsControllerActions, never, @@ -1400,7 +1394,7 @@ import { accountsControllerSelectors } from '@metamask/accounts-controller'; type AllowedActions = AccountsControllerGetStateAction; -export type TokensControllerMessenger = RestrictedControllerMessenger< +export type TokensMessenger = RestrictedMessenger< 'TokensController', AllowedActions, never, @@ -1413,7 +1407,7 @@ class TokensController extends BaseController { messenger, // ... }: { - messenger: TokensControllerMessenger, + messenger: TokensMessenger, // ... }) { // ... diff --git a/examples/example-controllers/src/gas-prices-controller.test.ts b/examples/example-controllers/src/gas-prices-controller.test.ts index 93a5efc22c..b6ed3967a9 100644 --- a/examples/example-controllers/src/gas-prices-controller.test.ts +++ b/examples/example-controllers/src/gas-prices-controller.test.ts @@ -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'; @@ -27,7 +27,7 @@ describe('GasPricesController', () => { }, }; const controller = new GasPricesController({ - messenger: getControllerMessenger(), + messenger: getMessenger(), state: givenState, gasPricesService, }); @@ -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, }); @@ -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, }); @@ -112,7 +112,7 @@ type RootEvent = ExtractAvailableEvent; * `NetworkController:getState` action on the messenger. * @returns The unrestricted messenger suited for GasPricesController. */ -function getRootControllerMessenger({ +function getRootMessenger({ networkControllerGetStateActionHandler = jest .fn< ReturnType, @@ -121,8 +121,8 @@ function getRootControllerMessenger({ .mockReturnValue(getDefaultNetworkControllerState()), }: { networkControllerGetStateActionHandler?: NetworkControllerGetStateAction['handler']; -} = {}): ControllerMessenger { - const rootMessenger = new ControllerMessenger(); +} = {}): Messenger { + const rootMessenger = new Messenger(); rootMessenger.registerActionHandler( 'NetworkController:getState', networkControllerGetStateActionHandler, @@ -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', diff --git a/examples/example-controllers/src/gas-prices-controller.ts b/examples/example-controllers/src/gas-prices-controller.ts index ecfc37a4dc..5d2fb50930 100644 --- a/examples/example-controllers/src/gas-prices-controller.ts +++ b/examples/example-controllers/src/gas-prices-controller.ts @@ -1,7 +1,7 @@ import type { ControllerGetStateAction, ControllerStateChangeEvent, - RestrictedControllerMessenger, + RestrictedMessenger, StateMetadata, } from '@metamask/base-controller'; import { BaseController } from '@metamask/base-controller'; @@ -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, @@ -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 @@ -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 * >(); diff --git a/examples/example-controllers/src/pet-names-controller.test.ts b/examples/example-controllers/src/pet-names-controller.test.ts index 8d92282553..9369dde88b 100644 --- a/examples/example-controllers/src/pet-names-controller.test.ts +++ b/examples/example-controllers/src/pet-names-controller.test.ts @@ -1,4 +1,4 @@ -import { ControllerMessenger } from '@metamask/base-controller'; +import { Messenger } from '@metamask/base-controller'; import type { ExtractAvailableAction, @@ -20,7 +20,7 @@ describe('PetNamesController', () => { }, }; const controller = new PetNamesController({ - messenger: getControllerMessenger(), + messenger: getMessenger(), state: givenState, }); @@ -29,7 +29,7 @@ describe('PetNamesController', () => { it('fills in missing state properties with default values', () => { const controller = new PetNamesController({ - messenger: getControllerMessenger(), + messenger: getMessenger(), }); expect(controller.state).toMatchInlineSnapshot(` @@ -44,7 +44,7 @@ describe('PetNamesController', () => { for (const blockedKey of PROTOTYPE_POLLUTION_BLOCKLIST) { it(`throws if given a chainId of "${blockedKey}"`, () => { const controller = new PetNamesController({ - messenger: getControllerMessenger(), + messenger: getMessenger(), }); expect(() => @@ -56,7 +56,7 @@ describe('PetNamesController', () => { it('registers the given pet name in state with the given chain ID and address', () => { const controller = new PetNamesController({ - messenger: getControllerMessenger(), + messenger: getMessenger(), state: { namesByChainIdAndAddress: { '0x1': { @@ -80,7 +80,7 @@ describe('PetNamesController', () => { it("creates a new group for the chain if it doesn't already exist", () => { const controller = new PetNamesController({ - messenger: getControllerMessenger(), + messenger: getMessenger(), }); controller.assignPetName('0x1', '0xaaaaaa', 'My Account'); @@ -96,7 +96,7 @@ describe('PetNamesController', () => { it('overwrites any existing pet name for the address', () => { const controller = new PetNamesController({ - messenger: getControllerMessenger(), + messenger: getMessenger(), state: { namesByChainIdAndAddress: { '0x1': { @@ -119,7 +119,7 @@ describe('PetNamesController', () => { it('lowercases the given address before registering it to avoid duplicate entries', () => { const controller = new PetNamesController({ - messenger: getControllerMessenger(), + messenger: getMessenger(), state: { namesByChainIdAndAddress: { '0x1': { @@ -158,11 +158,8 @@ type RootEvent = ExtractAvailableEvent; * * @returns The unrestricted messenger suited for PetNamesController. */ -function getRootControllerMessenger(): ControllerMessenger< - RootAction, - RootEvent -> { - return new ControllerMessenger(); +function getRootMessenger(): Messenger { + return new Messenger(); } /** @@ -172,8 +169,8 @@ function getRootControllerMessenger(): ControllerMessenger< * @param rootMessenger - The root messenger to restrict. * @returns The restricted messenger. */ -function getControllerMessenger( - rootMessenger = getRootControllerMessenger(), +function getMessenger( + rootMessenger = getRootMessenger(), ): PetNamesControllerMessenger { return rootMessenger.getRestricted({ name: 'PetNamesController', diff --git a/examples/example-controllers/src/pet-names-controller.ts b/examples/example-controllers/src/pet-names-controller.ts index cece05f6ec..5997b8ac2f 100644 --- a/examples/example-controllers/src/pet-names-controller.ts +++ b/examples/example-controllers/src/pet-names-controller.ts @@ -1,7 +1,7 @@ import type { ControllerGetStateAction, ControllerStateChangeEvent, - RestrictedControllerMessenger, + RestrictedMessenger, StateMetadata, } from '@metamask/base-controller'; import { BaseController } from '@metamask/base-controller'; @@ -89,7 +89,7 @@ type AllowedEvents = never; * The messenger which is restricted to actions and events accessed by * {@link PetNamesController}. */ -export type PetNamesControllerMessenger = RestrictedControllerMessenger< +export type PetNamesControllerMessenger = RestrictedMessenger< typeof controllerName, PetNamesControllerActions | AllowedActions, PetNamesControllerEvents | AllowedEvents, @@ -120,13 +120,13 @@ export function getDefaultPetNamesControllerState(): PetNamesControllerState { * @example * * ``` ts - * import { ControllerMessenger } from '@metamask/base-controller'; + * import { Messenger } from '@metamask/base-controller'; * import type { * PetNamesControllerActions, * PetNamesControllerEvents * } from '@metamask/example-controllers'; * - * const rootMessenger = new ControllerMessenger< + * const rootMessenger = new Messenger< * PetNamesControllerActions, * PetNamesControllerEvents * >();