You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Each controller needs a default representation in order to fully initialize itself when [receiving a partial representation of state](#accept-an-optional-partial-representation-of-state). A default representation of state is also useful when testing interactions with a controller's `*:stateChange` event.
67
+
Each controller needs a default representation in order to fully initialize itself when [receiving a partial representation of state](#accept-an-optional-partial-representation-of-state). A default representation of state is also useful when testing interactions with a controller's `*:stateChanged` event.
68
68
69
69
A function which returns this default representation should be defined and exported. It should be called `getDefault${ControllerName}State`.
70
70
@@ -226,7 +226,7 @@ const fooController = new FooController({
226
226
227
227
If the recipient controller uses a messenger, however, the callback pattern is unnecessary. Using the messenger not only aligns the controller with `BaseController`, but also reduces the number of options that consumers need to remember in order to use the controller:
228
228
229
-
✅ **The constructor subscribes to the `BarController:stateChange` event**
229
+
✅ **The constructor subscribes to the `BarController:stateChanged` event**
230
230
231
231
```typescript
232
232
/* === This repo: packages/foo-controller/src/FooController.ts === */
@@ -247,7 +247,7 @@ class FooController extends BaseController<
3. Remove messenger action types; instead, run `yarn generate-method-action-types`. This will create a file called `${ControllerName}-method-action-types.ts`, which exports a type called `${ControllerName}MethodActions`.
460
+
3. Remove messenger action types; instead, run `yarn messenger-action-types:generate`. This will create a file called `${ControllerName}-method-action-types.ts`, which exports a type called `${ControllerName}MethodActions`.
461
461
4. Import `${ControllerName}-method-action-types.ts` in your controller file, and add `${ControllerName}MethodActions` to `${ControllerName}Actions`.
462
462
5. Export the action types from `${ControllerName}-method-action-types.ts` in your package's `index.ts` file. Do **not** export the `${ControllerName}MethodActions` type.
463
463
@@ -541,16 +541,16 @@ type FooControllerGetStateAction = ControllerGetStateAction<
541
541
>;
542
542
```
543
543
544
-
## Define the `*:stateChange` event using the `ControllerStateChangeEvent` utility type
544
+
## Define the `*:stateChanged` event using the `ControllerStateChangedEvent` utility type
545
545
546
-
Each controller needs a type for its `*:stateChange` event. The `ControllerStateChangeEvent` utility type from the `@metamask/base-controller` package should be used to define this type.
546
+
Each controller needs a type for its `*:stateChanged` event. The `ControllerStateChangedEvent` utility type from the `@metamask/base-controller` package should be used to define this type.
547
547
548
-
The name of this type should be `${ControllerName}StateChangeEvent`.
548
+
The name of this type should be `${ControllerName}StateChangedEvent`.
## Do not call messenger actions in the constructor
958
+
959
+
One of the responsibilities of the messenger is to act as a liaison between controllers and services. A controller should not require direct access to another controller or service in order to communicate with it. This decouples controllers and services from each other and allows clients to initialize controllers and services in any order.
960
+
961
+
Calling `this.messenger.call(...)` inside a controller's constructor, however, prevents this goal from being achieved. The action's handler must already be registered by the time the constructor runs, which means the controller that owns that handler must have been instantiated first.
962
+
963
+
Instead of accessing actions in controller constructors, move any calls to an `init()` method (which clients are free to call after instantiating all controllers and services).
964
+
965
+
🚫 **A messenger action is called during construction**
One way to fix this is to check if the other controller (the one being subscribed to) has a more suitable, granular event for the data being acted upon. For instance, `NetworkController` has a `networkDidChange` event which can be used in place of `NetworkController:stateChange` if the subscribing controller needs to know when the network has been switched:
1102
+
One way to fix this is to check if the other controller (the one being subscribed to) has a more suitable, granular event for the data being acted upon. For instance, `NetworkController` has a `networkDidChange` event which can be used in place of `NetworkController:stateChanged` if the subscribing controller needs to know when the network has been switched:
1058
1103
1059
-
✅ **`NetworkController:networkDidChange` is used instead of `NetworkController:stateChange`**
1104
+
✅ **`NetworkController:networkDidChange` is used instead of `NetworkController:stateChanged`**
0 commit comments