@metamask-previews/composable-controller 3.0.0-preview.3dbf14f

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/CHANGELOG.md ADDED
@@ -0,0 +1,41 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [3.0.0]
10
+ ### Changed
11
+ - **BREAKING:** Bump to Node 16 ([#1262](https://github.com/MetaMask/core/pull/1262))
12
+
13
+ ## [2.0.0]
14
+ ### Removed
15
+ - **BREAKING:** Remove `isomorphic-fetch` ([#1106](https://github.com/MetaMask/controllers/pull/1106))
16
+ - Consumers must now import `isomorphic-fetch` or another polyfill themselves if they are running in an environment without `fetch`
17
+
18
+ ## [1.0.2]
19
+ ### Changed
20
+ - Rename this repository to `core` ([#1031](https://github.com/MetaMask/controllers/pull/1031))
21
+ - Update `@metamask/controller-utils` package ([#1041](https://github.com/MetaMask/controllers/pull/1041))
22
+
23
+ ## [1.0.1]
24
+ ### Changed
25
+ - Relax dependency on `@metamask/controller-utils` (use `^` instead of `~`) ([#998](https://github.com/MetaMask/core/pull/998))
26
+
27
+ ## [1.0.0]
28
+ ### Added
29
+ - Initial release
30
+ - As a result of converting our shared controllers repo into a monorepo ([#831](https://github.com/MetaMask/core/pull/831)), we've created this package from select parts of [`@metamask/controllers` v33.0.0](https://github.com/MetaMask/core/tree/v33.0.0), namely:
31
+ - `src/ComposableController.ts`
32
+ - `src/ComposableController.test.ts`
33
+
34
+ All changes listed after this point were applied to this package following the monorepo conversion.
35
+
36
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@3.0.0...HEAD
37
+ [3.0.0]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@2.0.0...@metamask/composable-controller@3.0.0
38
+ [2.0.0]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@1.0.2...@metamask/composable-controller@2.0.0
39
+ [1.0.2]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@1.0.1...@metamask/composable-controller@1.0.2
40
+ [1.0.1]: https://github.com/MetaMask/core/compare/@metamask/composable-controller@1.0.0...@metamask/composable-controller@1.0.1
41
+ [1.0.0]: https://github.com/MetaMask/core/releases/tag/@metamask/composable-controller@1.0.0
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 MetaMask
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # `@metamask/composable-controller`
2
+
3
+ Consolidates the state from multiple controllers into one.
4
+
5
+ ## Installation
6
+
7
+ `yarn add @metamask/composable-controller`
8
+
9
+ or
10
+
11
+ `npm install @metamask/composable-controller`
12
+
13
+ ## Contributing
14
+
15
+ This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
@@ -0,0 +1,44 @@
1
+ import type { RestrictedControllerMessenger } from '@metamask/base-controller';
2
+ import { BaseController } from '@metamask/base-controller';
3
+ /**
4
+ * List of child controller instances
5
+ *
6
+ * This type encompasses controllers based up either BaseController or
7
+ * BaseControllerV2. The BaseControllerV2 type can't be included directly
8
+ * because the generic parameters it expects require knowing the exact state
9
+ * shape, so instead we look for an object with the BaseControllerV2 properties
10
+ * that we use in the ComposableController (name and state).
11
+ */
12
+ export declare type ControllerList = (BaseController<any, any> | {
13
+ name: string;
14
+ state: Record<string, unknown>;
15
+ })[];
16
+ export declare type ComposableControllerRestrictedMessenger = RestrictedControllerMessenger<'ComposableController', never, any, never, any>;
17
+ /**
18
+ * Controller that can be used to compose multiple controllers together.
19
+ */
20
+ export declare class ComposableController extends BaseController<never, any> {
21
+ private readonly controllers;
22
+ private readonly messagingSystem?;
23
+ /**
24
+ * Name of this controller used during composition
25
+ */
26
+ name: string;
27
+ /**
28
+ * Creates a ComposableController instance.
29
+ *
30
+ * @param controllers - Map of names to controller instances.
31
+ * @param messenger - The controller messaging system, used for communicating with BaseControllerV2 controllers.
32
+ */
33
+ constructor(controllers: ControllerList, messenger?: ComposableControllerRestrictedMessenger);
34
+ /**
35
+ * Flat state representation, one that isn't keyed
36
+ * of controller name. Instead, all child controller state is merged
37
+ * together into a single, flat object.
38
+ *
39
+ * @returns Merged state representation of all child controllers.
40
+ */
41
+ get flatState(): {};
42
+ }
43
+ export default ComposableController;
44
+ //# sourceMappingURL=ComposableController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ComposableController.d.ts","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,2BAA2B,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D;;;;;;;;GAQG;AACH,oBAAY,cAAc,GAAG,CACzB,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC,GACxB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,CACnD,EAAE,CAAC;AAEJ,oBAAY,uCAAuC,GACjD,6BAA6B,CAAC,sBAAsB,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;AAEhF;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,cAAc,CAAC,KAAK,EAAE,GAAG,CAAC;IAClE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAsB;IAElD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAC,CAA0C;IAE3E;;OAEG;IACM,IAAI,SAA0B;IAEvC;;;;;OAKG;gBAED,WAAW,EAAE,cAAc,EAC3B,SAAS,CAAC,EAAE,uCAAuC;IAiCrD;;;;;;OAMG;IACH,IAAI,SAAS,OAMZ;CACF;AAED,eAAe,oBAAoB,CAAC"}
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ComposableController = void 0;
4
+ const base_controller_1 = require("@metamask/base-controller");
5
+ /**
6
+ * Controller that can be used to compose multiple controllers together.
7
+ */
8
+ class ComposableController extends base_controller_1.BaseController {
9
+ /**
10
+ * Creates a ComposableController instance.
11
+ *
12
+ * @param controllers - Map of names to controller instances.
13
+ * @param messenger - The controller messaging system, used for communicating with BaseControllerV2 controllers.
14
+ */
15
+ constructor(controllers, messenger) {
16
+ super(undefined, controllers.reduce((state, controller) => {
17
+ state[controller.name] = controller.state;
18
+ return state;
19
+ }, {}));
20
+ this.controllers = [];
21
+ /**
22
+ * Name of this controller used during composition
23
+ */
24
+ this.name = 'ComposableController';
25
+ this.initialize();
26
+ this.controllers = controllers;
27
+ this.messagingSystem = messenger;
28
+ this.controllers.forEach((controller) => {
29
+ const { name } = controller;
30
+ if (controller.subscribe !== undefined) {
31
+ controller.subscribe((state) => {
32
+ this.update({ [name]: state });
33
+ });
34
+ }
35
+ else if (this.messagingSystem) {
36
+ this.messagingSystem.subscribe(`${name}:stateChange`, (state) => {
37
+ this.update({ [name]: state });
38
+ });
39
+ }
40
+ else {
41
+ throw new Error(`Messaging system required if any BaseControllerV2 controllers are used`);
42
+ }
43
+ });
44
+ }
45
+ /**
46
+ * Flat state representation, one that isn't keyed
47
+ * of controller name. Instead, all child controller state is merged
48
+ * together into a single, flat object.
49
+ *
50
+ * @returns Merged state representation of all child controllers.
51
+ */
52
+ get flatState() {
53
+ let flatState = {};
54
+ for (const controller of this.controllers) {
55
+ flatState = Object.assign(Object.assign({}, flatState), controller.state);
56
+ }
57
+ return flatState;
58
+ }
59
+ }
60
+ exports.ComposableController = ComposableController;
61
+ exports.default = ComposableController;
62
+ //# sourceMappingURL=ComposableController.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ComposableController.js","sourceRoot":"","sources":["../src/ComposableController.ts"],"names":[],"mappings":";;;AACA,+DAA2D;AAmB3D;;GAEG;AACH,MAAa,oBAAqB,SAAQ,gCAA0B;IAUlE;;;;;OAKG;IACH,YACE,WAA2B,EAC3B,SAAmD;QAEnD,KAAK,CACH,SAAS,EACT,WAAW,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;YACvC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC;YAC1C,OAAO,KAAK,CAAC;QACf,CAAC,EAAE,EAAS,CAAC,CACd,CAAC;QAzBa,gBAAW,GAAmB,EAAE,CAAC;QAIlD;;WAEG;QACM,SAAI,GAAG,sBAAsB,CAAC;QAmBrC,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QACjC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;YACtC,MAAM,EAAE,IAAI,EAAE,GAAG,UAAU,CAAC;YAC5B,IAAK,UAAuC,CAAC,SAAS,KAAK,SAAS,EAAE;gBACnE,UAAuC,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE;oBAC3D,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;gBACjC,CAAC,CAAC,CAAC;aACJ;iBAAM,IAAI,IAAI,CAAC,eAAe,EAAE;gBAC9B,IAAI,CAAC,eAAe,CAAC,SAAiB,CACrC,GAAG,IAAI,cAAc,EACrB,CAAC,KAAU,EAAE,EAAE;oBACb,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;gBACjC,CAAC,CACF,CAAC;aACH;iBAAM;gBACL,MAAM,IAAI,KAAK,CACb,wEAAwE,CACzE,CAAC;aACH;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACH,IAAI,SAAS;QACX,IAAI,SAAS,GAAG,EAAE,CAAC;QACnB,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,WAAW,EAAE;YACzC,SAAS,mCAAQ,SAAS,GAAK,UAAU,CAAC,KAAK,CAAE,CAAC;SACnD;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AAjED,oDAiEC;AAED,kBAAe,oBAAoB,CAAC","sourcesContent":["import type { RestrictedControllerMessenger } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\n\n/**\n * List of child controller instances\n *\n * This type encompasses controllers based up either BaseController or\n * BaseControllerV2. The BaseControllerV2 type can't be included directly\n * because the generic parameters it expects require knowing the exact state\n * shape, so instead we look for an object with the BaseControllerV2 properties\n * that we use in the ComposableController (name and state).\n */\nexport type ControllerList = (\n | BaseController<any, any>\n | { name: string; state: Record<string, unknown> }\n)[];\n\nexport type ComposableControllerRestrictedMessenger =\n RestrictedControllerMessenger<'ComposableController', never, any, never, any>;\n\n/**\n * Controller that can be used to compose multiple controllers together.\n */\nexport class ComposableController extends BaseController<never, any> {\n private readonly controllers: ControllerList = [];\n\n private readonly messagingSystem?: ComposableControllerRestrictedMessenger;\n\n /**\n * Name of this controller used during composition\n */\n override name = 'ComposableController';\n\n /**\n * Creates a ComposableController instance.\n *\n * @param controllers - Map of names to controller instances.\n * @param messenger - The controller messaging system, used for communicating with BaseControllerV2 controllers.\n */\n constructor(\n controllers: ControllerList,\n messenger?: ComposableControllerRestrictedMessenger,\n ) {\n super(\n undefined,\n controllers.reduce((state, controller) => {\n state[controller.name] = controller.state;\n return state;\n }, {} as any),\n );\n this.initialize();\n this.controllers = controllers;\n this.messagingSystem = messenger;\n this.controllers.forEach((controller) => {\n const { name } = controller;\n if ((controller as BaseController<any, any>).subscribe !== undefined) {\n (controller as BaseController<any, any>).subscribe((state) => {\n this.update({ [name]: state });\n });\n } else if (this.messagingSystem) {\n (this.messagingSystem.subscribe as any)(\n `${name}:stateChange`,\n (state: any) => {\n this.update({ [name]: state });\n },\n );\n } else {\n throw new Error(\n `Messaging system required if any BaseControllerV2 controllers are used`,\n );\n }\n });\n }\n\n /**\n * Flat state representation, one that isn't keyed\n * of controller name. Instead, all child controller state is merged\n * together into a single, flat object.\n *\n * @returns Merged state representation of all child controllers.\n */\n get flatState() {\n let flatState = {};\n for (const controller of this.controllers) {\n flatState = { ...flatState, ...controller.state };\n }\n return flatState;\n }\n}\n\nexport default ComposableController;\n"]}
@@ -0,0 +1,2 @@
1
+ export * from './ComposableController';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./ComposableController"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC","sourcesContent":["export * from './ComposableController';\n"]}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@metamask-previews/composable-controller",
3
+ "version": "3.0.0-preview.3dbf14f",
4
+ "description": "Consolidates the state from multiple controllers into one",
5
+ "keywords": [
6
+ "MetaMask",
7
+ "Ethereum"
8
+ ],
9
+ "homepage": "https://github.com/MetaMask/core/tree/main/packages/composable-controller#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/MetaMask/core/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/MetaMask/core.git"
16
+ },
17
+ "license": "MIT",
18
+ "main": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "files": [
21
+ "dist/"
22
+ ],
23
+ "scripts": {
24
+ "build:docs": "typedoc",
25
+ "changelog:validate": "../../scripts/validate-changelog.sh @metamask/composable-controller",
26
+ "publish:preview": "yarn npm publish --tag preview",
27
+ "test": "jest",
28
+ "test:watch": "jest --watch"
29
+ },
30
+ "dependencies": {
31
+ "@metamask-previews/base-controller": "3.2.0-preview.3dbf14f"
32
+ },
33
+ "devDependencies": {
34
+ "@metamask/auto-changelog": "^3.1.0",
35
+ "@types/jest": "^27.4.1",
36
+ "deepmerge": "^4.2.2",
37
+ "immer": "^9.0.6",
38
+ "jest": "^27.5.1",
39
+ "sinon": "^9.2.4",
40
+ "ts-jest": "^27.1.4",
41
+ "typedoc": "^0.22.15",
42
+ "typedoc-plugin-missing-exports": "^0.22.6",
43
+ "typescript": "~4.6.3"
44
+ },
45
+ "engines": {
46
+ "node": ">=16.0.0"
47
+ },
48
+ "publishConfig": {
49
+ "access": "public",
50
+ "registry": "https://registry.npmjs.org/"
51
+ }
52
+ }