Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Feb 23, 2024
1 parent b35b77a commit 0c830ba
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 20 deletions.
1 change: 0 additions & 1 deletion packages/composable-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- **BREAKING:** Passing a non-controller into `controllers` constructor option now throws an error ([#3904](https://github.com/MetaMask/core/pull/3904))
- **BREAKING:** The `AllowedActions` parameter of the `ComposableControllerMessenger` type is narrowed from `string` to `never`, as `ComposableController` does not use any external controller actions. ([#3904](https://github.com/MetaMask/core/pull/3904))
- Add `@metamask/utils` ^8.3.0 as a dependency. ([#3904](https://github.com/MetaMask/core/pull/3904))

### Removed

Expand Down
3 changes: 1 addition & 2 deletions packages/composable-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/base-controller": "^4.1.1",
"@metamask/utils": "^8.3.0"
"@metamask/base-controller": "^4.1.1"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
Expand Down
25 changes: 9 additions & 16 deletions packages/composable-controller/src/ComposableController.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { BaseController, BaseControllerV1 } from '@metamask/base-controller';
import type {
RestrictedControllerMessenger,
BaseState,
BaseConfig,
BaseState,
RestrictedControllerMessenger,
StateMetadata,
StateConstraint,
} from '@metamask/base-controller';
import { isValidJson } from '@metamask/utils';
import type { Patch } from 'immer';

export const controllerName = 'ComposableController';

// TODO: Remove this type once `BaseControllerV2` migrations are completed for all controllers.
/**
* A type encompassing all controller instances that extend from `BaseControllerV1`.
*/
Expand All @@ -26,14 +24,13 @@ export type BaseControllerV1Instance =
* The `BaseController` class itself can't be used directly as a type representing all of its subclasses,
* because the generic parameters it expects require knowing the exact shape of the controller's state and messenger.
*
* Instead, we look for an object with the `BaseController` properties that we use in the ComposableController (name and state).
* Instead, we look for an object with the `BaseController` properties that we use in the ComposableController (name, state).
*/
export type BaseControllerV2Instance = {
name: string;
state: StateConstraint;
};

// TODO: Remove `BaseControllerV1Instance` member once `BaseControllerV2` migrations are completed for all controllers.
/**
* A type encompassing all controller instances that extend from `BaseControllerV1` or `BaseController`.
*/
Expand All @@ -45,7 +42,6 @@ export type ControllerInstance =
* Determines if the given controller is an instance of BaseControllerV1
* @param controller - Controller instance to check
* @returns True if the controller is an instance of BaseControllerV1
* TODO: Deprecate once `BaseControllerV2` migrations are completed for all controllers.
*/
export function isBaseControllerV1(
controller: ControllerInstance,
Expand Down Expand Up @@ -83,10 +79,10 @@ export function isBaseController(
);
}

// TODO: Replace `any` with `Json` once `BaseControllerV2` migrations are completed for all controllers.
export type ComposableControllerState = {
// `any` is used here to disable the `BaseController` type constraint which expects state properties to extend `Record<string, Json>`.
// `ComposableController` state needs to accommodate `BaseControllerV1` state objects that may have properties wider than `Json`.
// TODO: Replace `any` with `Json` once `BaseControllerV2` migrations are completed for all controllers.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[name: string]: Record<string, any>;
};
Expand Down Expand Up @@ -146,7 +142,7 @@ export class ComposableController extends BaseController<
(metadata, controller) => ({
...metadata,
[controller.name]: isBaseController(controller)
? controller.metadata
? (controller as BaseController<never, never, never>).metadata
: { persist: true, anonymous: true },
}),
{},
Expand All @@ -168,7 +164,6 @@ export class ComposableController extends BaseController<
/**
* Constructor helper that subscribes to child controller state changes.
* @param controller - Controller instance to update
* TODO: Remove `isBaseControllerV1` branch once `BaseControllerV2` migrations are completed for all controllers.
*/
#updateChildController(controller: ControllerInstance): void {
const { name } = controller;
Expand All @@ -183,12 +178,10 @@ export class ComposableController extends BaseController<
this.messagingSystem.subscribe(
`${name}:stateChange`,
(childState: StateConstraint) => {
if (isValidJson(childState)) {
this.update((state) => ({
...state,
[name]: childState,
}));
}
this.update((state) => ({
...state,
[name]: childState,
}));
},
);
} else {
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,6 @@ __metadata:
"@metamask/auto-changelog": ^3.4.4
"@metamask/base-controller": ^4.1.1
"@metamask/json-rpc-engine": ^7.3.2
"@metamask/utils": ^8.3.0
"@types/jest": ^27.4.1
deepmerge: ^4.2.2
immer: ^9.0.6
Expand Down

0 comments on commit 0c830ba

Please sign in to comment.