@metamask-previews/composable-controller 11.0.0-preview-ddceb3a8 → 11.0.0-preview-d79fc0b7

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
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Changed
11
11
 
12
- - Bump `@metamask/base-controller` from `^8.0.0` to `^8.2.0` ([#5722](https://github.com/MetaMask/core/pull/5722), [#6284](https://github.com/MetaMask/core/pull/6284), [#6355](https://github.com/MetaMask/core/pull/6355))
12
+ - Bump `@metamask/base-controller` from `^8.0.0` to `^8.1.0` ([#5722](https://github.com/MetaMask/core/pull/5722), [#6284](https://github.com/MetaMask/core/pull/6284))
13
13
 
14
14
  ## [11.0.0]
15
15
 
@@ -56,7 +56,7 @@ class ComposableController extends base_controller_1.BaseController {
56
56
  exports.ComposableController = ComposableController;
57
57
  _ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
58
58
  const { name } = controller;
59
- if (!isBaseController(controller)) {
59
+ if (!(0, base_controller_1.isBaseController)(controller)) {
60
60
  try {
61
61
  delete this.metadata[name];
62
62
  delete this.state[name];
@@ -85,21 +85,5 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
85
85
  console.error(`${name} - ${String(error)}`);
86
86
  }
87
87
  };
88
- /**
89
- * Determines if the given controller is an instance of `BaseController`
90
- *
91
- * @param controller - Controller instance to check
92
- * @returns True if the controller is an instance of `BaseController`
93
- */
94
- function isBaseController(controller) {
95
- return (typeof controller === 'object' &&
96
- controller !== null &&
97
- 'name' in controller &&
98
- typeof controller.name === 'string' &&
99
- 'state' in controller &&
100
- typeof controller.state === 'object' &&
101
- 'metadata' in controller &&
102
- typeof controller.metadata === 'object');
103
- }
104
88
  exports.default = ComposableController;
105
89
  //# sourceMappingURL=ComposableController.cjs.map
@@ -1 +1 @@
1
- {"version":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ourcesContent":["import type {\n RestrictedMessenger,\n StateConstraint,\n StateMetadata,\n StateMetadataConstraint,\n ControllerStateChangeEvent,\n BaseControllerInstance as ControllerInstance,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` and inherit from `BaseController`.';\n\n/**\n * The narrowest supertype for the composable controller state object.\n */\nexport type ComposableControllerStateConstraint = {\n [controllerName: string]: StateConstraint;\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> = ControllerStateChangeEvent<\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 * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> =\n ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n >\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<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> = RestrictedMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllersMap - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends ComposableControllerStateConstraint,\n ChildControllersMap extends Record<\n keyof ComposableControllerState,\n ControllerInstance\n >,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - An object that contains child controllers keyed by their names.\n * @param options.messenger - A restricted messenger.\n */\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllersMap;\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n metadata: Object.keys(controllers).reduce<\n StateMetadata<ComposableControllerState>\n >((metadata, name) => {\n (metadata as StateMetadataConstraint)[name] = {\n persist: true,\n anonymous: true,\n };\n return metadata;\n }, {} as never),\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n (state as ComposableControllerStateConstraint)[controller.name] =\n controller.state;\n return state;\n },\n {} as never,\n ),\n messenger,\n });\n\n Object.values(controllers).forEach((controller) => {\n this.#updateChildController(controller);\n });\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller)) {\n try {\n delete this.metadata[name];\n delete this.state[name];\n // eslint-disable-next-line no-empty\n } catch (_) {}\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: StateConstraint) => {\n this.update((state) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n // @ts-expect-error \"Type instantiation is excessively deep\"\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 }\n}\n\n/**\n * Determines if the given controller is an instance of `BaseController`\n *\n * @param controller - Controller instance to check\n * @returns True if the controller is an instance of `BaseController`\n */\nfunction isBaseController(\n controller: unknown,\n): controller is ControllerInstance {\n return (\n typeof controller === 'object' &&\n controller !== null &&\n 'name' in controller &&\n typeof controller.name === 'string' &&\n 'state' in controller &&\n typeof controller.state === 'object' &&\n 'metadata' in controller &&\n typeof controller.metadata === 'object'\n );\n}\n\nexport default ComposableController;\n"]}
1
+ {"version":3,"file":"ComposableController.cjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;;;;AAQA,+DAA6E;AAEhE,QAAA,cAAc,GAAG,sBAAsB,CAAC;AAExC,QAAA,wBAAwB,GACnC,iGAAiG,CAAC;AAyEpG;;;;;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,C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,CAAC,CAAC;KAC1D;IACD,IAAI;QACF,IAAI,CAAC,eAAe,CAAC,SAAS;QAC5B,2CAA2C;QAC3C,4EAA4E;QAC5E,GAAG,IAAI,cAAc,EACrB,CAAC,UAA2B,EAAE,EAAE;YAC9B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,2IAA2I;gBAC3I,4DAA4D;gBAC3D,KAA6C,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;YACpE,CAAC,CAAC,CAAC;QACL,CAAC,CACF,CAAC;KACH;IAAC,OAAO,KAAc,EAAE;QACvB,2CAA2C;QAC3C,4EAA4E;QAC5E,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,MAAM,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;KAC7C;AACH,CAAC;AAGH,kBAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedMessenger,\n StateConstraint,\n StateMetadata,\n StateMetadataConstraint,\n ControllerStateChangeEvent,\n BaseControllerInstance as ControllerInstance,\n} from '@metamask/base-controller';\nimport { BaseController, isBaseController } from '@metamask/base-controller';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` and inherit from `BaseController`.';\n\n/**\n * The narrowest supertype for the composable controller state object.\n */\nexport type ComposableControllerStateConstraint = {\n [controllerName: string]: StateConstraint;\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> = ControllerStateChangeEvent<\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 * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> =\n ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n >\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<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> = RestrictedMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllersMap - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends ComposableControllerStateConstraint,\n ChildControllersMap extends Record<\n keyof ComposableControllerState,\n ControllerInstance\n >,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - An object that contains child controllers keyed by their names.\n * @param options.messenger - A restricted messenger.\n */\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllersMap;\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n metadata: Object.keys(controllers).reduce<\n StateMetadata<ComposableControllerState>\n >((metadata, name) => {\n (metadata as StateMetadataConstraint)[name] = {\n persist: true,\n anonymous: true,\n };\n return metadata;\n }, {} as never),\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n (state as ComposableControllerStateConstraint)[controller.name] =\n controller.state;\n return state;\n },\n {} as never,\n ),\n messenger,\n });\n\n Object.values(controllers).forEach((controller) => {\n this.#updateChildController(controller);\n });\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller)) {\n try {\n delete this.metadata[name];\n delete this.state[name];\n // eslint-disable-next-line no-empty\n } catch (_) {}\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: StateConstraint) => {\n this.update((state) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n // @ts-expect-error \"Type instantiation is excessively deep\"\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 }\n}\n\nexport default ComposableController;\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"ComposableController.d.cts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"A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,yBAAyB,SAAS,mCAAmC,EACrE,mBAAmB,SAAS,MAAM,CAChC,MAAM,yBAAyB,EAC/B,kBAAkB,CACnB,CACD,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBACS,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,mBAAmB,CAAC;QACjC,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CAuEF;AAuBD,eAAe,oBAAoB,CAAC"}
1
+ {"version":3,"file":"ComposableController.d.cts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,eAAe,EAGf,0BAA0B,EAC1B,sBAAsB,IAAI,kBAAkB,EAC7C,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAoB,kCAAkC;AAE7E,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD,eAAO,MAAM,wBAAwB,oGAC8D,CAAC;AAEpG;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAAG;IAChD,CAAC,cAAc,EAAE,MAAM,GAAG,eAAe,CAAC;CAC3C,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,oCAAoC,CAC9C,yBAAyB,SAAS,mCAAmC,IACnE,0BAA0B,CAC5B,OAAO,cAAc,EACrB,yBAAyB,CAC1B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,0BAA0B,CACpC,yBAAyB,SAAS,mCAAmC,IACnE,oCAAoC,CAAC,yBAAyB,CAAC,CAAC;AAEpE;;;;;GAKG;AACH,MAAM,MAAM,gCAAgC,CAC1C,yBAAyB,SAAS,mCAAmC,IAErE,yBAAyB,SAAS,MAAM,CACtC,MAAM,cAAc,SAAS,MAAM,EACnC,MAAM,eAAe,CACtB,GACG,eAAe,SAAS,eAAe,GACrC,0BAA0B,CAAC,cAAc,EAAE,eAAe,CAAC,GAC3D,KAAK,GACP,KAAK,CAAC;AAEZ;;;;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,mBAAmB,CACrB,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,mCAAmC,EACrE,mBAAmB,SAAS,MAAM,CAChC,MAAM,yBAAyB,EAC/B,kBAAkB,CACnB,CACD,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBACS,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,mBAAmB,CAAC;QACjC,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CAuEF;AAED,eAAe,oBAAoB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"ComposableController.d.mts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,eAAe,EAGf,0BAA0B,EAC1B,sBAAsB,IAAI,kBAAkB,EAC7C,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD,eAAO,MAAM,wBAAwB,oGAC8D,CAAC;AAEpG;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAAG;IAChD,CAAC,cAAc,EAAE,MAAM,GAAG,eAAe,CAAC;CAC3C,CAAC;AAE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AuEF;AAuBD,eAAe,oBAAoB,CAAC"}
1
+ {"version":3,"file":"ComposableController.d.mts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,eAAe,EAGf,0BAA0B,EAC1B,sBAAsB,IAAI,kBAAkB,EAC7C,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAoB,kCAAkC;AAE7E,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD,eAAO,MAAM,wBAAwB,oGAC8D,CAAC;AAEpG;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAAG;IAChD,CAAC,cAAc,EAAE,MAAM,GAAG,eAAe,CAAC;CAC3C,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,oCAAoC,CAC9C,yBAAyB,SAAS,mCAAmC,IACnE,0BAA0B,CAC5B,OAAO,cAAc,EACrB,yBAAyB,CAC1B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,0BAA0B,CACpC,yBAAyB,SAAS,mCAAmC,IACnE,oCAAoC,CAAC,yBAAyB,CAAC,CAAC;AAEpE;;;;;GAKG;AACH,MAAM,MAAM,gCAAgC,CAC1C,yBAAyB,SAAS,mCAAmC,IAErE,yBAAyB,SAAS,MAAM,CACtC,MAAM,cAAc,SAAS,MAAM,EACnC,MAAM,eAAe,CACtB,GACG,eAAe,SAAS,eAAe,GACrC,0BAA0B,CAAC,cAAc,EAAE,eAAe,CAAC,GAC3D,KAAK,GACP,KAAK,CAAC;AAEZ;;;;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,mBAAmB,CACrB,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,mCAAmC,EACrE,mBAAmB,SAAS,MAAM,CAChC,MAAM,yBAAyB,EAC/B,kBAAkB,CACnB,CACD,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBACS,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,mBAAmB,CAAC;QACjC,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CAuEF;AAED,eAAe,oBAAoB,CAAC"}
@@ -4,7 +4,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
4
4
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
5
  };
6
6
  var _ComposableController_instances, _ComposableController_updateChildController;
7
- import { BaseController } from "@metamask/base-controller";
7
+ import { BaseController, isBaseController } from "@metamask/base-controller";
8
8
  export const controllerName = 'ComposableController';
9
9
  export const INVALID_CONTROLLER_ERROR = 'Invalid controller: controller must have a `messagingSystem` and inherit from `BaseController`.';
10
10
  /**
@@ -81,21 +81,5 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
81
81
  console.error(`${name} - ${String(error)}`);
82
82
  }
83
83
  };
84
- /**
85
- * Determines if the given controller is an instance of `BaseController`
86
- *
87
- * @param controller - Controller instance to check
88
- * @returns True if the controller is an instance of `BaseController`
89
- */
90
- function isBaseController(controller) {
91
- return (typeof controller === 'object' &&
92
- controller !== null &&
93
- 'name' in controller &&
94
- typeof controller.name === 'string' &&
95
- 'state' in controller &&
96
- typeof controller.state === 'object' &&
97
- 'metadata' in controller &&
98
- typeof controller.metadata === 'object');
99
- }
100
84
  export default ComposableController;
101
85
  //# sourceMappingURL=ComposableController.mj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,QAAQ,CACxC,CAAC;AACJ,CAAC;AAED,eAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedMessenger,\n StateConstraint,\n StateMetadata,\n StateMetadataConstraint,\n ControllerStateChangeEvent,\n BaseControllerInstance as ControllerInstance,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` and inherit from `BaseController`.';\n\n/**\n * The narrowest supertype for the composable controller state object.\n */\nexport type ComposableControllerStateConstraint = {\n [controllerName: string]: StateConstraint;\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> = ControllerStateChangeEvent<\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 * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> =\n ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n >\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<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> = RestrictedMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllersMap - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends ComposableControllerStateConstraint,\n ChildControllersMap extends Record<\n keyof ComposableControllerState,\n ControllerInstance\n >,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - An object that contains child controllers keyed by their names.\n * @param options.messenger - A restricted messenger.\n */\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllersMap;\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n metadata: Object.keys(controllers).reduce<\n StateMetadata<ComposableControllerState>\n >((metadata, name) => {\n (metadata as StateMetadataConstraint)[name] = {\n persist: true,\n anonymous: true,\n };\n return metadata;\n }, {} as never),\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n (state as ComposableControllerStateConstraint)[controller.name] =\n controller.state;\n return state;\n },\n {} as never,\n ),\n messenger,\n });\n\n Object.values(controllers).forEach((controller) => {\n this.#updateChildController(controller);\n });\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller)) {\n try {\n delete this.metadata[name];\n delete this.state[name];\n // eslint-disable-next-line no-empty\n } catch (_) {}\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: StateConstraint) => {\n this.update((state) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n // @ts-expect-error \"Type instantiation is excessively deep\"\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 }\n}\n\n/**\n * Determines if the given controller is an instance of `BaseController`\n *\n * @param controller - Controller instance to check\n * @returns True if the controller is an instance of `BaseController`\n */\nfunction isBaseController(\n controller: unknown,\n): controller is ControllerInstance {\n return (\n typeof controller === 'object' &&\n controller !== null &&\n 'name' in controller &&\n typeof controller.name === 'string' &&\n 'state' in controller &&\n typeof controller.state === 'object' &&\n 'metadata' in controller &&\n typeof controller.metadata === 'object'\n );\n}\n\nexport default ComposableController;\n"]}
1
+ {"version":3,"file":"ComposableController.mjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;AAQA,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,kCAAkC;AAE7E,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,CAAC,MAAM,wBAAwB,GACnC,iGAAiG,CAAC;AAyEpG;;;;;GAKG;AACH,MAAM,OAAO,oBAMX,SAAQ,cAIT;IACC;;;;;;OAM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E,GAAE;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,UAA2B,EAAE,EAAE;YAC9B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,2IAA2I;gBAC3I,4DAA4D;gBAC3D,KAA6C,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;YACpE,CAAC,CAAC,CAAC;QACL,CAAC,CACF,CAAC;KACH;IAAC,OAAO,KAAc,EAAE;QACvB,2CAA2C;QAC3C,4EAA4E;QAC5E,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,MAAM,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;KAC7C;AACH,CAAC;AAGH,eAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedMessenger,\n StateConstraint,\n StateMetadata,\n StateMetadataConstraint,\n ControllerStateChangeEvent,\n BaseControllerInstance as ControllerInstance,\n} from '@metamask/base-controller';\nimport { BaseController, isBaseController } from '@metamask/base-controller';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` and inherit from `BaseController`.';\n\n/**\n * The narrowest supertype for the composable controller state object.\n */\nexport type ComposableControllerStateConstraint = {\n [controllerName: string]: StateConstraint;\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> = ControllerStateChangeEvent<\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 * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> =\n ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n >\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<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> = RestrictedMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllersMap - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends ComposableControllerStateConstraint,\n ChildControllersMap extends Record<\n keyof ComposableControllerState,\n ControllerInstance\n >,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - An object that contains child controllers keyed by their names.\n * @param options.messenger - A restricted messenger.\n */\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllersMap;\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n metadata: Object.keys(controllers).reduce<\n StateMetadata<ComposableControllerState>\n >((metadata, name) => {\n (metadata as StateMetadataConstraint)[name] = {\n persist: true,\n anonymous: true,\n };\n return metadata;\n }, {} as never),\n // This reduce operation intentionally reuses its output object. This provides a significant performance benefit over returning a new object on each iteration.\n state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n (state as ComposableControllerStateConstraint)[controller.name] =\n controller.state;\n return state;\n },\n {} as never,\n ),\n messenger,\n });\n\n Object.values(controllers).forEach((controller) => {\n this.#updateChildController(controller);\n });\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller)) {\n try {\n delete this.metadata[name];\n delete this.state[name];\n // eslint-disable-next-line no-empty\n } catch (_) {}\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: StateConstraint) => {\n this.update((state) => {\n // Type assertion is necessary for property assignment to a generic type. This does not pollute or widen the type of the asserted variable.\n // @ts-expect-error \"Type instantiation is excessively deep\"\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 }\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": "11.0.0-preview-ddceb3a8",
3
+ "version": "11.0.0-preview-d79fc0b7",
4
4
  "description": "Consolidates the state from multiple controllers into one",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -47,7 +47,7 @@
47
47
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
48
48
  },
49
49
  "dependencies": {
50
- "@metamask/base-controller": "^8.2.0"
50
+ "@metamask/base-controller": "^8.1.0"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@metamask/auto-changelog": "^3.4.4",