@metamask-previews/composable-controller 9.0.1-preview-50bb1e2c → 9.0.1-preview-2b9717c

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,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Changed
11
+
12
+ - **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.
13
+ - **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.
14
+
15
+ ## Fixed
16
+
17
+ - **BREAKING:** `ComposableController` class field object `metadata` now assigns the `StateMetadataProperty`-type object `{ persist: true, anonymous: true }` to each child controller name.
18
+ - Previously, V2 child controllers were erroneously assigned their own metadata object. This issue was introduced in `@metamask/base-controller@6.0.0`.
19
+
10
20
  ## [9.0.1]
11
21
 
12
22
  ### Fixed
@@ -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 ChildControllers - 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,36 @@ 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
+ metadata: Object.keys(controllers).reduce((metadata, name) => {
34
+ metadata[name] = {
35
+ persist: true,
36
+ anonymous: true,
37
+ };
38
+ return metadata;
39
+ }, {}),
40
+ state: Object.values(controllers).reduce((state, controller) => {
41
+ state[controller.name] =
42
+ controller.state;
43
+ return state;
41
44
  }, {}),
42
45
  messenger,
43
46
  });
44
47
  _ComposableController_instances.add(this);
45
- 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
+ });
46
51
  }
47
52
  }
48
53
  exports.ComposableController = ComposableController;
49
54
  _ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
50
55
  const { name } = controller;
51
56
  if (!(0, base_controller_1.isBaseController)(controller) && !(0, base_controller_1.isBaseControllerV1)(controller)) {
57
+ if (name in this.metadata && this.metadata[name]) {
58
+ delete this.metadata[name];
59
+ }
60
+ if (name in this.state && this.state[name]) {
61
+ delete this.state[name];
62
+ }
52
63
  // False negative. `name` is a string type.
53
64
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
54
65
  throw new Error(`${name} - ${exports.INVALID_CONTROLLER_ERROR}`);
@@ -59,7 +70,7 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
59
70
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
60
71
  `${name}:stateChange`, (childState) => {
61
72
  this.update((state) => {
62
- Object.assign(state, { [name]: childState });
73
+ state[name] = childState;
63
74
  });
64
75
  });
65
76
  }
@@ -71,7 +82,7 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
71
82
  if ((0, base_controller_1.isBaseControllerV1)(controller)) {
72
83
  controller.subscribe((childState) => {
73
84
  this.update((state) => {
74
- Object.assign(state, { [name]: childState });
85
+ state[name] = childState;
75
86
  });
76
87
  });
77
88
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ComposableController.cjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;;;;AASA,+DAImC;AAGtB,QAAA,cAAc,GAAG,sBAAsB,CAAC;AAExC,QAAA,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAa,oBAGX,SAAQ,gCAIT;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,sBAAc;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,IAAA,kCAAgB,EAAC,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;YACD,KAAK,EAAE,WAAW,CAAC,MAAM,CACvB,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;gBACpB,OAAO,EAAE,GAAG,KAAK,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,UAAU,CAAC,KAAK,EAAE,CAAC;YAC3D,CAAC,EACD,EAAW,CACZ;YACD,SAAS;SACV,CAAC,CAAC;;QAEH,WAAW,CAAC,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AC;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,GAAG,sBAAsB,CAAC;AAExC,QAAA,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAa,oBAMX,SAAQ,gCAIT;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,sBAAc;YACpB,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,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,MAAM,CACtC,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;gBACnB,KAA6C,CAAC,UAAU,CAAC,IAAI,CAAC;oBAC7D,UAAU,CAAC,KAAK,CAAC;gBACnB,OAAO,KAAK,CAAC;YACf,CAAC,EACD,EAAW,CACZ;YACD,SAAS;SACV,CAAC,CAAC;;QAEH,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;YAChD,uBAAA,IAAI,oFAAuB,MAA3B,IAAI,EAAwB,UAAU,CAAC,CAAC;QAC1C,CAAC,CAAC,CAAC;IACL,CAAC;C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,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 ChildControllers - 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 ChildControllers 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: 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: 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 state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\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 if (name in this.metadata && this.metadata[name]) {\n delete this.metadata[name];\n }\n if (name in this.state && this.state[name]) {\n delete this.state[name];\n }\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 (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 (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 ChildControllers - 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, ChildControllers 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: ChildControllers;
79
79
  messenger: ComposableControllerMessenger<ComposableControllerState>;
80
80
  });
81
81
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ComposableController.d.cts","sourceR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AC,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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,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,MAAM,CAC7B,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,gBAAgB,CAAC;QAC9B,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CA0EF;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 ChildControllers - 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, ChildControllers 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: ChildControllers;
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,OA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AmC,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,MAAM,CAC7B,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,gBAAgB,CAAC;QAC9B,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CA0EF;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 ChildControllers - 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,35 @@ 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
+ metadata: Object.keys(controllers).reduce((metadata, name) => {
31
+ metadata[name] = {
32
+ persist: true,
33
+ anonymous: true,
34
+ };
35
+ return metadata;
36
+ }, {}),
37
+ state: Object.values(controllers).reduce((state, controller) => {
38
+ state[controller.name] =
39
+ controller.state;
40
+ return state;
38
41
  }, {}),
39
42
  messenger,
40
43
  });
41
44
  _ComposableController_instances.add(this);
42
- controllers.forEach((controller) => __classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controller));
45
+ Object.values(controllers).forEach((controller) => {
46
+ __classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controller);
47
+ });
43
48
  }
44
49
  }
45
50
  _ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
46
51
  const { name } = controller;
47
52
  if (!isBaseController(controller) && !isBaseControllerV1(controller)) {
53
+ if (name in this.metadata && this.metadata[name]) {
54
+ delete this.metadata[name];
55
+ }
56
+ if (name in this.state && this.state[name]) {
57
+ delete this.state[name];
58
+ }
48
59
  // False negative. `name` is a string type.
49
60
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
50
61
  throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);
@@ -55,7 +66,7 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
55
66
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
56
67
  `${name}:stateChange`, (childState) => {
57
68
  this.update((state) => {
58
- Object.assign(state, { [name]: childState });
69
+ state[name] = childState;
59
70
  });
60
71
  });
61
72
  }
@@ -67,7 +78,7 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
67
78
  if (isBaseControllerV1(controller)) {
68
79
  controller.subscribe((childState) => {
69
80
  this.update((state) => {
70
- Object.assign(state, { [name]: childState });
81
+ state[name] = childState;
71
82
  });
72
83
  });
73
84
  }
@@ -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;YACD,KAAK,EAAE,WAAW,CAAC,MAAM,CACvB,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;gBACpB,OAAO,EAAE,GAAG,KAAK,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,UAAU,CAAC,KAAK,EAAE,CAAC;YAC3D,CAAC,EACD,EAAW,CACZ;YACD,SAAS;SACV,CAAC,CAAC;;QAEH,WAAW,CAAC,OAA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":["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,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,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,MAAM,CACtC,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;gBACnB,KAA6C,CAAC,UAAU,CAAC,IAAI,CAAC;oBAC7D,UAAU,CAAC,KAAK,CAAC;gBACnB,OAAO,KAAK,CAAC;YACf,CAAC,EACD,EAAW,CACZ;YACD,SAAS;SACV,CAAC,CAAC;;QAEH,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;YAChD,uBAAA,IAAI,oFAAuB,MAA3B,IAAI,EAAwB,UAAU,CAAC,CAAC;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AAI,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 ChildControllers - 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 ChildControllers 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: 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: 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 state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\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 if (name in this.metadata && this.metadata[name]) {\n delete this.metadata[name];\n }\n if (name in this.state && this.state[name]) {\n delete this.state[name];\n }\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 (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 (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-50bb1e2c",
3
+ "version": "9.0.1-preview-2b9717c",
4
4
  "description": "Consolidates the state from multiple controllers into one",
5
5
  "keywords": [
6
6
  "MetaMask",