-
-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[composable-controller] Make class and messenger generic upon child controllers #3627
Comments
ComposableController
typing for child controllers and allow listsComposableController
polymorphic upon child controllers
ComposableController
polymorphic upon child controllersComposableController
polymorphic upon its child controllers, and improve typing for messenger allow lists
ComposableController
polymorphic upon its child controllers, and improve typing for messenger allow lists
Hey team! Please add your planning poker estimate with Zenhub @Gudahtt @kanthesha @MajorLift @mcmire @mikesposito |
One approach we could take is to add the types listed in requirements as generic parameters to the ComposableController class and compute them in place: export class ComposableController<
ChildControllers extends ControllerInstance[],
ComposedState extends ComposableControllerState = T1<ChildControllers>,
ComposedStateChangeEvent = ControllerStateChangeEvent<'ComposableController', ComposedState>,
ChildStateChangeEvents extends EventConstraint & { type: `${string}:stateChange` } = T2<ChildControllers>,
ComposedControllerMessenger = RestrictedControllerMessenger<
'ComposableController',
never,
ComposedStateChangeEvent | ChildStateChangeEvents,
never,
ChildStateChangeEvents['type']
>,
> extends BaseController<
'ComposableController',
ComposedState,
ComposedControllerMessenger
> {
constructor({
controllers,
messenger,
}: {
controllers: ControllerList;
messenger: ComposedControllerMessenger;
}) {}
}
|
…pe checks for inputs, remove `#controllers` field (#3904) ## Explanation There are several issues with the current ComposableController implementation that should be addressed before further updates are made to the controller (e.g. #3627). - Removes `#controllers` class field, which is not being updated by `#updateChildController` or anywhere else. - Removing this makes it clear that the list of child controllers to be composed is determined at class instantiation and cannot be altered later. - This behavior is consistent with `#updateChildController` being a private method. - Types BaseController, ComposableController state with `Record<string, Json>` - Opted to use `any` to disable BaseController state type constraint, as there is no straightforward way to type `BaseControllerV1` state to be compatible with `Json`. - Adds a `isBaseController` type guard, ~and removes the deprecated `subscribed` property from `BaseController`~. - Removes `ControllerList` type in anticipation of #3627. Internally, `ControllerInstance` will be used to type child controllers or unions and tuples thereof. - Adds and exports `ControllerInstance`, `BaseControllerV{1,2}Instance` types. - Populates state metadata object in constructor with child controllers. ## References - Blocks #3627 - Closes #3716 - Replaces some TODOs with comment explaining necessity of `any` usage. - Closes #3907 ## Changelog Recorded under "Unreleased" heading in CHANGELOG files. ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate --------- Co-authored-by: Elliot Winkler <[email protected]>
@MajorLift How would you feel about changing the estimate on this reflect the actual complexity? And just wondering what the next steps on this one are? |
…izing over child controllers list (#3952) ## Explanation Currently, the allow lists of `ComposableControllerMessenger` are set to `string`, which is too permissive. Each `ComposableController` instance needs typing and allow lists that are specific to its set of input child controllers. To achieve this, the `ComposableController` class and its messenger are made polymorphic upon the `ControllerState` type, which defines the shape of the composed state. ## References - Closes #3627 ## Changelog ### [`@metamask/composable-controller`](https://github.com/MetaMask/core/pull/3952/files#diff-9745631a8a7023c408b4f9b96dc8c0eaa9a41599e8d93eb470bb268f1fcc75ff) ### Added - Adds and exports new types: ([#3952](#3952)) - `RestrictedControllerMessengerConstraint`, which is the narrowest supertype of all controller-messenger instances. - `LegacyControllerStateConstraint`, a universal supertype for the controller state object, encompassing both BaseControllerV1 and BaseControllerV2 state. - `ComposableControllerStateConstraint`, the narrowest supertype for the composable controller state object. ### Changed - **BREAKING:** The `ComposableController` class is now a generic class that expects one generic argument `ComposableControllerState` ([#3952](#3952)). - **BREAKING:** For the `ComposableController` class to be typed correctly, any of its child controllers that extend `BaseControllerV1` must have an overridden `name` property that is defined using the `as const` assertion. ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate
Explanation
Currently, the allow lists of
ComposableControllerMessenger
are set tostring
, which is too permissive. EachComposableController
instance needs typing and allow lists that are specific to its set of input child controllers.However, there's no simple way to achieve this using
BaseController
, as controller-messenger allow lists are defined for the controller class singleton, not controller instances.Instead,
ComposableController
andComposableControllerMessenger
need to be made polymorphic upon thecontrollers
constructor property, which represents the list of child controllers to be composed.Requirements
ControllerList
(or maybeChildControllers
) needs to be added to theComposableController
class andComposableControllerMessenger
type.ControllerList
should be constrained, but not defined by the least supertype for all base-controller instances (currentlyControllerInstance
).ControllerList
should be used to derive the return type offlatState()
at the type level.ControllerList
should be used to derive the state type forComposableController
instances.ControllerList
should be used to derive the allow lists ofComposableControllerMessenger
.stateChange
events for all child controllers that are subscribed to in the#updateChildController()
method.References
ComposableController
to extendBaseControllerV2
#3590 (comment)The text was updated successfully, but these errors were encountered: