@metamask-previews/composable-controller 9.0.1-preview-b5d0cd70 → 9.0.1-preview-9d476a5
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 +10 -0
- package/dist/ComposableController.cjs +24 -10
- package/dist/ComposableController.cjs.map +1 -1
- package/dist/ComposableController.d.cts +3 -3
- package/dist/ComposableController.d.cts.map +1 -1
- package/dist/ComposableController.d.mts +3 -3
- package/dist/ComposableController.d.mts.map +1 -1
- package/dist/ComposableController.mjs +24 -10
- package/dist/ComposableController.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
- **BREAKING:** `ComposableController` constructor option `controllers` and generic type argument `ChildControllers` are re-defined from an array of controller instances to an object that maps controller names to controller instances.
|
13
|
+
- **BREAKING:** `ComposableController` `state` field and `metadata` objects exclude child controllers that do not extend from `BaseController` or `BaseControllerV1`.
|
14
|
+
|
15
|
+
## Fixed
|
16
|
+
|
17
|
+
- **BREAKING:** `ComposableController` `metadata` field object now correctly populates `BaseControllerV1` controller properties with a metadata object that maps state property names to `StateMetadataProperty` type objects.
|
18
|
+
- Previously, during `metadata` object instantiation, `BaseControllerV1` controllers were assigned the object `{ persist: true, anonymous: true }`, and their state properties were overwritten.
|
19
|
+
|
10
20
|
## [9.0.1]
|
11
21
|
|
12
22
|
### Fixed
|
@@ -21,7 +21,7 @@ class ComposableController extends base_controller_1.BaseController {
|
|
21
21
|
* Creates a ComposableController instance.
|
22
22
|
*
|
23
23
|
* @param options - Initial options used to configure this controller
|
24
|
-
* @param options.controllers -
|
24
|
+
* @param options.controllers - An object of child controller instances to compose that is keyed with the names of the controllers.
|
25
25
|
* @param options.messenger - A restricted controller messenger.
|
26
26
|
*/
|
27
27
|
constructor({ controllers, messenger, }) {
|
@@ -30,19 +30,33 @@ class ComposableController extends base_controller_1.BaseController {
|
|
30
30
|
}
|
31
31
|
super({
|
32
32
|
name: exports.controllerName,
|
33
|
-
metadata: controllers.reduce((metadata, controller) =>
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
33
|
+
metadata: Object.entries(controllers).reduce((metadata, [name, controller]) => {
|
34
|
+
if ((0, base_controller_1.isBaseController)(controller)) {
|
35
|
+
// Type assertion is necessary for to assign new properties to a generic type - ts(2862)
|
36
|
+
metadata[name] = controller.metadata;
|
37
|
+
}
|
38
|
+
else if ((0, base_controller_1.isBaseControllerV1)(controller)) {
|
39
|
+
// Type assertion is necessary for to assign new properties to a generic type - ts(2862)
|
40
|
+
metadata[name] = Object.keys(controller.state).reduce((acc, curr) => {
|
41
|
+
acc[curr] = { persist: true, anonymous: true };
|
42
|
+
return acc;
|
43
|
+
}, {});
|
44
|
+
}
|
45
|
+
return metadata;
|
46
|
+
}, {}),
|
47
|
+
state: Object.entries(controllers).reduce((state, [name, controller]) => {
|
48
|
+
if ((0, base_controller_1.isBaseController)(controller) || (0, base_controller_1.isBaseControllerV1)(controller)) {
|
49
|
+
// Type assertion is necessary for to assign new properties to a generic type - ts(2862)
|
50
|
+
// TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.
|
51
|
+
state[name] =
|
52
|
+
controller.state;
|
53
|
+
}
|
54
|
+
return state;
|
41
55
|
}, {}),
|
42
56
|
messenger,
|
43
57
|
});
|
44
58
|
_ComposableController_instances.add(this);
|
45
|
-
controllers.forEach((
|
59
|
+
Object.keys(controllers).forEach((name) => __classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controllers[name]));
|
46
60
|
}
|
47
61
|
}
|
48
62
|
exports.ComposableController = ComposableController;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"ComposableController.cjs","sourceRoot":"","sources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AAC,IAAA,oCAAkB,EAAC,UAAU,CAAC,EAAE;QACpE,2CAA2C;QAC3C,4EAA4E;QAC5E,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,MAAM,gCAAwB,EAAE,CAAC,CAAC;KAC1D;IACD,IAAI;QACF,IAAI,CAAC,eAAe,CAAC,SAAS;QAC5B,2CAA2C;QAC3C,4EAA4E;QAC5E,GAAG,IAAI,cAAc,EACrB,CAAC,UAA2C,EAAE,EAAE;YAC9C,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CACF,CAAC;KACH;IAAC,OAAO,KAAc,EAAE;QACvB,2CAA2C;QAC3C,4EAA4E;QAC5E,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,MAAM,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;KAC7C;IACD,IAAI,IAAA,oCAAkB,EAAC,UAAU,CAAC,EAAE;QAClC,UAAU,CAAC,SAAS,CAAC,CAAC,UAA6B,EAAE,EAAE;YACrD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;AAGH,kBAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends ControllerInstance,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - List of child controller instances to compose.\n * @param options.messenger - A restricted controller messenger.\n */\n\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllers[];\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: controllers.reduce<StateMetadata<ComposableControllerState>>(\n (metadata, controller) => ({\n ...metadata,\n [controller.name]: isBaseController(controller)\n ? controller.metadata\n : { persist: true, anonymous: true },\n }),\n {} as never,\n ),\n state: controllers.reduce<ComposableControllerState>(\n (state, controller) => {\n return { ...state, [controller.name]: controller.state };\n },\n {} as never,\n ),\n messenger,\n });\n\n controllers.forEach((controller) =>\n this.#updateChildController(controller),\n );\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n },\n );\n } catch (error: unknown) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n console.error(`${name} - ${String(error)}`);\n }\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState: StateConstraintV1) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
|
1
|
+
{"version":3,"file":"ComposableController.cjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;;;;AAUA,+DAImC;AAGtB,QAAA,cAAc,GAAG,sBAAsB,CAAC;AAExC,QAAA,wBAAwB,GACnC,gHAAgH,C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,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 StateMetadataConstraint,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends Record<string, ControllerInstance>,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - An object of child controller instances to compose that is keyed with the names of the controllers.\n * @param options.messenger - A restricted controller messenger.\n */\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllers;\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: Object.entries(controllers).reduce<\n StateMetadata<ComposableControllerState>\n >((metadata, [name, controller]) => {\n if (isBaseController(controller)) {\n // Type assertion is necessary for to assign new properties to a generic type - ts(2862)\n (metadata as unknown as Record<string, StateMetadataConstraint>)[\n name\n ] = controller.metadata;\n } else if (isBaseControllerV1(controller)) {\n // Type assertion is necessary for to assign new properties to a generic type - ts(2862)\n (metadata as unknown as Record<string, StateMetadataConstraint>)[\n name\n ] = Object.keys(controller.state).reduce<StateMetadataConstraint>(\n (acc, curr) => {\n acc[curr] = { persist: true, anonymous: true };\n return acc;\n },\n {} as never,\n );\n }\n return metadata;\n }, {} as never),\n state: Object.entries(controllers).reduce<ComposableControllerState>(\n (state, [name, controller]) => {\n if (isBaseController(controller) || isBaseControllerV1(controller)) {\n // Type assertion is necessary for to assign new properties to a generic type - ts(2862)\n // TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.\n (state as Record<string, StateConstraint | object>)[name] =\n controller.state;\n }\n return state;\n },\n {} as never,\n ),\n messenger,\n });\n\n Object.keys(controllers).forEach((name) =>\n this.#updateChildController(controllers[name]),\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"]}
|
@@ -65,17 +65,17 @@ export type ComposableControllerMessenger<ComposableControllerState extends Comp
|
|
65
65
|
* @template ComposableControllerState - A type object containing the names and state types of the child controllers.
|
66
66
|
* @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.
|
67
67
|
*/
|
68
|
-
export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends ControllerInstance
|
68
|
+
export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends Record<string, ControllerInstance>> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
|
69
69
|
#private;
|
70
70
|
/**
|
71
71
|
* Creates a ComposableController instance.
|
72
72
|
*
|
73
73
|
* @param options - Initial options used to configure this controller
|
74
|
-
* @param options.controllers -
|
74
|
+
* @param options.controllers - An object of child controller instances to compose that is keyed with the names of the controllers.
|
75
75
|
* @param options.messenger - A restricted controller messenger.
|
76
76
|
*/
|
77
77
|
constructor({ controllers, messenger, }: {
|
78
|
-
controllers: ChildControllers
|
78
|
+
controllers: ChildControllers;
|
79
79
|
messenger: ComposableControllerMessenger<ComposableControllerState>;
|
80
80
|
});
|
81
81
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"ComposableController.d.cts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,
|
1
|
+
{"version":3,"file":"ComposableController.d.cts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,EAGjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,OAAO,EACL,cAAc,EAGf,kCAAkC;AACnC,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc;AAEnC,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD,eAAO,MAAM,wBAAwB,mHAC6E,CAAC;AAEnH;;;;;GAKG;AAEH,KAAK,yCAAyC,GAAG;IAI/C,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACrC,CAAC;AAEF;;;GAGG;AAEH,MAAM,MAAM,mCAAmC,GAAG;IAChD,CAAC,IAAI,EAAE,MAAM,GAAG,+BAA+B,CAAC;CACjD,CAAC;AAEF;;GAEG;AAEH,KAAK,gCAAgC,CACnC,cAAc,SAAS,MAAM,EAC7B,eAAe,SAAS,iBAAiB,IACvC;IACF,IAAI,EAAE,GAAG,cAAc,cAAc,CAAC;IACtC,OAAO,EAAE,CAAC,eAAe,EAAE,KAAK,EAAE,CAAC,CAAC;CACrC,CAAC;AAEF;;;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C;QAC9B,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CAqFF;AAED,eAAe,oBAAoB,CAAC"}
|
@@ -65,17 +65,17 @@ export type ComposableControllerMessenger<ComposableControllerState extends Comp
|
|
65
65
|
* @template ComposableControllerState - A type object containing the names and state types of the child controllers.
|
66
66
|
* @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.
|
67
67
|
*/
|
68
|
-
export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends ControllerInstance
|
68
|
+
export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends Record<string, ControllerInstance>> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
|
69
69
|
#private;
|
70
70
|
/**
|
71
71
|
* Creates a ComposableController instance.
|
72
72
|
*
|
73
73
|
* @param options - Initial options used to configure this controller
|
74
|
-
* @param options.controllers -
|
74
|
+
* @param options.controllers - An object of child controller instances to compose that is keyed with the names of the controllers.
|
75
75
|
* @param options.messenger - A restricted controller messenger.
|
76
76
|
*/
|
77
77
|
constructor({ controllers, messenger, }: {
|
78
|
-
controllers: ChildControllers
|
78
|
+
controllers: ChildControllers;
|
79
79
|
messenger: ComposableControllerMessenger<ComposableControllerState>;
|
80
80
|
});
|
81
81
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"ComposableController.d.mts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,
|
1
|
+
{"version":3,"file":"ComposableController.d.mts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,EAGjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,OAAO,EACL,cAAc,EAGf,kCAAk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BAAyB,CAAC,CAAC;AAEhE;;;;GAIG;AACH,MAAM,MAAM,6BAA6B,CACvC,yBAAyB,SAAS,mCAAmC,IACnE,6BAA6B,CAC/B,OAAO,cAAc,EACrB,KAAK,EACH,0BAA0B,CAAC,yBAAyB,CAAC,GACrD,aAAa,CAAC,yBAAyB,CAAC,EAC1C,KAAK,EACL,aAAa,CAAC,yBAAyB,CAAC,CAAC,MAAM,CAAC,CACjD,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,oBAAoB,CAC/B,yBAAyB,SAAS,yCAAyC,EAC3E,gBAAgB,SAAS,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAC3D,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBACS,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,gBAAgB,CAAC;QAC9B,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CAqFF;AAED,eAAe,oBAAoB,CAAC"}
|
@@ -18,7 +18,7 @@ export class ComposableController extends BaseController {
|
|
18
18
|
* Creates a ComposableController instance.
|
19
19
|
*
|
20
20
|
* @param options - Initial options used to configure this controller
|
21
|
-
* @param options.controllers -
|
21
|
+
* @param options.controllers - An object of child controller instances to compose that is keyed with the names of the controllers.
|
22
22
|
* @param options.messenger - A restricted controller messenger.
|
23
23
|
*/
|
24
24
|
constructor({ controllers, messenger, }) {
|
@@ -27,19 +27,33 @@ export class ComposableController extends BaseController {
|
|
27
27
|
}
|
28
28
|
super({
|
29
29
|
name: controllerName,
|
30
|
-
metadata: controllers.reduce((metadata, controller) =>
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
30
|
+
metadata: Object.entries(controllers).reduce((metadata, [name, controller]) => {
|
31
|
+
if (isBaseController(controller)) {
|
32
|
+
// Type assertion is necessary for to assign new properties to a generic type - ts(2862)
|
33
|
+
metadata[name] = controller.metadata;
|
34
|
+
}
|
35
|
+
else if (isBaseControllerV1(controller)) {
|
36
|
+
// Type assertion is necessary for to assign new properties to a generic type - ts(2862)
|
37
|
+
metadata[name] = Object.keys(controller.state).reduce((acc, curr) => {
|
38
|
+
acc[curr] = { persist: true, anonymous: true };
|
39
|
+
return acc;
|
40
|
+
}, {});
|
41
|
+
}
|
42
|
+
return metadata;
|
43
|
+
}, {}),
|
44
|
+
state: Object.entries(controllers).reduce((state, [name, controller]) => {
|
45
|
+
if (isBaseController(controller) || isBaseControllerV1(controller)) {
|
46
|
+
// Type assertion is necessary for to assign new properties to a generic type - ts(2862)
|
47
|
+
// TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.
|
48
|
+
state[name] =
|
49
|
+
controller.state;
|
50
|
+
}
|
51
|
+
return state;
|
38
52
|
}, {}),
|
39
53
|
messenger,
|
40
54
|
});
|
41
55
|
_ComposableController_instances.add(this);
|
42
|
-
controllers.forEach((
|
56
|
+
Object.keys(controllers).forEach((name) => __classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controllers[name]));
|
43
57
|
}
|
44
58
|
}
|
45
59
|
_ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
|
@@ -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,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AI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC/C,CAAC,CAAC,CAAC;QACL,CAAC,CACF,CAAC;KACH;IAAC,OAAO,KAAc,EAAE;QACvB,2CAA2C;QAC3C,4EAA4E;QAC5E,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,MAAM,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;KAC7C;IACD,IAAI,kBAAkB,CAAC,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,eAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends ControllerInstance,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - List of child controller instances to compose.\n * @param options.messenger - A restricted controller messenger.\n */\n\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllers[];\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: controllers.reduce<StateMetadata<ComposableControllerState>>(\n (metadata, controller) => ({\n ...metadata,\n [controller.name]: isBaseController(controller)\n ? controller.metadata\n : { persist: true, anonymous: true },\n }),\n {} as never,\n ),\n state: controllers.reduce<ComposableControllerState>(\n (state, controller) => {\n return { ...state, [controller.name]: controller.state };\n },\n {} as never,\n ),\n messenger,\n });\n\n controllers.forEach((controller) =>\n this.#updateChildController(controller),\n );\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n },\n );\n } catch (error: unknown) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n console.error(`${name} - ${String(error)}`);\n }\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState: StateConstraintV1) => {\n this.update((state) => {\n Object.assign(state, { [name]: childState });\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
|
1
|
+
{"version":3,"file":"ComposableController.mjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;AAUA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EACnB,kCAAkC;AAGnC,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,CAAC,MAAM,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAM,OAAO,oBAGX,SAAQ,cAIT;IACC;;;;;;OAMG;IACH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QAED,KAAK,CAAC;YACJ,IA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StateMetadataConstraint,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllers - A union type of the child controllers being used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends Record<string, ControllerInstance>,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - An object of child controller instances to compose that is keyed with the names of the controllers.\n * @param options.messenger - A restricted controller messenger.\n */\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllers;\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: Object.entries(controllers).reduce<\n StateMetadata<ComposableControllerState>\n >((metadata, [name, controller]) => {\n if (isBaseController(controller)) {\n // Type assertion is necessary for to assign new properties to a generic type - ts(2862)\n (metadata as unknown as Record<string, StateMetadataConstraint>)[\n name\n ] = controller.metadata;\n } else if (isBaseControllerV1(controller)) {\n // Type assertion is necessary for to assign new properties to a generic type - ts(2862)\n (metadata as unknown as Record<string, StateMetadataConstraint>)[\n name\n ] = Object.keys(controller.state).reduce<StateMetadataConstraint>(\n (acc, curr) => {\n acc[curr] = { persist: true, anonymous: true };\n return acc;\n },\n {} as never,\n );\n }\n return metadata;\n }, {} as never),\n state: Object.entries(controllers).reduce<ComposableControllerState>(\n (state, [name, controller]) => {\n if (isBaseController(controller) || isBaseControllerV1(controller)) {\n // Type assertion is necessary for to assign new properties to a generic type - ts(2862)\n // TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.\n (state as Record<string, StateConstraint | object>)[name] =\n controller.state;\n }\n return state;\n },\n {} as never,\n ),\n messenger,\n });\n\n Object.keys(controllers).forEach((name) =>\n this.#updateChildController(controllers[name]),\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"]}
|
package/package.json
CHANGED