@metamask-previews/composable-controller 9.0.1-preview-aefb09ff → 9.0.1-preview-ebca6967

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.
@@ -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 type object containing child controllers that are 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 - An object that maps child controller names to corresponding controller instances.
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, }) {
@@ -34,7 +34,7 @@ class ComposableController extends base_controller_1.BaseController {
34
34
  // Type assertion is necessary to assign new properties to a generic type - ts(2862)
35
35
  metadata[name] = {
36
36
  persist: true,
37
- anonymous: true,
37
+ anonymous: false,
38
38
  };
39
39
  return metadata;
40
40
  }, {}),
@@ -56,8 +56,12 @@ exports.ComposableController = ComposableController;
56
56
  _ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
57
57
  const { name } = controller;
58
58
  if (!(0, base_controller_1.isBaseController)(controller) && !(0, base_controller_1.isBaseControllerV1)(controller)) {
59
- delete this.metadata[name];
60
- delete this.state[name];
59
+ if (name in this.metadata && this.metadata[name]) {
60
+ delete this.metadata[name];
61
+ }
62
+ if (name in this.state && this.state[name]) {
63
+ delete this.state[name];
64
+ }
61
65
  // False negative. `name` is a string type.
62
66
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
63
67
  throw new Error(`${name} - ${exports.INVALID_CONTROLLER_ERROR}`);
@@ -1 +1 @@
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,oBAGX,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;gBACnB,oFAAoF;gBACnF,QAA4D,CAAC,IAAI,CAAC,GAAG;oBACpE,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;gBACpB,oFAAoF;gBACpF,yGAAyG;gBACxG,KAAmD,CAClD,UAAU,CAAC,IAAI,CAChB,GAAG,UAAU,CAAC,KAAK,CAAC;gBACrB,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;CA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etadataConstraint,\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 containing child controllers that are being used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends Record<string, 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 - An object that maps child controller names to corresponding controller instances.\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 // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n (metadata as Record<string, StatePropertyMetadataConstraint>)[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 // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n // TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.\n (state as LegacyComposableControllerStateConstraint)[\n 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 delete this.metadata[name];\n delete this.state[name];\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 LegacyComposableControllerStateConstraint)[name] =\n 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 Record<string, StateConstraintV1>)[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,oBAGX,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;gBACnB,oFAAoF;gBACnF,QAA4D,CAAC,IAAI,CAAC,GAAG;oBACpE,OAAO,EAAE,IAAI;oBACb,SAAS,EAAE,KAAK;iBACjB,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;gBACpB,oFAAoF;gBACpF,yGAAyG;gBACxG,KAAmD,CAClD,UAAU,CAAC,IAAI,CAChB,GAAG,UAAU,CAAC,KAAK,CAAC;gBACrB,OAAO,KAAK,CAAC;YACf,CAAC,EACD,EAAW,CACZ;YACD,SAAS;SACV,CAAC,CAA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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;gBACnB,KAA2C,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;YAClE,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 StatePropertyMetadataConstraint,\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<string, 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 - 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 // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n (metadata as Record<string, StatePropertyMetadataConstraint>)[name] = {\n persist: true,\n anonymous: false,\n };\n return metadata;\n }, {} as never),\n state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\n // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n // TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.\n (state as LegacyComposableControllerStateConstraint)[\n 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 LegacyComposableControllerStateConstraint)[name] =\n 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 Record<string, StateConstraintV1>)[name] = childState;\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
@@ -63,7 +63,7 @@ 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 type object containing child controllers that are 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
68
  export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends Record<string, ControllerInstance>> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
69
69
  #private;
@@ -71,7 +71,7 @@ export declare class ComposableController<ComposableControllerState extends Lega
71
71
  * Creates a ComposableController instance.
72
72
  *
73
73
  * @param options - Initial options used to configure this controller
74
- * @param options.controllers - An object that maps child controller names to corresponding controller instances.
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, }: {
@@ -1 +1 @@
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,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,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAC3D,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;CA2EF;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,cAA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CAAgC,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,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAC3D,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;CA+EF;AAED,eAAe,oBAAoB,CAAC"}
@@ -63,7 +63,7 @@ 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 type object containing child controllers that are 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
68
  export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends Record<string, ControllerInstance>> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
69
69
  #private;
@@ -71,7 +71,7 @@ export declare class ComposableController<ComposableControllerState extends Lega
71
71
  * Creates a ComposableController instance.
72
72
  *
73
73
  * @param options - Initial options used to configure this controller
74
- * @param options.controllers - An object that maps child controller names to corresponding controller instances.
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, }: {
@@ -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,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,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,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAC3D,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;CA2EF;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;AAC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S,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,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAC3D,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;CA+EF;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 type object containing child controllers that are 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 - An object that maps child controller names to corresponding controller instances.
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, }) {
@@ -31,7 +31,7 @@ export class ComposableController extends BaseController {
31
31
  // Type assertion is necessary to assign new properties to a generic type - ts(2862)
32
32
  metadata[name] = {
33
33
  persist: true,
34
- anonymous: true,
34
+ anonymous: false,
35
35
  };
36
36
  return metadata;
37
37
  }, {}),
@@ -52,8 +52,12 @@ export class ComposableController extends BaseController {
52
52
  _ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
53
53
  const { name } = controller;
54
54
  if (!isBaseController(controller) && !isBaseControllerV1(controller)) {
55
- delete this.metadata[name];
56
- delete this.state[name];
55
+ if (name in this.metadata && this.metadata[name]) {
56
+ delete this.metadata[name];
57
+ }
58
+ if (name in this.state && this.state[name]) {
59
+ delete this.state[name];
60
+ }
57
61
  // False negative. `name` is a string type.
58
62
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
59
63
  throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);
@@ -1 +1 @@
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,oBAGX,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;gBACnB,oFAAoF;gBACnF,QAA4D,CAAC,IAAI,CAAC,GAAG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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,kBAAkB,CAAC,UAAU,CAAC,EAAE;QAClC,UAAU,CAAC,SAAS,CAAC,CAAC,UAA6B,EAAE,EAAE;YACrD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACnB,KAA2C,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;YAClE,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 StatePropertyMetadataConstraint,\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 containing child controllers that are being used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends Record<string, 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 - An object that maps child controller names to corresponding controller instances.\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 // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n (metadata as Record<string, StatePropertyMetadataConstraint>)[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 // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n // TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.\n (state as LegacyComposableControllerStateConstraint)[\n 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 delete this.metadata[name];\n delete this.state[name];\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 LegacyComposableControllerStateConstraint)[name] =\n 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 Record<string, StateConstraintV1>)[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,oBAGX,SAAQ,cAIT;IACC;;;;;;OAMG;IACH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC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C,EAAE;YAC1C,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;SACzB;QACD,2CAA2C;QAC3C,4EAA4E;QAC5E,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,MAAM,wBAAwB,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;gBACnB,KAAmD,CAAC,IAAI,CAAC;oBACxD,UAAU,CAAC;YACf,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,kBAAkB,CAAC,UAAU,CAAC,EAAE;QAClC,UAAU,CAAC,SAAS,CAAC,CAAC,UAA6B,EAAE,EAAE;YACrD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACnB,KAA2C,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;YAClE,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 StatePropertyMetadataConstraint,\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<string, 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 - 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 // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n (metadata as Record<string, StatePropertyMetadataConstraint>)[name] = {\n persist: true,\n anonymous: false,\n };\n return metadata;\n }, {} as never),\n state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\n // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n // TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.\n (state as LegacyComposableControllerStateConstraint)[\n 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 LegacyComposableControllerStateConstraint)[name] =\n 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 Record<string, StateConstraintV1>)[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-aefb09ff",
3
+ "version": "9.0.1-preview-ebca6967",
4
4
  "description": "Consolidates the state from multiple controllers into one",
5
5
  "keywords": [
6
6
  "MetaMask",