-
-
Notifications
You must be signed in to change notification settings - Fork 187
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] Enable handling of non-controller input #4943
base: main
Are you sure you want to change the base?
[composable-controller] Enable handling of non-controller input #4943
Conversation
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
@@ -16,8 +15,18 @@ import type { Patch } from 'immer'; | |||
|
|||
export const controllerName = 'ComposableController'; | |||
|
|||
export const INVALID_CONTROLLER_ERROR = | |||
'Invalid controller: controller must have a `messagingSystem` or be a class inheriting from `BaseControllerV1`.'; | |||
// TODO: Replace with type guard once superclass for messageable non-controllers has been defined. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the goal is to figure out which controllers do not have state, can we check for the absence of the state
property?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately only AssetsContractController
lacks a state
property.
NftDetectionController
and TokenDetectionController
both have a state
property which is assigned an empty object of type Record<never, never>
, because both inherit from BaseController
.
Checking for an empty state object is not useful because there could be controllers with non-empty state that are initialized with empty state objects.
These inconsistencies make the changes in this PR less type-safe than they could be. This ties in to one of the motivations for the MessengerClient/Messageable ADR. which is to align inheritance for stateless non-controllers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. My thought is that we could clean this up by assuming that the non-controllers do not inherit from BaseController. I forgot that we will do that through the MessengerClient/Messageable ADR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's currently no common supertype for the stateless non-controllers that is narrower than an object with a 'name' property.
Ideally, we would be able to exclude all classes with properties 'name', 'messagingSystem' and without 'state', 'metadata', but since that's currently not an option until the ADR is implemented, I went for just explicitly specifying the non-controllers, so we wouldn't have to deal with edge cases in the input.
I refactored the logic a bit. Hopefully this makes the intention a bit clearer: 85389bb
TODO: move to `@metamask/base-controller` once we have a type and type guard for stateless non-controllers
References
ComposableController
API updates that fix broken functionality metamask-mobile#10441Changelog
@metamask/composable-controller
Changed
ComposableController
controller optioncontrollers
and generic type parameterChildControllers
to accept all objects with a 'name' property. Passing in non-controller input no longer produces a runtime error.stateChange
event subscription operation will exclude non-controller input.ComposableControllerState
only accepts controllers with a 'state' property.isBaseController
,isBaseControllerV1
fromControllerInstance
tounknown
.Checklist