@metamask-previews/composable-controller
Advanced tools
Comparing version 4.0.0-preview.3fbb6b41 to 5.0.0-preview.353ee83b
@@ -9,2 +9,13 @@ # Changelog | ||
## [5.0.0] | ||
### Added | ||
- Add types `ComposableControllerState`, `ComposableControllerStateChangeEvent`, `ComposableControllerEvents`, `ComposableControllerMessenger` ([#3590](https://github.com/MetaMask/core/pull/3590)) | ||
### Changed | ||
- **BREAKING:** `ComposableController` is upgraded to extend `BaseControllerV2` ([#3590](https://github.com/MetaMask/core/pull/3590)) | ||
- The constructor now expects an options object with required properties `controllers` and `messenger` as its only argument. | ||
- `ComposableController` no longer has a `subscribe` method. Instead, listeners for `ComposableController` events must be registered to the controller messenger that generated the restricted messenger assigned to the instance's `messagingSystem` class field. | ||
- Any getters for `ComposableController` state that access the internal class field directly should be refactored to instead use listeners that are subscribed to `ComposableControllerStateChangeEvent`. | ||
- Bump `@metamask/base-controller` to `^4.0.1` ([#3695](https://github.com/MetaMask/core/pull/3695)) | ||
## [4.0.0] | ||
@@ -54,3 +65,4 @@ ### Changed | ||
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@4.0.0...HEAD | ||
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@5.0.0...HEAD | ||
[5.0.0]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@4.0.0...@metamask/composable-controller@5.0.0 | ||
[4.0.0]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@3.0.3...@metamask/composable-controller@4.0.0 | ||
@@ -57,0 +69,0 @@ [3.0.3]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@3.0.2...@metamask/composable-controller@3.0.3 |
@@ -1,34 +0,34 @@ | ||
import type { RestrictedControllerMessenger } from '@metamask/base-controller'; | ||
import { BaseControllerV1 } from '@metamask/base-controller'; | ||
import { BaseController, BaseControllerV1 } from '@metamask/base-controller'; | ||
import type { ControllerStateChangeEvent, RestrictedControllerMessenger } from '@metamask/base-controller'; | ||
export declare const controllerName = "ComposableController"; | ||
declare type ControllerInstance = BaseControllerV1<any, any> | { | ||
name: string; | ||
state: Record<string, unknown>; | ||
}; | ||
/** | ||
* List of child controller instances | ||
* | ||
* This type encompasses controllers based up either BaseControllerV1 or | ||
* BaseController. The BaseController type can't be included directly | ||
* because the generic parameters it expects require knowing the exact state | ||
* shape, so instead we look for an object with the BaseController properties | ||
* that we use in the ComposableController (name and state). | ||
*/ | ||
export declare type ControllerList = (BaseControllerV1<any, any> | { | ||
name: string; | ||
state: Record<string, unknown>; | ||
})[]; | ||
export declare type ComposableControllerRestrictedMessenger = RestrictedControllerMessenger<'ComposableController', never, any, never, any>; | ||
export declare type ControllerList = ControllerInstance[]; | ||
export declare type ComposableControllerState = { | ||
[name: string]: ControllerInstance['state']; | ||
}; | ||
export declare type ComposableControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, ComposableControllerState>; | ||
export declare type ComposableControllerEvents = ComposableControllerStateChangeEvent; | ||
export declare type ComposableControllerMessenger = RestrictedControllerMessenger<typeof controllerName, never, ControllerStateChangeEvent<string, Record<string, unknown>>, string, string>; | ||
/** | ||
* Controller that can be used to compose multiple controllers together. | ||
*/ | ||
export declare class ComposableController extends BaseControllerV1<never, any> { | ||
private readonly controllers; | ||
private readonly messagingSystem?; | ||
export declare class ComposableController extends BaseController<typeof controllerName, ComposableControllerState, ComposableControllerMessenger> { | ||
#private; | ||
/** | ||
* Name of this controller used during composition | ||
*/ | ||
name: string; | ||
/** | ||
* Creates a ComposableController instance. | ||
* | ||
* @param controllers - Map of names to controller instances. | ||
* @param messenger - The controller messaging system, used for communicating with BaseController controllers. | ||
* @param options - Initial options used to configure this controller | ||
* @param options.controllers - List of child controller instances to compose. | ||
* @param options.messenger - A restricted controller messenger. | ||
*/ | ||
constructor(controllers: ControllerList, messenger?: ComposableControllerRestrictedMessenger); | ||
constructor({ controllers, messenger, }: { | ||
controllers: ControllerList; | ||
messenger: ComposableControllerMessenger; | ||
}); | ||
/** | ||
@@ -35,0 +35,0 @@ * Flat state representation, one that isn't keyed |
"use strict"; | ||
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
if (kind === "m") throw new TypeError("Private method is not writable"); | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); | ||
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; | ||
}; | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
}; | ||
var _ComposableController_instances, _ComposableController_controllers, _ComposableController_updateChildController; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ComposableController = void 0; | ||
exports.ComposableController = exports.controllerName = void 0; | ||
const base_controller_1 = require("@metamask/base-controller"); | ||
exports.controllerName = 'ComposableController'; | ||
/** | ||
* 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 | ||
*/ | ||
function isBaseControllerV1(controller) { | ||
return controller instanceof base_controller_1.BaseControllerV1; | ||
} | ||
/** | ||
* Controller that can be used to compose multiple controllers together. | ||
*/ | ||
class ComposableController extends base_controller_1.BaseControllerV1 { | ||
class ComposableController extends base_controller_1.BaseController { | ||
/** | ||
* Creates a ComposableController instance. | ||
* | ||
* @param controllers - Map of names to controller instances. | ||
* @param messenger - The controller messaging system, used for communicating with BaseController controllers. | ||
* @param options - Initial options used to configure this controller | ||
* @param options.controllers - List of child controller instances to compose. | ||
* @param options.messenger - A restricted controller messenger. | ||
*/ | ||
constructor(controllers, messenger) { | ||
super(undefined, controllers.reduce((state, controller) => { | ||
state[controller.name] = controller.state; | ||
return state; | ||
}, {})); | ||
this.controllers = []; | ||
/** | ||
* Name of this controller used during composition | ||
*/ | ||
this.name = 'ComposableController'; | ||
this.initialize(); | ||
this.controllers = controllers; | ||
this.messagingSystem = messenger; | ||
this.controllers.forEach((controller) => { | ||
const { name } = controller; | ||
if (controller.subscribe !== undefined) { | ||
controller.subscribe((state) => { | ||
this.update({ [name]: state }); | ||
}); | ||
} | ||
else if (this.messagingSystem) { | ||
this.messagingSystem.subscribe(`${name}:stateChange`, (state) => { | ||
this.update({ [name]: state }); | ||
}); | ||
} | ||
else { | ||
throw new Error(`Messaging system required if any BaseController controllers are used`); | ||
} | ||
constructor({ controllers, messenger, }) { | ||
if (messenger === undefined) { | ||
throw new Error(`Messaging system is required`); | ||
} | ||
super({ | ||
name: exports.controllerName, | ||
metadata: {}, | ||
state: controllers.reduce((state, controller) => { | ||
return Object.assign(Object.assign({}, state), { [controller.name]: controller.state }); | ||
}, {}), | ||
messenger, | ||
}); | ||
_ComposableController_instances.add(this); | ||
_ComposableController_controllers.set(this, []); | ||
__classPrivateFieldSet(this, _ComposableController_controllers, controllers, "f"); | ||
__classPrivateFieldGet(this, _ComposableController_controllers, "f").forEach((controller) => __classPrivateFieldGet(this, _ComposableController_instances, "m", _ComposableController_updateChildController).call(this, controller)); | ||
} | ||
@@ -54,3 +63,3 @@ /** | ||
let flatState = {}; | ||
for (const controller of this.controllers) { | ||
for (const controller of __classPrivateFieldGet(this, _ComposableController_controllers, "f")) { | ||
flatState = Object.assign(Object.assign({}, flatState), controller.state); | ||
@@ -62,3 +71,16 @@ } | ||
exports.ComposableController = ComposableController; | ||
_ComposableController_controllers = new WeakMap(), _ComposableController_instances = new WeakSet(), _ComposableController_updateChildController = function _ComposableController_updateChildController(controller) { | ||
const { name } = controller; | ||
if (isBaseControllerV1(controller)) { | ||
controller.subscribe((childState) => { | ||
this.update((state) => (Object.assign(Object.assign({}, state), { [name]: childState }))); | ||
}); | ||
} | ||
else { | ||
this.messagingSystem.subscribe(`${String(name)}:stateChange`, (childState) => { | ||
this.update((state) => (Object.assign(Object.assign({}, state), { [name]: childState }))); | ||
}); | ||
} | ||
}; | ||
exports.default = ComposableController; | ||
//# sourceMappingURL=ComposableController.js.map |
@@ -1,2 +0,3 @@ | ||
export * from './ComposableController'; | ||
export type { ControllerList, ComposableControllerState, ComposableControllerStateChangeEvent, ComposableControllerEvents, ComposableControllerMessenger, } from './ComposableController'; | ||
export { ComposableController } from './ComposableController'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./ComposableController"), exports); | ||
exports.ComposableController = void 0; | ||
var ComposableController_1 = require("./ComposableController"); | ||
Object.defineProperty(exports, "ComposableController", { enumerable: true, get: function () { return ComposableController_1.ComposableController; } }); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@metamask-previews/composable-controller", | ||
"version": "4.0.0-preview.3fbb6b41", | ||
"version": "5.0.0-preview.353ee83b", | ||
"description": "Consolidates the state from multiple controllers into one", | ||
@@ -25,2 +25,3 @@ "keywords": [ | ||
"build:docs": "typedoc", | ||
"changelog:update": "../../scripts/update-changelog.sh @metamask/composable-controller", | ||
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/composable-controller", | ||
@@ -34,6 +35,6 @@ "publish:preview": "yarn npm publish --tag preview", | ||
"dependencies": { | ||
"@metamask/base-controller": "^4.0.0" | ||
"@metamask/base-controller": "^4.0.1" | ||
}, | ||
"devDependencies": { | ||
"@metamask/auto-changelog": "^3.4.3", | ||
"@metamask/auto-changelog": "^3.4.4", | ||
"@types/jest": "^27.4.1", | ||
@@ -40,0 +41,0 @@ "deepmerge": "^4.2.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22601
133
1149