@metamask-previews/composable-controller 9.0.1-preview-3178c25e → 10.0.0-preview-9cfbf21f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [10.0.0]
11
+
12
+ ### Changed
13
+
14
+ - **BREAKING:** `ComposableController` constructor option `controllers` and generic type argument `ChildControllers` are re-defined from an array of controller instances to an object that maps controller names to controller instances ([#4968](https://github.com/MetaMask/core/pull/4968))
15
+ - **BREAKING:** `ComposableController` class field objects `state` and `metadata` exclude child controllers that do not extend from `BaseController` or `BaseControllerV1`. Any non-controller entries that are passed into the constructor will be removed automatically ([#4968](https://github.com/MetaMask/core/pull/4968))
16
+ - Bump devDependency `@metamask/json-rpc-engine` from `^9.0.3` to `^10.0.1` ([#4798](https://github.com/MetaMask/core/pull/4798), [#4862](https://github.com/MetaMask/core/pull/4862))
17
+
18
+ ### Fixed
19
+
20
+ - **BREAKING:** `ComposableController` class field object `metadata` now assigns the `StateMetadataProperty`-type object `{ persist: true, anonymous: true }` to each child controller name ([#4968](https://github.com/MetaMask/core/pull/4968))
21
+ - Previously, V2 child controllers were erroneously assigned their own metadata object. This issue was introduced in `@metamask/base-controller@6.0.0`.
22
+
10
23
  ## [9.0.1]
11
24
 
12
25
  ### Fixed
@@ -199,7 +212,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
199
212
 
200
213
  All changes listed after this point were applied to this package following the monorepo conversion.
201
214
 
202
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@9.0.1...HEAD
215
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@10.0.0...HEAD
216
+ [10.0.0]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@9.0.1...@metamask/composable-controller@10.0.0
203
217
  [9.0.1]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@9.0.0...@metamask/composable-controller@9.0.1
204
218
  [9.0.0]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@8.0.0...@metamask/composable-controller@9.0.0
205
219
  [8.0.0]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@7.0.0...@metamask/composable-controller@8.0.0
@@ -14,14 +14,14 @@ exports.INVALID_CONTROLLER_ERROR = 'Invalid controller: controller must have a `
14
14
  * Controller that composes multiple child controllers and maintains up-to-date composed state.
15
15
  *
16
16
  * @template ComposableControllerState - A type object containing the names and state types of the child controllers.
17
- * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.
17
+ * @template ChildControllersMap - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.
18
18
  */
19
19
  class ComposableController extends base_controller_1.BaseController {
20
20
  /**
21
21
  * Creates a ComposableController instance.
22
22
  *
23
23
  * @param options - Initial options used to configure this controller
24
- * @param options.controllers - List of child controller instances to compose.
24
+ * @param options.controllers - An object that contains child controllers keyed by their names.
25
25
  * @param options.messenger - A restricted controller messenger.
26
26
  */
27
27
  constructor({ controllers, messenger, }) {
@@ -30,25 +30,39 @@ class ComposableController extends base_controller_1.BaseController {
30
30
  }
31
31
  super({
32
32
  name: exports.controllerName,
33
- metadata: controllers.reduce((metadata, controller) => ({
34
- ...metadata,
35
- [controller.name]: (0, base_controller_1.isBaseController)(controller)
36
- ? controller.metadata
37
- : { persist: true, anonymous: true },
38
- }), {}),
39
- state: controllers.reduce((state, controller) => {
40
- return { ...state, [controller.name]: controller.state };
33
+ // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.
34
+ metadata: Object.keys(controllers).reduce((metadata, name) => {
35
+ metadata[name] = {
36
+ persist: true,
37
+ anonymous: true,
38
+ };
39
+ return metadata;
40
+ }, {}),
41
+ // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.
42
+ state: Object.values(controllers).reduce((state, controller) => {
43
+ // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.
44
+ state[controller.name] =
45
+ controller.state;
46
+ return state;
41
47
  }, {}),
42
48
  messenger,
43
49
  });
44
50
  _ComposableController_instances.add(this);
45
- controllers.forEach((controller) => __classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controller));
51
+ Object.values(controllers).forEach((controller) => {
52
+ __classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controller);
53
+ });
46
54
  }
47
55
  }
48
56
  exports.ComposableController = ComposableController;
49
57
  _ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
50
58
  const { name } = controller;
51
59
  if (!(0, base_controller_1.isBaseController)(controller) && !(0, base_controller_1.isBaseControllerV1)(controller)) {
60
+ try {
61
+ delete this.metadata[name];
62
+ delete this.state[name];
63
+ // eslint-disable-next-line no-empty
64
+ }
65
+ catch (_) { }
52
66
  // False negative. `name` is a string type.
53
67
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
54
68
  throw new Error(`${name} - ${exports.INVALID_CONTROLLER_ERROR}`);
@@ -59,7 +73,8 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
59
73
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
60
74
  `${name}:stateChange`, (childState) => {
61
75
  this.update((state) => {
62
- Object.assign(state, { [name]: childState });
76
+ // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.
77
+ state[name] = childState;
63
78
  });
64
79
  });
65
80
  }
@@ -71,7 +86,8 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
71
86
  if ((0, base_controller_1.isBaseControllerV1)(controller)) {
72
87
  controller.subscribe((childState) => {
73
88
  this.update((state) => {
74
- Object.assign(state, { [name]: childState });
89
+ // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.
90
+ state[name] = childState;
75
91
  });
76
92
  });
77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AC;IAC5B,IAAI,CAAC,IAAA,kCAAgB,EAAC,UAAU,CAAC,IAAI,CAAC,IAAA,oCAAkB,EAAC,UAAU,CAAC,EAAE;QACpE,2CAA2C;QAC3C,4EAA4E;QAC5E,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,MAAM,gCAAwB,EAAE,CAAC,CAAC;KAC1D;IACD,IAAI;QACF,IAAI,CAAC,eAAe,CAAC,SAAS;QAC5B,2CAA2C;QAC3C,4EAA4E;QAC5E,GAAG,IAAI,cAAc,EACrB,CAAC,UAA2C,EAAE,EAAE;YAC9C,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CACF,CAAC;KACH;IAAC,OAAO,KAAc,EAAE;QACvB,2CAA2C;QAC3C,4EAA4E;QAC5E,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,MAAM,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;KAC7C;IACD,IAAI,IAAA,oCAAkB,EAAC,UAAU,CAAC,EAAE;QAClC,UAAU,CAAC,SAAS,CAAC,CAAC,UAA6B,EAAE,EAAE;YACrD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;AAGH,kBAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends ControllerInstance,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - List of child controller instances to compose.\n * @param options.messenger - A restricted controller messenger.\n */\n\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllers[];\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: controllers.reduce<StateMetadata<ComposableControllerState>>(\n (metadata, controller) => ({\n ...metadata,\n [controller.name]: isBaseController(controller)\n ? controller.metadata\n : { persist: true, anonymous: true },\n }),\n {} as never,\n ),\n state: controllers.reduce<ComposableControllerState>(\n (state, controller) => {\n return { ...state, [controller.name]: controller.state };\n },\n {} as never,\n ),\n messenger,\n });\n\n controllers.forEach((controller) =>\n this.#updateChildController(controller),\n );\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n },\n );\n } catch (error: unknown) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n console.error(`${name} - ${String(error)}`);\n }\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState: StateConstraintV1) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
1
+ {"version":3,"file":"ComposableController.cjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;;;;AAUA,+DAImC;AAGtB,QAAA,cAAc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oCAAkB,EAAC,UAAU,CAAC,EAAE;QACpE,IAAI;YACF,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;YAC3B,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACxB,oCAAoC;SACrC;QAAC,OAAO,CAAC,EAAE,GAAE;QACd,2CAA2C;QAC3C,4EAA4E;QAC5E,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,MAAM,gCAAwB,EAAE,CAAC,CAAC;KAC1D;IACD,IAAI;QACF,IAAI,CAAC,eAAe,CAAC,SAAS;QAC5B,2CAA2C;QAC3C,4EAA4E;QAC5E,GAAG,IAAI,cAAc,EACrB,CAAC,UAA2C,EAAE,EAAE;YAC9C,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,2IAA2I;gBAC1I,KAA6C,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;YACpE,CAAC,CAAC,CAAC;QACL,CAAC,CACF,CAAC;KACH;IAAC,OAAO,KAAc,EAAE;QACvB,2CAA2C;QAC3C,4EAA4E;QAC5E,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,MAAM,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;KAC7C;IACD,IAAI,IAAA,oCAAkB,EAAC,UAAU,CAAC,EAAE;QAClC,UAAU,CAAC,SAAS,CAAC,CAAC,UAA6B,EAAE,EAAE;YACrD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,2IAA2I;gBAC1I,KAA6C,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;YACpE,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;AAGH,kBAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n StateMetadataConstraint,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllersMap - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllersMap extends Record<\n keyof ComposableControllerState,\n ControllerInstance\n >,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - An object that contains child controllers keyed by their names.\n * @param options.messenger - A restricted controller messenger.\n */\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllersMap;\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n metadata: Object.keys(controllers).reduce<\n StateMetadata<ComposableControllerState>\n >((metadata, name) => {\n (metadata as StateMetadataConstraint)[name] = {\n persist: true,\n anonymous: true,\n };\n return metadata;\n }, {} as never),\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n (state as ComposableControllerStateConstraint)[controller.name] =\n controller.state;\n return state;\n },\n {} as never,\n ),\n messenger,\n });\n\n Object.values(controllers).forEach((controller) => {\n this.#updateChildController(controller);\n });\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n try {\n delete this.metadata[name];\n delete this.state[name];\n // eslint-disable-next-line no-empty\n } catch (_) {}\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n (state as ComposableControllerStateConstraint)[name] = childState;\n });\n },\n );\n } catch (error: unknown) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n console.error(`${name} - ${String(error)}`);\n }\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState: StateConstraintV1) => {\n this.update((state) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n (state as ComposableControllerStateConstraint)[name] = childState;\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
@@ -63,19 +63,19 @@ export type ComposableControllerMessenger<ComposableControllerState extends Comp
63
63
  * Controller that composes multiple child controllers and maintains up-to-date composed state.
64
64
  *
65
65
  * @template ComposableControllerState - A type object containing the names and state types of the child controllers.
66
- * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.
66
+ * @template ChildControllersMap - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.
67
67
  */
68
- export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends ControllerInstance> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
68
+ export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllersMap extends Record<keyof ComposableControllerState, ControllerInstance>> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
69
69
  #private;
70
70
  /**
71
71
  * Creates a ComposableController instance.
72
72
  *
73
73
  * @param options - Initial options used to configure this controller
74
- * @param options.controllers - List of child controller instances to compose.
74
+ * @param options.controllers - An object that contains child controllers keyed by their names.
75
75
  * @param options.messenger - A restricted controller messenger.
76
76
  */
77
77
  constructor({ controllers, messenger, }: {
78
- controllers: ChildControllers[];
78
+ controllers: ChildControllersMap;
79
79
  messenger: ComposableControllerMessenger<ComposableControllerState>;
80
80
  });
81
81
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ComposableController.d.cts","sourceRoot":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GAE7D,eAAe,SAAS,iBAAiB,GACvC,gCAAgC,CAAC,cAAc,EAAE,eAAe,CAAC,GACjE,KAAK,GACP,KAAK,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,aAAa,CACvB,yBAAyB,SAAS,mCAAmC,IACnE,gCAAgC,CAAC,yBAAyB,CAAC,CAAC;AAEhE;;;;GAIG;AACH,MAAM,MAAM,6BAA6B,CACvC,yBAAyB,SAAS,mCAAmC,IACnE,6BAA6B,CAC/B,OAAO,cAAc,EACrB,KAAK,EACH,0BAA0B,CAAC,yBAAyB,CAAC,GACrD,aAAa,CAAC,yBAAyB,CAAC,EAC1C,KAAK,EACL,aAAa,CAAC,yBAAyB,CAAC,CAAC,MAAM,CAAC,CACjD,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,oBAAoB,CAC/B,yBAAyB,SAAS,yCAAyC,EAC3E,gBAAgB,SAAS,kBAAkB,CAC3C,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBAES,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,gBAAgB,EAAE,CAAC;QAChC,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CAkEF;AAED,eAAe,oBAAoB,CAAC"}
1
+ {"version":3,"file":"ComposableController.d.cts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,EAGjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,OAAO,EACL,cAAc,EAGf,kCAAkC;AACnC,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc;AAEnC,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD,eAAO,MAAM,wBAAwB,mHAC6E,CAAC;AAEnH;;;;;GAKG;AAEH,KAAK,yC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AAyB,CAAC,GACrD,aAAa,CAAC,yBAAyB,CAAC,EAC1C,KAAK,EACL,aAAa,CAAC,yBAAyB,CAAC,CAAC,MAAM,CAAC,CACjD,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,oBAAoB,CAC/B,yBAAyB,SAAS,yCAAyC,EAC3E,mBAAmB,SAAS,MAAM,CAChC,MAAM,yBAAyB,EAC/B,kBAAkB,CACnB,CACD,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBACS,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,mBAAmB,CAAC;QACjC,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CA8EF;AAED,eAAe,oBAAoB,CAAC"}
@@ -63,19 +63,19 @@ export type ComposableControllerMessenger<ComposableControllerState extends Comp
63
63
  * Controller that composes multiple child controllers and maintains up-to-date composed state.
64
64
  *
65
65
  * @template ComposableControllerState - A type object containing the names and state types of the child controllers.
66
- * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.
66
+ * @template ChildControllersMap - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.
67
67
  */
68
- export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends ControllerInstance> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
68
+ export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllersMap extends Record<keyof ComposableControllerState, ControllerInstance>> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
69
69
  #private;
70
70
  /**
71
71
  * Creates a ComposableController instance.
72
72
  *
73
73
  * @param options - Initial options used to configure this controller
74
- * @param options.controllers - List of child controller instances to compose.
74
+ * @param options.controllers - An object that contains child controllers keyed by their names.
75
75
  * @param options.messenger - A restricted controller messenger.
76
76
  */
77
77
  constructor({ controllers, messenger, }: {
78
- controllers: ChildControllers[];
78
+ controllers: ChildControllersMap;
79
79
  messenger: ComposableControllerMessenger<ComposableControllerState>;
80
80
  });
81
81
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ComposableController.d.mts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,EAEjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,OAAO,EACL,cAAc,EAGf,kCAAkC;AACnC,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc;AAEnC,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD,eAAO,MAAM,wBAAwB,mHAC6E,CAAC;AAEnH;;;;;GAKG;AAEH,KAAK,yCAAyC,GAAG;IAI/C,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACrC,CAAC;AAEF;;;GAGG;AAEH,MAAM,MAAM,mCAAmC,GAAG;IAChD,CAAC,IAAI,EAAE,MAAM,GAAG,+BAA+B,CAAC;CACjD,CAAC;AAEF;;GAEG;AAEH,KAAK,gCAAgC,CACnC,cAAc,SAAS,MAAM,EAC7B,eAAe,SAAS,iBAAiB,IACvC;IACF,IAAI,EAAE,GAAG,cAAc,cAAc,CAAC;IACtC,OAAO,EAAE,CAAC,eAAe,EAAE,KAAK,EAAE,CAAC,CAAC;CACrC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,oCAAoC,CAC9C,yBAAyB,SAAS,mCAAmC,IACnE,gCAAgC,CAClC,OAAO,cAAc,EACrB,yBAAyB,CAC1B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,0BAA0B,CACpC,yBAAyB,SAAS,mCAAmC,IACnE,oCAAoC,CAAC,yBAAyB,CAAC,CAAC;AAEpE;;;;;;;GAOG;AACH,MAAM,MAAM,gCAAgC,CAC1C,yBAAyB,SAAS,mCAAmC,IACnE,yBAAyB,SAAS,MAAM,CAC1C,MAAM,cAAc,SAAS,MAAM,EACnC,MAAM,eAAe,CACtB,GACG,eAAe,SAAS,eAAe,GACrC,0BAA0B,CAAC,cAAc,EAAE,eAAe,CAAC,GAE7D,eAAe,SAAS,iBAAiB,GACvC,gCAAgC,CAAC,cAAc,EAAE,eAAe,CAAC,GACjE,KAAK,GACP,KAAK,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,aAAa,CACvB,yBAAyB,SAAS,mCAAmC,IACnE,gCAAgC,CAAC,yBAAyB,CAAC,CAAC;AAEhE;;;;GAIG;AACH,MAAM,MAAM,6BAA6B,CACvC,yBAAyB,SAAS,mCAAmC,IACnE,6BAA6B,CAC/B,OAAO,cAAc,EACrB,KAAK,EACH,0BAA0B,CAAC,yBAAyB,CAAC,GACrD,aAAa,CAAC,yBAAyB,CAAC,EAC1C,KAAK,EACL,aAAa,CAAC,yBAAyB,CAAC,CAAC,MAAM,CAAC,CACjD,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,oBAAoB,CAC/B,yBAAyB,SAAS,yCAAyC,EAC3E,gBAAgB,SAAS,kBAAkB,CAC3C,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBAES,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,gBAAgB,EAAE,CAAC;QAChC,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CAkEF;AAED,eAAe,oBAAoB,CAAC"}
1
+ {"version":3,"file":"ComposableController.d.mts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,EAGjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,O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AAmC,IACnE,gCAAgC,CAAC,yBAAyB,CAAC,CAAC;AAEhE;;;;GAIG;AACH,MAAM,MAAM,6BAA6B,CACvC,yBAAyB,SAAS,mCAAmC,IACnE,6BAA6B,CAC/B,OAAO,cAAc,EACrB,KAAK,EACH,0BAA0B,CAAC,yBAAyB,CAAC,GACrD,aAAa,CAAC,yBAAyB,CAAC,EAC1C,KAAK,EACL,aAAa,CAAC,yBAAyB,CAAC,CAAC,MAAM,CAAC,CACjD,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,oBAAoB,CAC/B,yBAAyB,SAAS,yCAAyC,EAC3E,mBAAmB,SAAS,MAAM,CAChC,MAAM,yBAAyB,EAC/B,kBAAkB,CACnB,CACD,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBACS,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,mBAAmB,CAAC;QACjC,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CA8EF;AAED,eAAe,oBAAoB,CAAC"}
@@ -11,14 +11,14 @@ export const INVALID_CONTROLLER_ERROR = 'Invalid controller: controller must hav
11
11
  * Controller that composes multiple child controllers and maintains up-to-date composed state.
12
12
  *
13
13
  * @template ComposableControllerState - A type object containing the names and state types of the child controllers.
14
- * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.
14
+ * @template ChildControllersMap - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.
15
15
  */
16
16
  export class ComposableController extends BaseController {
17
17
  /**
18
18
  * Creates a ComposableController instance.
19
19
  *
20
20
  * @param options - Initial options used to configure this controller
21
- * @param options.controllers - List of child controller instances to compose.
21
+ * @param options.controllers - An object that contains child controllers keyed by their names.
22
22
  * @param options.messenger - A restricted controller messenger.
23
23
  */
24
24
  constructor({ controllers, messenger, }) {
@@ -27,24 +27,38 @@ export class ComposableController extends BaseController {
27
27
  }
28
28
  super({
29
29
  name: controllerName,
30
- metadata: controllers.reduce((metadata, controller) => ({
31
- ...metadata,
32
- [controller.name]: isBaseController(controller)
33
- ? controller.metadata
34
- : { persist: true, anonymous: true },
35
- }), {}),
36
- state: controllers.reduce((state, controller) => {
37
- return { ...state, [controller.name]: controller.state };
30
+ // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.
31
+ metadata: Object.keys(controllers).reduce((metadata, name) => {
32
+ metadata[name] = {
33
+ persist: true,
34
+ anonymous: true,
35
+ };
36
+ return metadata;
37
+ }, {}),
38
+ // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.
39
+ state: Object.values(controllers).reduce((state, controller) => {
40
+ // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.
41
+ state[controller.name] =
42
+ controller.state;
43
+ return state;
38
44
  }, {}),
39
45
  messenger,
40
46
  });
41
47
  _ComposableController_instances.add(this);
42
- controllers.forEach((controller) => __classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controller));
48
+ Object.values(controllers).forEach((controller) => {
49
+ __classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controller);
50
+ });
43
51
  }
44
52
  }
45
53
  _ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
46
54
  const { name } = controller;
47
55
  if (!isBaseController(controller) && !isBaseControllerV1(controller)) {
56
+ try {
57
+ delete this.metadata[name];
58
+ delete this.state[name];
59
+ // eslint-disable-next-line no-empty
60
+ }
61
+ catch (_) { }
48
62
  // False negative. `name` is a string type.
49
63
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
50
64
  throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);
@@ -55,7 +69,8 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
55
69
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
56
70
  `${name}:stateChange`, (childState) => {
57
71
  this.update((state) => {
58
- Object.assign(state, { [name]: childState });
72
+ // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.
73
+ state[name] = childState;
59
74
  });
60
75
  });
61
76
  }
@@ -67,7 +82,8 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
67
82
  if (isBaseControllerV1(controller)) {
68
83
  controller.subscribe((childState) => {
69
84
  this.update((state) => {
70
- Object.assign(state, { [name]: childState });
85
+ // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.
86
+ state[name] = childState;
71
87
  });
72
88
  });
73
89
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ComposableController.mjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;AASA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EACnB,kCAAkC;AAGnC,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,CAAC,MAAM,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAM,OAAO,oBAGX,SAAQ,cAIT;IACC;;;;;;OAMG;IAEH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QAED,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,WAAW,CAAC,MAAM,CAC1B,CAAC,QAAQ,EAAE,UAAU,EAAE,EAAE,CAAC,CAAC;gBACzB,GAAG,QAAQ;gBACX,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,gBAAgB,CAAC,UAAU,CAAC;oBAC7C,CAAC,CAAC,UAAU,CAAC,QAAQ;oBACrB,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;aACvC,CAAC,EACF,EAAW,CACZ;YAC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E,EAAE;YACrD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;AAGH,eAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends ControllerInstance,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - List of child controller instances to compose.\n * @param options.messenger - A restricted controller messenger.\n */\n\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllers[];\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: controllers.reduce<StateMetadata<ComposableControllerState>>(\n (metadata, controller) => ({\n ...metadata,\n [controller.name]: isBaseController(controller)\n ? controller.metadata\n : { persist: true, anonymous: true },\n }),\n {} as never,\n ),\n state: controllers.reduce<ComposableControllerState>(\n (state, controller) => {\n return { ...state, [controller.name]: controller.state };\n },\n {} as never,\n ),\n messenger,\n });\n\n controllers.forEach((controller) =>\n this.#updateChildController(controller),\n );\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n },\n );\n } catch (error: unknown) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n console.error(`${name} - ${String(error)}`);\n }\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState: StateConstraintV1) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
1
+ {"version":3,"file":"ComposableController.mjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;AAUA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EACnB,kCAAkC;AAGnC,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,CAAC,MAAM,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAM,OAAO,oBAMX,SAAQ,cAIT;IACC;;;;;;OAMG;IACH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QAED,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,+JAA+J;YAC/J,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,CAEvC,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE;gBAClB,QAAoC,CAAC,IAAI,CAAC,GAAG;oBAC5C,OAAO,EAAE,IAAI;oBACb,SAAS,EAAE,IAAI;iBAChB,CAAC;gBACF,OAAO,QAAQ,CAAC;YAClB,CAAC,EAAE,EAAW,CAAC;YACf,+JAA+J;YAC/J,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,MAAM,CACtC,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;gBACpB,2IAA2I;gBA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AC,CAAC;KAC7C;IACD,IAAI,kBAAkB,CAAC,UAAU,CAAC,EAAE;QAClC,UAAU,CAAC,SAAS,CAAC,CAAC,UAA6B,EAAE,EAAE;YACrD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,2IAA2I;gBAC1I,KAA6C,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;YACpE,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;AAGH,eAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n StateMetadataConstraint,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllersMap - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllersMap extends Record<\n keyof ComposableControllerState,\n ControllerInstance\n >,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - An object that contains child controllers keyed by their names.\n * @param options.messenger - A restricted controller messenger.\n */\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllersMap;\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n metadata: Object.keys(controllers).reduce<\n StateMetadata<ComposableControllerState>\n >((metadata, name) => {\n (metadata as StateMetadataConstraint)[name] = {\n persist: true,\n anonymous: true,\n };\n return metadata;\n }, {} as never),\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n (state as ComposableControllerStateConstraint)[controller.name] =\n controller.state;\n return state;\n },\n {} as never,\n ),\n messenger,\n });\n\n Object.values(controllers).forEach((controller) => {\n this.#updateChildController(controller);\n });\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n try {\n delete this.metadata[name];\n delete this.state[name];\n // eslint-disable-next-line no-empty\n } catch (_) {}\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n (state as ComposableControllerStateConstraint)[name] = childState;\n });\n },\n );\n } catch (error: unknown) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n console.error(`${name} - ${String(error)}`);\n }\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState: StateConstraintV1) => {\n this.update((state) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n (state as ComposableControllerStateConstraint)[name] = childState;\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/composable-controller",
3
- "version": "9.0.1-preview-3178c25e",
3
+ "version": "10.0.0-preview-9cfbf21f",
4
4
  "description": "Consolidates the state from multiple controllers into one",
5
5
  "keywords": [
6
6
  "MetaMask",