@metamask-previews/composable-controller 9.0.1-preview-9d476a5 → 9.0.1-preview-9959ebb6
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 +24 -24
- package/dist/ComposableController.cjs.map +1 -1
- package/dist/ComposableController.d.cts +2 -2
- package/dist/ComposableController.d.cts.map +1 -1
- package/dist/ComposableController.d.mts +2 -2
- package/dist/ComposableController.d.mts.map +1 -1
- package/dist/ComposableController.mjs +24 -24
- package/dist/ComposableController.mjs.map +1 -1
- package/package.json +1 -1
@@ -14,14 +14,14 @@ exports.INVALID_CONTROLLER_ERROR = 'Invalid controller: controller must have a `
|
|
14
14
|
* Controller that composes multiple child controllers and maintains up-to-date composed state.
|
15
15
|
*
|
16
16
|
* @template ComposableControllerState - A type object containing the names and state types of the child controllers.
|
17
|
-
* @template ChildControllers - A
|
17
|
+
* @template ChildControllers - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.
|
18
18
|
*/
|
19
19
|
class ComposableController extends base_controller_1.BaseController {
|
20
20
|
/**
|
21
21
|
* Creates a ComposableController instance.
|
22
22
|
*
|
23
23
|
* @param options - Initial options used to configure this controller
|
24
|
-
* @param options.controllers - An object
|
24
|
+
* @param options.controllers - An object that contains child controllers keyed by their names.
|
25
25
|
* @param options.messenger - A restricted controller messenger.
|
26
26
|
*/
|
27
27
|
constructor({ controllers, messenger, }) {
|
@@ -30,39 +30,38 @@ class ComposableController extends base_controller_1.BaseController {
|
|
30
30
|
}
|
31
31
|
super({
|
32
32
|
name: exports.controllerName,
|
33
|
-
metadata: Object.
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
}
|
33
|
+
metadata: Object.keys(controllers).reduce((metadata, name) => {
|
34
|
+
// Type assertion is necessary to assign new properties to a generic type - ts(2862)
|
35
|
+
metadata[name] = {
|
36
|
+
persist: true,
|
37
|
+
anonymous: true,
|
38
|
+
};
|
45
39
|
return metadata;
|
46
40
|
}, {}),
|
47
|
-
state: Object.
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
state[name] =
|
52
|
-
controller.state;
|
53
|
-
}
|
41
|
+
state: Object.values(controllers).reduce((state, controller) => {
|
42
|
+
// Type assertion is necessary to assign new properties to a generic type - ts(2862)
|
43
|
+
// TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.
|
44
|
+
state[controller.name] = controller.state;
|
54
45
|
return state;
|
55
46
|
}, {}),
|
56
47
|
messenger,
|
57
48
|
});
|
58
49
|
_ComposableController_instances.add(this);
|
59
|
-
Object.
|
50
|
+
Object.values(controllers).forEach((controller) => {
|
51
|
+
__classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controller);
|
52
|
+
});
|
60
53
|
}
|
61
54
|
}
|
62
55
|
exports.ComposableController = ComposableController;
|
63
56
|
_ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
|
64
57
|
const { name } = controller;
|
65
58
|
if (!(0, base_controller_1.isBaseController)(controller) && !(0, base_controller_1.isBaseControllerV1)(controller)) {
|
59
|
+
if (name in this.metadata && this.metadata[name]) {
|
60
|
+
delete this.metadata[name];
|
61
|
+
}
|
62
|
+
if (name in this.state && this.state[name]) {
|
63
|
+
delete this.state[name];
|
64
|
+
}
|
66
65
|
// False negative. `name` is a string type.
|
67
66
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
68
67
|
throw new Error(`${name} - ${exports.INVALID_CONTROLLER_ERROR}`);
|
@@ -73,7 +72,8 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
|
|
73
72
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
74
73
|
`${name}:stateChange`, (childState) => {
|
75
74
|
this.update((state) => {
|
76
|
-
|
75
|
+
state[name] =
|
76
|
+
childState;
|
77
77
|
});
|
78
78
|
});
|
79
79
|
}
|
@@ -85,7 +85,7 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
|
|
85
85
|
if ((0, base_controller_1.isBaseControllerV1)(controller)) {
|
86
86
|
controller.subscribe((childState) => {
|
87
87
|
this.update((state) => {
|
88
|
-
|
88
|
+
state[name] = childState;
|
89
89
|
});
|
90
90
|
});
|
91
91
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"ComposableController.cjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;;;;AAUA,+DAImC;AAGtB,QAAA,cAAc,GAAG,sBAAsB,CAAC;AAExC,QAAA,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAa,oBAGX,SAAQ,gCAIT;IACC;;;;;;OAMG;IACH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QAED,KAAK,CAAC;YACJ,IAAI,EAAE,sBAAc;YACpB,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,MAAM,CAE1C,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,EAAE,EAAE;gBACjC,IAAI,IAAA,kCAAgB,EAAC,UAAU,CAAC,EAAE;oBAChC,wFAAwF;oBACvF,QAA+D,CAC9D,IAAI,CACL,GAAG,UAAU,CAAC,QAAQ,CAAC;iBACzB;qBAAM,IAAI,IAAA,oCAAkB,EAAC,UAAU,CAAC,EAAE;oBACzC,wFAAwF;oBACvF,QAA+D,CAC9D,IAAI,CACL,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,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4E;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 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"]}
|
1
|
+
{"version":3,"file":"ComposableController.cjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;;;;AAUA,+DAImC;AAGtB,QAAA,cAAc,GAAG,sBAAsB,CAAC;AAExC,QAAA,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAa,oBAGX,SAAQ,gCAIT;IACC;;;;;;OAMG;IACH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QAED,KAAK,CAAC;YACJ,IAAI,EAAE,sBAAc;YACpB,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,CAEvC,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE;gBACnB,oFAAoF;gBACnF,QAA4D,CAAC,IAAI,CAAC,GAAG;oBACpE,OAAO,EAAE,IAAI;oBACb,SAAS,EAAE,IAAI;iBAChB,CAAC;gBACF,OAAO,QAAQ,CAAC;YAClB,CAAC,EAAE,EAAW,CAAC;YACf,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,MAAM,CACtC,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;gBACpB,oFAAoF;gBACpF,yGAAyG;gBACxG,KAAmD,CAClD,UAAU,CAAC,IAAI,CAChB,GAAG,UAAU,CAAC,KAAK,CAAC;gBACrB,OAAO,KAAK,CAAC;YACf,CAAC,EACD,EAAW,CACZ;YACD,SAAS;SACV,CAAC,CAAC;;QAEH,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;YAChD,uBAAA,IAAI,oFAAuB,MAA3B,IAAI,EAAwB,UAAU,CAAC,CAAC;QAC1C,CAAC,CAAC,C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AA2C,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;YAClE,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;AAGH,kBAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n StatePropertyMetadataConstraint,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllers - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends Record<string, ControllerInstance>,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - An object that contains child controllers keyed by their names.\n * @param options.messenger - A restricted controller messenger.\n */\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllers;\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: Object.keys(controllers).reduce<\n StateMetadata<ComposableControllerState>\n >((metadata, name) => {\n // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n (metadata as Record<string, StatePropertyMetadataConstraint>)[name] = {\n persist: true,\n anonymous: true,\n };\n return metadata;\n }, {} as never),\n state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\n // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n // TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.\n (state as LegacyComposableControllerStateConstraint)[\n controller.name\n ] = controller.state;\n return state;\n },\n {} as never,\n ),\n messenger,\n });\n\n Object.values(controllers).forEach((controller) => {\n this.#updateChildController(controller);\n });\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n if (name in this.metadata && this.metadata[name]) {\n delete this.metadata[name];\n }\n if (name in this.state && this.state[name]) {\n delete this.state[name];\n }\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n (state as LegacyComposableControllerStateConstraint)[name] =\n childState;\n });\n },\n );\n } catch (error: unknown) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n console.error(`${name} - ${String(error)}`);\n }\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState: StateConstraintV1) => {\n this.update((state) => {\n (state as Record<string, StateConstraintV1>)[name] = childState;\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
|
@@ -63,7 +63,7 @@ export type ComposableControllerMessenger<ComposableControllerState extends Comp
|
|
63
63
|
* Controller that composes multiple child controllers and maintains up-to-date composed state.
|
64
64
|
*
|
65
65
|
* @template ComposableControllerState - A type object containing the names and state types of the child controllers.
|
66
|
-
* @template ChildControllers - A
|
66
|
+
* @template ChildControllers - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.
|
67
67
|
*/
|
68
68
|
export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends Record<string, ControllerInstance>> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
|
69
69
|
#private;
|
@@ -71,7 +71,7 @@ export declare class ComposableController<ComposableControllerState extends Lega
|
|
71
71
|
* Creates a ComposableController instance.
|
72
72
|
*
|
73
73
|
* @param options - Initial options used to configure this controller
|
74
|
-
* @param options.controllers - An object
|
74
|
+
* @param options.controllers - An object that contains child controllers keyed by their names.
|
75
75
|
* @param options.messenger - A restricted controller messenger.
|
76
76
|
*/
|
77
77
|
constructor({ controllers, messenger, }: {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"ComposableController.d.cts","sour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CAAC,GAE7D,eAAe,SAAS,iBAAiB,GACvC,gCAAgC,CAAC,cAAc,EAAE,eAAe,CAAC,GACjE,KAAK,GACP,KAAK,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,aAAa,CACvB,yBAAyB,SAAS,mCAAmC,IACnE,gCAAgC,CAAC,yBAAyB,CAAC,CAAC;AAEhE;;;;GAIG;AACH,MAAM,MAAM,6BAA6B,CACvC,yBAAyB,SAAS,mCAAmC,IACnE,6BAA6B,CAC/B,OAAO,cAAc,EACrB,KAAK,EACH,0BAA0B,CAAC,yBAAyB,CAAC,GACrD,aAAa,CAAC,yBAAyB,CAAC,EAC1C,KAAK,EACL,aAAa,CAAC,yBAAyB,CAAC,CAAC,MAAM,CAAC,CACjD,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,oBAAoB,CAC/B,yBAAyB,SAAS,yCAAyC,EAC3E,gBAAgB,SAAS,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAC3D,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBACS,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,gBAAgB,CAAC;QAC9B,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;
|
1
|
+
{"version":3,"file":"ComposableController.d.cts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,EAGjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,OAAO,EACL,cAAc,EAGf,kCAAkC;AACnC,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc;AAEnC,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD,eAAO,MAAM,wBAAwB,mHAC6E,CAAC;AAEnH;;;;;GAKG;AAEH,KAAK,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yBAAyB,CAAC,GACrD,aAAa,CAAC,yBAAyB,CAAC,EAC1C,KAAK,EACL,aAAa,CAAC,yBAAyB,CAAC,CAAC,MAAM,CAAC,CACjD,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,oBAAoB,CAC/B,yBAAyB,SAAS,yCAAyC,EAC3E,gBAAgB,SAAS,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAC3D,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBACS,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,gBAAgB,CAAC;QAC9B,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CA+EF;AAED,eAAe,oBAAoB,CAAC"}
|
@@ -63,7 +63,7 @@ export type ComposableControllerMessenger<ComposableControllerState extends Comp
|
|
63
63
|
* Controller that composes multiple child controllers and maintains up-to-date composed state.
|
64
64
|
*
|
65
65
|
* @template ComposableControllerState - A type object containing the names and state types of the child controllers.
|
66
|
-
* @template ChildControllers - A
|
66
|
+
* @template ChildControllers - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.
|
67
67
|
*/
|
68
68
|
export declare class ComposableController<ComposableControllerState extends LegacyComposableControllerStateConstraint, ChildControllers extends Record<string, ControllerInstance>> extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger<ComposableControllerState>> {
|
69
69
|
#private;
|
@@ -71,7 +71,7 @@ export declare class ComposableController<ComposableControllerState extends Lega
|
|
71
71
|
* Creates a ComposableController instance.
|
72
72
|
*
|
73
73
|
* @param options - Initial options used to configure this controller
|
74
|
-
* @param options.controllers - An object
|
74
|
+
* @param options.controllers - An object that contains child controllers keyed by their names.
|
75
75
|
* @param options.messenger - A restricted controller messenger.
|
76
76
|
*/
|
77
77
|
constructor({ controllers, messenger, }: {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":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AAC,cAAc,EAAE,eAAe,CAAC,GAE7D,eAAe,SAAS,iBAAiB,GACvC,gCAAgC,CAAC,cAAc,EAAE,eAAe,CAAC,GACjE,KAAK,GACP,KAAK,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,aAAa,CACvB,yBAAyB,SAAS,mCAAmC,IACnE,gCAAgC,CAAC,yBAAyB,CAAC,CAAC;AAEhE;;;;GAIG;AACH,MAAM,MAAM,6BAA6B,CACvC,yBAAyB,SAAS,mCAAmC,IACnE,6BAA6B,CAC/B,OAAO,cAAc,EACrB,KAAK,EACH,0BAA0B,CAAC,yBAAyB,CAAC,GACrD,aAAa,CAAC,yBAAyB,CAAC,EAC1C,KAAK,EACL,aAAa,CAAC,yBAAyB,CAAC,CAAC,MAAM,CAAC,CACjD,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,oBAAoB,CAC/B,yBAAyB,SAAS,yCAAyC,EAC3E,gBAAgB,SAAS,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAC3D,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBACS,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,gBAAgB,CAAC;QAC9B,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;
|
1
|
+
{"version":3,"file":"ComposableController.d.mts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,eAAe,EACf,iBAAiB,EAGjB,0BAA0B,EAC1B,+BAA+B,EAC/B,kBAAkB,EACnB,kCAAkC;AACnC,OAAO,EACL,cAAc,EAGf,kCAAkC;AACnC,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc;AAEnC,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAErD,eAAO,MAAM,wBAAwB,mHAC6E,CAAC;AAEnH;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AK,EACH,0BAA0B,CAAC,yBAAyB,CAAC,GACrD,aAAa,CAAC,yBAAyB,CAAC,EAC1C,KAAK,EACL,aAAa,CAAC,yBAAyB,CAAC,CAAC,MAAM,CAAC,CACjD,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,oBAAoB,CAC/B,yBAAyB,SAAS,yCAAyC,EAC3E,gBAAgB,SAAS,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAC3D,SAAQ,cAAc,CACtB,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAAC,yBAAyB,CAAC,CACzD;;IACC;;;;;;OAMG;gBACS,EACV,WAAW,EACX,SAAS,GACV,EAAE;QACD,WAAW,EAAE,gBAAgB,CAAC;QAC9B,SAAS,EAAE,6BAA6B,CAAC,yBAAyB,CAAC,CAAC;KACrE;CA+EF;AAED,eAAe,oBAAoB,CAAC"}
|
@@ -11,14 +11,14 @@ export const INVALID_CONTROLLER_ERROR = 'Invalid controller: controller must hav
|
|
11
11
|
* Controller that composes multiple child controllers and maintains up-to-date composed state.
|
12
12
|
*
|
13
13
|
* @template ComposableControllerState - A type object containing the names and state types of the child controllers.
|
14
|
-
* @template ChildControllers - A
|
14
|
+
* @template ChildControllers - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.
|
15
15
|
*/
|
16
16
|
export class ComposableController extends BaseController {
|
17
17
|
/**
|
18
18
|
* Creates a ComposableController instance.
|
19
19
|
*
|
20
20
|
* @param options - Initial options used to configure this controller
|
21
|
-
* @param options.controllers - An object
|
21
|
+
* @param options.controllers - An object that contains child controllers keyed by their names.
|
22
22
|
* @param options.messenger - A restricted controller messenger.
|
23
23
|
*/
|
24
24
|
constructor({ controllers, messenger, }) {
|
@@ -27,38 +27,37 @@ export class ComposableController extends BaseController {
|
|
27
27
|
}
|
28
28
|
super({
|
29
29
|
name: controllerName,
|
30
|
-
metadata: Object.
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
}
|
30
|
+
metadata: Object.keys(controllers).reduce((metadata, name) => {
|
31
|
+
// Type assertion is necessary to assign new properties to a generic type - ts(2862)
|
32
|
+
metadata[name] = {
|
33
|
+
persist: true,
|
34
|
+
anonymous: true,
|
35
|
+
};
|
42
36
|
return metadata;
|
43
37
|
}, {}),
|
44
|
-
state: Object.
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
state[name] =
|
49
|
-
controller.state;
|
50
|
-
}
|
38
|
+
state: Object.values(controllers).reduce((state, controller) => {
|
39
|
+
// Type assertion is necessary to assign new properties to a generic type - ts(2862)
|
40
|
+
// TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.
|
41
|
+
state[controller.name] = controller.state;
|
51
42
|
return state;
|
52
43
|
}, {}),
|
53
44
|
messenger,
|
54
45
|
});
|
55
46
|
_ComposableController_instances.add(this);
|
56
|
-
Object.
|
47
|
+
Object.values(controllers).forEach((controller) => {
|
48
|
+
__classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controller);
|
49
|
+
});
|
57
50
|
}
|
58
51
|
}
|
59
52
|
_ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) {
|
60
53
|
const { name } = controller;
|
61
54
|
if (!isBaseController(controller) && !isBaseControllerV1(controller)) {
|
55
|
+
if (name in this.metadata && this.metadata[name]) {
|
56
|
+
delete this.metadata[name];
|
57
|
+
}
|
58
|
+
if (name in this.state && this.state[name]) {
|
59
|
+
delete this.state[name];
|
60
|
+
}
|
62
61
|
// False negative. `name` is a string type.
|
63
62
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
64
63
|
throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);
|
@@ -69,7 +68,8 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
|
|
69
68
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
70
69
|
`${name}:stateChange`, (childState) => {
|
71
70
|
this.update((state) => {
|
72
|
-
|
71
|
+
state[name] =
|
72
|
+
childState;
|
73
73
|
});
|
74
74
|
});
|
75
75
|
}
|
@@ -81,7 +81,7 @@ _ComposableController_instances = new WeakSet(), _ComposableController_updateChi
|
|
81
81
|
if (isBaseControllerV1(controller)) {
|
82
82
|
controller.subscribe((childState) => {
|
83
83
|
this.update((state) => {
|
84
|
-
|
84
|
+
state[name] = childState;
|
85
85
|
});
|
86
86
|
});
|
87
87
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"ComposableController.mjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;AAUA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EACnB,kCAAkC;AAGnC,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,CAAC,MAAM,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAM,OAAO,oBAGX,SAAQ,cAIT;IACC;;;;;;OAMG;IACH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QAED,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,MAAM,CAE1C,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,EAAE,EAAE;gBACjC,IAAI,gBAAgB,CAAC,UAAU,CAAC,EAAE;oBAChC,wFAAwF;oBACvF,QAA+D,CAC9D,IAAI,CACL,GAAG,UAAU,CAAC,QAAQ,CAAC;iBACzB;qBAAM,I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AAM,wBAAwB,EAAE,CAAC,CAAC;KAC1D;IACD,IAAI;QACF,IAAI,CAAC,eAAe,CAAC,SAAS;QAC5B,2CAA2C;QAC3C,4EAA4E;QAC5E,GAAG,IAAI,cAAc,EACrB,CAAC,UAA2C,EAAE,EAAE;YAC9C,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;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 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"]}
|
1
|
+
{"version":3,"file":"ComposableController.mjs","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;;;;AAUA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EACnB,kCAAkC;AAGnC,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,CAAC,MAAM,wBAAwB,GACnC,gHAAgH,CAAC;AAyGnH;;;;;GAKG;AACH,MAAM,OAAO,oBAGX,SAAQ,cAIT;IACC;;;;;;OAMG;IACH,YAAY,EACV,WAAW,EACX,SAAS,GAIV;QACC,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;SACjD;QAED,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,CAEvC,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE;gBACnB,oFAAoF;gBACnF,QAA4D,CAAC,IAAI,CAAC,GAAG;oBACpE,OAAO,EAAE,IAAI;oBACb,SAAS,EAAE,IAAI;iBAChB,CAAC;gBACF,OAAO,QAAQ,CAAC;YAClB,CAAC,EAAE,EAAW,CAAC;YACf,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,MAAM,CACtC,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;gBACpB,oFAAoF;gBACpF,yGAAyG;gBACxG,KAAmD,CAClD,UAAU,CAAC,IAAI,CAChB,GAAG,UAAU,CAAC,KAAK,CAAC;gBACrB,OAAO,KAAK,CAAC;YACf,CAAC,EACD,EAAW,CACZ;YACD,SAAS;SACV,CAA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AAkB,CAAC,UAAU,CAAC,EAAE;QAClC,UAAU,CAAC,SAAS,CAAC,CAAC,UAA6B,EAAE,EAAE;YACrD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACnB,KAA2C,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC;YAClE,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;AAGH,eAAe,oBAAoB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n StateConstraint,\n StateConstraintV1,\n StateMetadata,\n StatePropertyMetadataConstraint,\n ControllerStateChangeEvent,\n LegacyControllerStateConstraint,\n ControllerInstance,\n} from '@metamask/base-controller';\nimport {\n BaseController,\n isBaseController,\n isBaseControllerV1,\n} from '@metamask/base-controller';\nimport type { Patch } from 'immer';\n\nexport const controllerName = 'ComposableController';\n\nexport const INVALID_CONTROLLER_ERROR =\n 'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.';\n\n/**\n * A universal supertype for the composable controller state object.\n *\n * This type is only intended to be used for disabling the generic constraint on the `ControllerState` type argument in the `BaseController` type as a temporary solution for ensuring compatibility with BaseControllerV1 child controllers.\n * Note that it is unsuitable for general use as a type constraint.\n */\n// TODO: Replace with `ComposableControllerStateConstraint` once BaseControllerV2 migrations are completed for all controllers.\ntype LegacyComposableControllerStateConstraint = {\n // `any` is used here to disable the generic constraint on the `ControllerState` type argument in the `BaseController` type,\n // enabling composable controller state types with BaseControllerV1 state objects to be.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [name: string]: Record<string, any>;\n};\n\n/**\n * The narrowest supertype for the composable controller state object.\n * This is also a widest subtype of the 'LegacyComposableControllerStateConstraint' type.\n */\n// TODO: Replace with `{ [name: string]: StateConstraint }` once BaseControllerV2 migrations are completed for all controllers.\nexport type ComposableControllerStateConstraint = {\n [name: string]: LegacyControllerStateConstraint;\n};\n\n/**\n * A `stateChange` event for any controller instance that extends from either `BaseControllerV1` or `BaseControllerV2`.\n */\n// TODO: Replace all instances with `ControllerStateChangeEvent` once `BaseControllerV2` migrations are completed for all controllers.\ntype LegacyControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraintV1,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\n/**\n * The `stateChange` event type for the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerStateChangeEvent<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = LegacyControllerStateChangeEvent<\n typeof controllerName,\n ComposableControllerState\n>;\n\n/**\n * A union type of internal event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerStateChangeEvent<ComposableControllerState>;\n\n/**\n * A utility type that extracts controllers from the {@link ComposableControllerState} type,\n * and derives a union type of all of their corresponding `stateChange` events.\n *\n * This type can handle both `BaseController` and `BaseControllerV1` controller instances.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ChildControllerStateChangeEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ComposableControllerState extends Record<\n infer ControllerName extends string,\n infer ControllerState\n>\n ? ControllerState extends StateConstraint\n ? ControllerStateChangeEvent<ControllerName, ControllerState>\n : // TODO: Remove this conditional branch once `BaseControllerV2` migrations are completed for all controllers.\n ControllerState extends StateConstraintV1\n ? LegacyControllerStateChangeEvent<ControllerName, ControllerState>\n : never\n : never;\n\n/**\n * A union type of external event types available to the {@link ComposableControllerMessenger}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type AllowedEvents<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = ChildControllerStateChangeEvents<ComposableControllerState>;\n\n/**\n * The messenger of the {@link ComposableController}.\n *\n * @template ComposableControllerState - A type object that maps controller names to their state types.\n */\nexport type ComposableControllerMessenger<\n ComposableControllerState extends ComposableControllerStateConstraint,\n> = RestrictedControllerMessenger<\n typeof controllerName,\n never,\n | ComposableControllerEvents<ComposableControllerState>\n | AllowedEvents<ComposableControllerState>,\n never,\n AllowedEvents<ComposableControllerState>['type']\n>;\n\n/**\n * Controller that composes multiple child controllers and maintains up-to-date composed state.\n *\n * @template ComposableControllerState - A type object containing the names and state types of the child controllers.\n * @template ChildControllers - A type object that specifies the child controllers which are used to instantiate the {@link ComposableController}.\n */\nexport class ComposableController<\n ComposableControllerState extends LegacyComposableControllerStateConstraint,\n ChildControllers extends Record<string, ControllerInstance>,\n> extends BaseController<\n typeof controllerName,\n ComposableControllerState,\n ComposableControllerMessenger<ComposableControllerState>\n> {\n /**\n * Creates a ComposableController instance.\n *\n * @param options - Initial options used to configure this controller\n * @param options.controllers - An object that contains child controllers keyed by their names.\n * @param options.messenger - A restricted controller messenger.\n */\n constructor({\n controllers,\n messenger,\n }: {\n controllers: ChildControllers;\n messenger: ComposableControllerMessenger<ComposableControllerState>;\n }) {\n if (messenger === undefined) {\n throw new Error(`Messaging system is required`);\n }\n\n super({\n name: controllerName,\n metadata: Object.keys(controllers).reduce<\n StateMetadata<ComposableControllerState>\n >((metadata, name) => {\n // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n (metadata as Record<string, StatePropertyMetadataConstraint>)[name] = {\n persist: true,\n anonymous: true,\n };\n return metadata;\n }, {} as never),\n state: Object.values(controllers).reduce<ComposableControllerState>(\n (state, controller) => {\n // Type assertion is necessary to assign new properties to a generic type - ts(2862)\n // TODO: Remove the 'object' member once `BaseControllerV2` migrations are completed for all controllers.\n (state as LegacyComposableControllerStateConstraint)[\n controller.name\n ] = controller.state;\n return state;\n },\n {} as never,\n ),\n messenger,\n });\n\n Object.values(controllers).forEach((controller) => {\n this.#updateChildController(controller);\n });\n }\n\n /**\n * Constructor helper that subscribes to child controller state changes.\n *\n * @param controller - Controller instance to update\n */\n #updateChildController(controller: ControllerInstance): void {\n const { name } = controller;\n if (!isBaseController(controller) && !isBaseControllerV1(controller)) {\n if (name in this.metadata && this.metadata[name]) {\n delete this.metadata[name];\n }\n if (name in this.state && this.state[name]) {\n delete this.state[name];\n }\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`${name} - ${INVALID_CONTROLLER_ERROR}`);\n }\n try {\n this.messagingSystem.subscribe(\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${name}:stateChange`,\n (childState: LegacyControllerStateConstraint) => {\n this.update((state) => {\n (state as LegacyComposableControllerStateConstraint)[name] =\n childState;\n });\n },\n );\n } catch (error: unknown) {\n // False negative. `name` is a string type.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n console.error(`${name} - ${String(error)}`);\n }\n if (isBaseControllerV1(controller)) {\n controller.subscribe((childState: StateConstraintV1) => {\n this.update((state) => {\n (state as Record<string, StateConstraintV1>)[name] = childState;\n });\n });\n }\n }\n}\n\nexport default ComposableController;\n"]}
|
package/package.json
CHANGED