@metamask-previews/composable-controller 9.0.1-preview-913113cf → 9.0.1-preview-bb2f1fdd
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/dist/ComposableController.cjs +21 -27
- package/dist/ComposableController.cjs.map +1 -1
- package/dist/ComposableController.d.cts +13 -7
- package/dist/ComposableController.d.cts.map +1 -1
- package/dist/ComposableController.d.mts +13 -7
- package/dist/ComposableController.d.mts.map +1 -1
- package/dist/ComposableController.mjs +20 -26
- package/dist/ComposableController.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -6,22 +6,21 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
6
6
|
};
|
|
7
7
|
var _ComposableController_instances, _ComposableController_updateChildController;
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.ComposableController = exports.
|
|
9
|
+
exports.ComposableController = exports.controllerName = void 0;
|
|
10
10
|
const base_controller_1 = require("@metamask/base-controller");
|
|
11
11
|
exports.controllerName = 'ComposableController';
|
|
12
|
-
exports.INVALID_CONTROLLER_ERROR = 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';
|
|
13
12
|
/**
|
|
14
13
|
* Controller that composes multiple child controllers and maintains up-to-date composed state.
|
|
15
14
|
*
|
|
16
|
-
* @template ComposableControllerState - A type object containing the names and state types of the child controllers.
|
|
17
|
-
* @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.
|
|
15
|
+
* @template ComposableControllerState - A type object containing the names and state types of the child controllers. Any non-controllers with empty state should be omitted from this type argument.
|
|
16
|
+
* @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}. Any non-controllers with empty state should be excluded from this type argument.
|
|
18
17
|
*/
|
|
19
18
|
class ComposableController extends base_controller_1.BaseController {
|
|
20
19
|
/**
|
|
21
20
|
* Creates a ComposableController instance.
|
|
22
21
|
*
|
|
23
22
|
* @param options - Initial options used to configure this controller
|
|
24
|
-
* @param options.controllers - List of child controller instances to compose.
|
|
23
|
+
* @param options.controllers - List of child controller instances to compose. Any non-controllers that are included here will be excluded from the composed state object.
|
|
25
24
|
* @param options.messenger - A restricted controller messenger.
|
|
26
25
|
*/
|
|
27
26
|
constructor({ controllers, messenger, }) {
|
|
@@ -30,15 +29,17 @@ class ComposableController extends base_controller_1.BaseController {
|
|
|
30
29
|
}
|
|
31
30
|
super({
|
|
32
31
|
name: exports.controllerName,
|
|
33
|
-
metadata: controllers.reduce((metadata, controller) => (
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
metadata: controllers.reduce((metadata, controller) => Object.assign(metadata,
|
|
33
|
+
// Overriding for better readability
|
|
34
|
+
// eslint-disable-next-line no-nested-ternary
|
|
35
|
+
(0, base_controller_1.isBaseController)(controller)
|
|
36
|
+
? { [controller.name]: controller.metadata }
|
|
37
|
+
: (0, base_controller_1.isBaseControllerV1)(controller)
|
|
38
|
+
? { [controller.name]: { persist: true, anonymous: true } }
|
|
39
|
+
: {}), {}),
|
|
40
|
+
state: controllers.reduce((state, controller) => Object.assign(state, (0, base_controller_1.isBaseController)(controller) || (0, base_controller_1.isBaseControllerV1)(controller)
|
|
41
|
+
? { [controller.name]: controller.state }
|
|
42
|
+
: {}), {}),
|
|
42
43
|
messenger,
|
|
43
44
|
});
|
|
44
45
|
_ComposableController_instances.add(this);
|
|
@@ -47,27 +48,20 @@ class ComposableController extends base_controller_1.BaseController {
|
|
|
47
48
|
}
|
|
48
49
|
exports.ComposableController = ComposableController;
|
|
49
50
|
_ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
|
|
50
|
-
const { name } = controller;
|
|
51
51
|
if (!(0, base_controller_1.isBaseController)(controller) && !(0, base_controller_1.isBaseControllerV1)(controller)) {
|
|
52
|
-
|
|
53
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
54
|
-
throw new Error(`${name} - ${exports.INVALID_CONTROLLER_ERROR}`);
|
|
52
|
+
return;
|
|
55
53
|
}
|
|
54
|
+
const { name } = controller;
|
|
56
55
|
try {
|
|
57
|
-
this.messagingSystem.subscribe(
|
|
58
|
-
// False negative. `name` is a string type.
|
|
59
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
60
|
-
`${name}:stateChange`, (childState) => {
|
|
56
|
+
this.messagingSystem.subscribe(`${name}:stateChange`, (childState) => {
|
|
61
57
|
this.update((state) => {
|
|
62
58
|
Object.assign(state, { [name]: childState });
|
|
63
59
|
});
|
|
64
60
|
});
|
|
61
|
+
// Invalid/non-existent event names from V1 controllers and non-controllers are expected, and should be handled without blocking ComposableController instantiation in downstream clients.
|
|
62
|
+
// eslint-disable-next-line no-empty
|
|
65
63
|
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
// False negative. `name` is a string type.
|
|
68
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
69
|
-
console.error(`${name} - ${String(error)}`);
|
|
70
|
-
}
|
|
64
|
+
catch (error) { }
|
|
71
65
|
if ((0, base_controller_1.isBaseControllerV1)(controller)) {
|
|
72
66
|
controller.subscribe((childState) => {
|
|
73
67
|
this.update((state) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ComposableController.cjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;;;;AASA,+DAImC;AAGtB,QAAA,cAAc,GAAG,sBAAsB,CAAC;AAExC,QAAA,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAa,oBAGX,SAAQ,gCAIT;IACC;;;;;;OAMG;IAEH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QA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E,EAAE;gBACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CACF,CAAC;KACH;IAAC,OAAO,KAAc,EAAE;QACvB,2CAA2C;QAC3C,4EAA4E;QAC5E,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,MAAM,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;KAC7C;IACD,IAAI,IAAA,oCAAkB,EAAC,UAAU,CAAC,EAAE;QAClC,UAAU,CAAC,SAAS,CAAC,CAAC,UAA6B,EAAE,EAAE;YACrD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;AAGH,kBAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends ControllerInstance,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - List of child controller instances to compose.\n * @param options.messenger - A restricted controller messenger.\n */\n\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllers[];\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: controllers.reduce<StateMetadata<ComposableControllerState>>(\n (metadata, controller) => ({\n ...metadata,\n [controller.name]: isBaseController(controller)\n ? controller.metadata\n : { persist: true, anonymous: true },\n }),\n {} as never,\n ),\n state: controllers.reduce<ComposableControllerState>(\n (state, controller) => {\n return { ...state, [controller.name]: controller.state };\n },\n {} as never,\n ),\n messenger,\n });\n\n controllers.forEach((controller) =>\n this.#updateChildController(controller),\n );\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n },\n );\n } catch (error: unknown) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n console.error(`${name} - ${String(error)}`);\n }\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState: StateConstraintV1) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
|
|
1
|
+
{"version":3,"file":"ComposableController.cjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;;;;AASA,+DAImC;AAGtB,QAAA,cAAc,GAAG,sBAAsB,CAAC;AA+GrD;;;;;GAKG;AACH,MAAa,oBAGX,SAAQ,gCAIT;IACC;;;;;;OAMG;IAEH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QAED,KAAK,CAAC;YACJ,IAAI,EAAE,sBAAc;YACpB,QAAQ,EAAE,WAAW,CAAC,MAAM,CAC1B,CAAC,QAAQ,EAAE,UAAU,EAAE,EAAE,CACvB,MAAM,CAAC,MAAM,CACX,QAAQ;YACR,oCAAo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M,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CACF,CAAC;QACF,0LAA0L;QAC1L,oCAAoC;KACrC;IAAC,OAAO,KAAc,EAAE,GAAE;IAC3B,IAAI,IAAA,oCAAkB,EAAC,UAAU,CAAC,EAAE;QAClC,UAAU,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,EAAE;YAClC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;AAGH,kBAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\n/**\n * A universal supertype for modules with a 'string'-type `name` property.\n * This type is intended to encompass controller and non-controller input that can be passed into the {@link ComposableController} `controllers` constructor option.\n */\nexport type NamedModule = { name: string } & Record<string, unknown>;\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 [controllerName: 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 [controllerName: 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. Any non-controllers with empty state should be omitted from this type argument.\n * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}. Any non-controllers with empty state should be excluded from this type argument.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends ControllerInstance,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - List of child controller instances to compose. Any non-controllers that are included here will be excluded from the composed state object.\n * @param options.messenger - A restricted controller messenger.\n */\n\n constructor({\n controllers,\n messenger,\n }: {\n controllers: (ChildControllers | NamedModule)[];\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: controllers.reduce<StateMetadata<ComposableControllerState>>(\n (metadata, controller) =>\n Object.assign(\n metadata,\n // Overriding for better readability\n // eslint-disable-next-line no-nested-ternary\n isBaseController(controller)\n ? { [controller.name]: controller.metadata }\n : isBaseControllerV1(controller)\n ? { [controller.name]: { persist: true, anonymous: true } }\n : {},\n ),\n {} as never,\n ),\n state: controllers.reduce<ComposableControllerState>(\n (state, controller) =>\n Object.assign(\n state,\n isBaseController(controller) || isBaseControllerV1(controller)\n ? { [controller.name]: controller.state }\n : {},\n ),\n {} as never,\n ),\n messenger,\n });\n\n controllers.forEach((controller) =>\n this.#updateChildController(controller),\n );\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ChildControllers | NamedModule): void {\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n return;\n }\n const { name } = controller;\n try {\n this.messagingSystem.subscribe(\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n },\n );\n // Invalid/non-existent event names from V1 controllers and non-controllers are expected, and should be handled without blocking ComposableController instantiation in downstream clients.\n // eslint-disable-next-line no-empty\n } catch (error: unknown) {}\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
|
|
@@ -2,7 +2,13 @@ import type { RestrictedControllerMessenger, StateConstraint, StateConstraintV1,
|
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Patch } from "immer";
|
|
4
4
|
export declare const controllerName = "ComposableController";
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* A universal supertype for modules with a 'string'-type `name` property.
|
|
7
|
+
* This type is intended to encompass controller and non-controller input that can be passed into the {@link ComposableController} `controllers` constructor option.
|
|
8
|
+
*/
|
|
9
|
+
export type NamedModule = {
|
|
10
|
+
name: string;
|
|
11
|
+
} & Record<string, unknown>;
|
|
6
12
|
/**
|
|
7
13
|
* A universal supertype for the composable controller state object.
|
|
8
14
|
*
|
|
@@ -10,14 +16,14 @@ export declare const INVALID_CONTROLLER_ERROR = "Invalid controller: controller
|
|
|
10
16
|
* Note that it is unsuitable for general use as a type constraint.
|
|
11
17
|
*/
|
|
12
18
|
type LegacyComposableControllerStateConstraint = {
|
|
13
|
-
[
|
|
19
|
+
[controllerName: string]: Record<string, any>;
|
|
14
20
|
};
|
|
15
21
|
/**
|
|
16
22
|
* The narrowest supertype for the composable controller state object.
|
|
17
23
|
* This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.
|
|
18
24
|
*/
|
|
19
25
|
export type ComposableControllerStateConstraint = {
|
|
20
|
-
[
|
|
26
|
+
[controllerName: string]: LegacyControllerStateConstraint;
|
|
21
27
|
};
|
|
22
28
|
/**
|
|
23
29
|
* A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.
|
|
@@ -62,8 +68,8 @@ export type ComposableControllerMessenger<ComposableControllerState extends Comp
|
|
|
62
68
|
/**
|
|
63
69
|
* Controller that composes multiple child controllers and maintains up-to-date composed state.
|
|
64
70
|
*
|
|
65
|
-
* @template ComposableControllerState - A type object containing the names and state types of the child controllers.
|
|
66
|
-
* @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.
|
|
71
|
+
* @template ComposableControllerState - A type object containing the names and state types of the child controllers. Any non-controllers with empty state should be omitted from this type argument.
|
|
72
|
+
* @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}. Any non-controllers with empty state should be excluded from this type argument.
|
|
67
73
|
*/
|
|
68
74
|
export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends ControllerInstance> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
|
|
69
75
|
#private;
|
|
@@ -71,11 +77,11 @@ export declare class ComposableController<ComposableControllerState extends Lega
|
|
|
71
77
|
* Creates a ComposableController instance.
|
|
72
78
|
*
|
|
73
79
|
* @param options - Initial options used to configure this controller
|
|
74
|
-
* @param options.controllers - List of child controller instances to compose.
|
|
80
|
+
* @param options.controllers - List of child controller instances to compose. Any non-controllers that are included here will be excluded from the composed state object.
|
|
75
81
|
* @param options.messenger - A restricted controller messenger.
|
|
76
82
|
*/
|
|
77
83
|
constructor({ controllers, messenger, }: {
|
|
78
|
-
controllers: ChildControllers[];
|
|
84
|
+
controllers: (ChildControllers | NamedModule)[];
|
|
79
85
|
messenger: ComposableControllerMessenger<ComposableControllerState>;
|
|
80
86
|
});
|
|
81
87
|
}
|
|
@@ -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,EAEjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,OAAO,EACL,cAAc,EAGf,kCAAkC;AACnC,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc;AAEnC,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD,
|
|
1
|
+
{"version":3,"file":"ComposableController.d.cts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,EAEjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,OAAO,EACL,cAAc,EAGf,kCAAkC;AACnC,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc;AAEnC,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAErE;;;;;GAKG;AAEH,KAAK,yCAAyC,GAAG;IAI/C,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC/C,CAAC;AAEF;;;GAGG;AAEH,MAAM,MAAM,mCAAmC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BAAa,oBAAoB,CAC/B,yBAAyB,SAAS,yCAAyC,EAC3E,gBAAgB,SAAS,kBAAkB,CAC3C,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBAES,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,CAAC,gBAAgB,GAAG,WAAW,CAAC,EAAE,CAAC;QAChD,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CAqEF;AAED,eAAe,oBAAoB,CAAC"}
|
|
@@ -2,7 +2,13 @@ import type { RestrictedControllerMessenger, StateConstraint, StateConstraintV1,
|
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Patch } from "immer";
|
|
4
4
|
export declare const controllerName = "ComposableController";
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* A universal supertype for modules with a 'string'-type `name` property.
|
|
7
|
+
* This type is intended to encompass controller and non-controller input that can be passed into the {@link ComposableController} `controllers` constructor option.
|
|
8
|
+
*/
|
|
9
|
+
export type NamedModule = {
|
|
10
|
+
name: string;
|
|
11
|
+
} & Record<string, unknown>;
|
|
6
12
|
/**
|
|
7
13
|
* A universal supertype for the composable controller state object.
|
|
8
14
|
*
|
|
@@ -10,14 +16,14 @@ export declare const INVALID_CONTROLLER_ERROR = "Invalid controller: controller
|
|
|
10
16
|
* Note that it is unsuitable for general use as a type constraint.
|
|
11
17
|
*/
|
|
12
18
|
type LegacyComposableControllerStateConstraint = {
|
|
13
|
-
[
|
|
19
|
+
[controllerName: string]: Record<string, any>;
|
|
14
20
|
};
|
|
15
21
|
/**
|
|
16
22
|
* The narrowest supertype for the composable controller state object.
|
|
17
23
|
* This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.
|
|
18
24
|
*/
|
|
19
25
|
export type ComposableControllerStateConstraint = {
|
|
20
|
-
[
|
|
26
|
+
[controllerName: string]: LegacyControllerStateConstraint;
|
|
21
27
|
};
|
|
22
28
|
/**
|
|
23
29
|
* A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.
|
|
@@ -62,8 +68,8 @@ export type ComposableControllerMessenger<ComposableControllerState extends Comp
|
|
|
62
68
|
/**
|
|
63
69
|
* Controller that composes multiple child controllers and maintains up-to-date composed state.
|
|
64
70
|
*
|
|
65
|
-
* @template ComposableControllerState - A type object containing the names and state types of the child controllers.
|
|
66
|
-
* @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.
|
|
71
|
+
* @template ComposableControllerState - A type object containing the names and state types of the child controllers. Any non-controllers with empty state should be omitted from this type argument.
|
|
72
|
+
* @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}. Any non-controllers with empty state should be excluded from this type argument.
|
|
67
73
|
*/
|
|
68
74
|
export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends ControllerInstance> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
|
|
69
75
|
#private;
|
|
@@ -71,11 +77,11 @@ export declare class ComposableController<ComposableControllerState extends Lega
|
|
|
71
77
|
* Creates a ComposableController instance.
|
|
72
78
|
*
|
|
73
79
|
* @param options - Initial options used to configure this controller
|
|
74
|
-
* @param options.controllers - List of child controller instances to compose.
|
|
80
|
+
* @param options.controllers - List of child controller instances to compose. Any non-controllers that are included here will be excluded from the composed state object.
|
|
75
81
|
* @param options.messenger - A restricted controller messenger.
|
|
76
82
|
*/
|
|
77
83
|
constructor({ controllers, messenger, }: {
|
|
78
|
-
controllers: ChildControllers[];
|
|
84
|
+
controllers: (ChildControllers | NamedModule)[];
|
|
79
85
|
messenger: ComposableControllerMessenger<ComposableControllerState>;
|
|
80
86
|
});
|
|
81
87
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ComposableController.d.mts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,EAEjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,OAAO,EACL,cAAc,EAGf,kCAAkC;AACnC,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc;AAEnC,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD,
|
|
1
|
+
{"version":3,"file":"ComposableController.d.mts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,EAEjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,OAAO,EACL,cAAc,EAGf,kCAAkC;AACnC,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc;AAEnC,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAErE;;;;;GAKG;AAEH,KAAK,yCAAyC,GAAG;IAI/C,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC/C,CAAC;AAEF;;;GAGG;AAEH,MAAM,MAAM,mCAAmC,GAAG;IAChD,CAAC,cAAc,EAAE,MAAM,GAAG,+BAA+B,CAAC;CAC3D,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,gC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ED,eAAe,oBAAoB,CAAC"}
|
|
@@ -6,19 +6,18 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
6
6
|
var _ComposableController_instances, _ComposableController_updateChildController;
|
|
7
7
|
import { BaseController, isBaseController, isBaseControllerV1 } from "@metamask/base-controller";
|
|
8
8
|
export const controllerName = 'ComposableController';
|
|
9
|
-
export const INVALID_CONTROLLER_ERROR = 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';
|
|
10
9
|
/**
|
|
11
10
|
* Controller that composes multiple child controllers and maintains up-to-date composed state.
|
|
12
11
|
*
|
|
13
|
-
* @template ComposableControllerState - A type object containing the names and state types of the child controllers.
|
|
14
|
-
* @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.
|
|
12
|
+
* @template ComposableControllerState - A type object containing the names and state types of the child controllers. Any non-controllers with empty state should be omitted from this type argument.
|
|
13
|
+
* @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}. Any non-controllers with empty state should be excluded from this type argument.
|
|
15
14
|
*/
|
|
16
15
|
export class ComposableController extends BaseController {
|
|
17
16
|
/**
|
|
18
17
|
* Creates a ComposableController instance.
|
|
19
18
|
*
|
|
20
19
|
* @param options - Initial options used to configure this controller
|
|
21
|
-
* @param options.controllers - List of child controller instances to compose.
|
|
20
|
+
* @param options.controllers - List of child controller instances to compose. Any non-controllers that are included here will be excluded from the composed state object.
|
|
22
21
|
* @param options.messenger - A restricted controller messenger.
|
|
23
22
|
*/
|
|
24
23
|
constructor({ controllers, messenger, }) {
|
|
@@ -27,15 +26,17 @@ export class ComposableController extends BaseController {
|
|
|
27
26
|
}
|
|
28
27
|
super({
|
|
29
28
|
name: controllerName,
|
|
30
|
-
metadata: controllers.reduce((metadata, controller) => (
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
metadata: controllers.reduce((metadata, controller) => Object.assign(metadata,
|
|
30
|
+
// Overriding for better readability
|
|
31
|
+
// eslint-disable-next-line no-nested-ternary
|
|
32
|
+
isBaseController(controller)
|
|
33
|
+
? { [controller.name]: controller.metadata }
|
|
34
|
+
: isBaseControllerV1(controller)
|
|
35
|
+
? { [controller.name]: { persist: true, anonymous: true } }
|
|
36
|
+
: {}), {}),
|
|
37
|
+
state: controllers.reduce((state, controller) => Object.assign(state, isBaseController(controller) || isBaseControllerV1(controller)
|
|
38
|
+
? { [controller.name]: controller.state }
|
|
39
|
+
: {}), {}),
|
|
39
40
|
messenger,
|
|
40
41
|
});
|
|
41
42
|
_ComposableController_instances.add(this);
|
|
@@ -43,27 +44,20 @@ export class ComposableController extends BaseController {
|
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
_ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
|
|
46
|
-
const { name } = controller;
|
|
47
47
|
if (!isBaseController(controller) && !isBaseControllerV1(controller)) {
|
|
48
|
-
|
|
49
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
50
|
-
throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);
|
|
48
|
+
return;
|
|
51
49
|
}
|
|
50
|
+
const { name } = controller;
|
|
52
51
|
try {
|
|
53
|
-
this.messagingSystem.subscribe(
|
|
54
|
-
// False negative. `name` is a string type.
|
|
55
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
56
|
-
`${name}:stateChange`, (childState) => {
|
|
52
|
+
this.messagingSystem.subscribe(`${name}:stateChange`, (childState) => {
|
|
57
53
|
this.update((state) => {
|
|
58
54
|
Object.assign(state, { [name]: childState });
|
|
59
55
|
});
|
|
60
56
|
});
|
|
57
|
+
// Invalid/non-existent event names from V1 controllers and non-controllers are expected, and should be handled without blocking ComposableController instantiation in downstream clients.
|
|
58
|
+
// eslint-disable-next-line no-empty
|
|
61
59
|
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
// False negative. `name` is a string type.
|
|
64
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
65
|
-
console.error(`${name} - ${String(error)}`);
|
|
66
|
-
}
|
|
60
|
+
catch (error) { }
|
|
67
61
|
if (isBaseControllerV1(controller)) {
|
|
68
62
|
controller.subscribe((childState) => {
|
|
69
63
|
this.update((state) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ComposableController.mjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;AASA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EACnB,kCAAkC;AAGnC,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,CAAC,MAAM,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAM,OAAO,oBAGX,SAAQ,cAIT;IACC;;;;;;OAMG;IAEH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QAED,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,WAAW,CAAC,MAAM,CAC1B,CAAC,QAAQ,EAAE,UAAU,EAAE,EAAE,CAAC,CAAC;gBACzB,GAAG,QAAQ;gBACX,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,gBAAgB,CAAC,UAAU,CAAC;oBAC7C,CAAC,CAAC,UAAU,CAAC,QAAQ;oBACrB,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;aACvC,CAAC,EACF,EAAW,CACZ;YACD,KAAK,EAAE,WAAW,CAAC,MAAM,CACvB,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;gBACpB,OAAO,EAAE,GAAG,KAAK,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EA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AE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;AAGH,eAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends ControllerInstance,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - List of child controller instances to compose.\n * @param options.messenger - A restricted controller messenger.\n */\n\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllers[];\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: controllers.reduce<StateMetadata<ComposableControllerState>>(\n (metadata, controller) => ({\n ...metadata,\n [controller.name]: isBaseController(controller)\n ? controller.metadata\n : { persist: true, anonymous: true },\n }),\n {} as never,\n ),\n state: controllers.reduce<ComposableControllerState>(\n (state, controller) => {\n return { ...state, [controller.name]: controller.state };\n },\n {} as never,\n ),\n messenger,\n });\n\n controllers.forEach((controller) =>\n this.#updateChildController(controller),\n );\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n },\n );\n } catch (error: unknown) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n console.error(`${name} - ${String(error)}`);\n }\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState: StateConstraintV1) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
|
|
1
|
+
{"version":3,"file":"ComposableController.mjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;AASA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EACnB,kCAAkC;AAGnC,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AA+GrD;;;;;GAKG;AACH,MAAM,OAAO,oBAGX,SAAQ,cAIT;IACC;;;;;;OAMG;IAEH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QAED,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,WAAW,CAAC,MAAM,CAC1B,CAAC,QAAQ,EAAE,UAAU,EAAE,EAAE,CACvB,MAAM,CAAC,MAAM,CACX,QAAQ;YACR,oCAAoC;YACpC,6CAA6C;YAC7C,gBAAgB,CAAC,UAAU,CAAC;gBAC1B,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,UAAU,CAAC,QAAQ,EAAE;gBAC5C,CAAC,CAAC,kBAAkB,CAAC,UAAU,CAAC;oBAChC,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE;oBAC3D,CAAC,CAAC,EAAE,CACP,EACH,EAAW,CACZ;YACD,KAAK,EAAE,WAAW,CAAC,MAAM,CACvB,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,CACpB,MAAM,CAAC,MAAM,CACX,KAAK,EACL,gBAAgB,CAAC,UA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","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\n/**\n * A universal supertype for modules with a 'string'-type `name` property.\n * This type is intended to encompass controller and non-controller input that can be passed into the {@link ComposableController} `controllers` constructor option.\n */\nexport type NamedModule = { name: string } & Record<string, unknown>;\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 [controllerName: 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 [controllerName: 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. Any non-controllers with empty state should be omitted from this type argument.\n * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}. Any non-controllers with empty state should be excluded from this type argument.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends ControllerInstance,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - List of child controller instances to compose. Any non-controllers that are included here will be excluded from the composed state object.\n * @param options.messenger - A restricted controller messenger.\n */\n\n constructor({\n controllers,\n messenger,\n }: {\n controllers: (ChildControllers | NamedModule)[];\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: controllers.reduce<StateMetadata<ComposableControllerState>>(\n (metadata, controller) =>\n Object.assign(\n metadata,\n // Overriding for better readability\n // eslint-disable-next-line no-nested-ternary\n isBaseController(controller)\n ? { [controller.name]: controller.metadata }\n : isBaseControllerV1(controller)\n ? { [controller.name]: { persist: true, anonymous: true } }\n : {},\n ),\n {} as never,\n ),\n state: controllers.reduce<ComposableControllerState>(\n (state, controller) =>\n Object.assign(\n state,\n isBaseController(controller) || isBaseControllerV1(controller)\n ? { [controller.name]: controller.state }\n : {},\n ),\n {} as never,\n ),\n messenger,\n });\n\n controllers.forEach((controller) =>\n this.#updateChildController(controller),\n );\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ChildControllers | NamedModule): void {\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n return;\n }\n const { name } = controller;\n try {\n this.messagingSystem.subscribe(\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n },\n );\n // Invalid/non-existent event names from V1 controllers and non-controllers are expected, and should be handled without blocking ComposableController instantiation in downstream clients.\n // eslint-disable-next-line no-empty\n } catch (error: unknown) {}\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
|
package/package.json
CHANGED