@metamask-previews/base-controller 4.1.1-preview.2431602 → 4.1.1-preview.303e509
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
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# Changelog
|
|
2
|
+
|
|
2
3
|
All notable changes to this project will be documented in this file.
|
|
3
4
|
|
|
4
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
@@ -7,15 +8,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
8
|
## [Unreleased]
|
|
8
9
|
|
|
9
10
|
## [4.1.1]
|
|
11
|
+
|
|
10
12
|
### Changed
|
|
13
|
+
|
|
11
14
|
- Bump `@metamask/utils` to `^8.3.0` ([#3769](https://github.com/MetaMask/core/pull/3769))
|
|
12
15
|
|
|
13
16
|
## [4.1.0]
|
|
17
|
+
|
|
14
18
|
### Added
|
|
19
|
+
|
|
15
20
|
- Add `registerInitialEventPayload` to `ControllerMessenger` and `RestrictedControllerMessenger` ([#3697](https://github.com/MetaMask/core/pull/3697))
|
|
16
21
|
- This allows registering an event payload function for an event, which has the benefit of ensuring the "subscription selector" feature works correctly the first time the event is fired after subscribing.
|
|
17
22
|
|
|
18
23
|
### Fixed
|
|
24
|
+
|
|
19
25
|
- Fix `subscribe` method selector support on first publish ([#3697](https://github.com/MetaMask/core/pull/3697))
|
|
20
26
|
- An event with a registered initial event payload function will work better with selectors, in that it will correctly compare with the initial selected state and return the previous value the first time it's published. Without this, the initial published event will always return `undefined` as the previous value.
|
|
21
27
|
- Subscribers to the `stateChange` event of any `BaseControllerV2`-based controllers will now correctly handle the initial state change event ([#3702](https://github.com/MetaMask/core/pull/3702))
|
|
@@ -23,18 +29,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
23
29
|
- `BaseControllerV2` has been updated to correctly compare with the previous value even for the first state change. The returned `previousValue` is also now guaranteed to be correct even for the initial state change.
|
|
24
30
|
|
|
25
31
|
## [4.0.1]
|
|
32
|
+
|
|
26
33
|
### Changed
|
|
34
|
+
|
|
27
35
|
- Deprecate `subscribe` property from `BaseControllerV2` ([#3590](https://github.com/MetaMask/core/pull/3590), [#3698](https://github.com/MetaMask/core/pull/3698))
|
|
28
36
|
- This property was used to differentiate between `BaseControllerV1` and `BaseControllerV2` controllers. It is no longer used, so it has been marked as deprecated.
|
|
29
37
|
|
|
30
38
|
## [4.0.0]
|
|
39
|
+
|
|
31
40
|
### Added
|
|
41
|
+
|
|
32
42
|
- Add `ControllerGetStateAction` and `ControllerStateChangeEvent` types ([#1890](https://github.com/MetaMask/core/pull/1890), [#2029](https://github.com/MetaMask/core/pull/2029))
|
|
33
43
|
- Add `NamespacedName` type ([#1890](https://github.com/MetaMask/core/pull/1890))
|
|
34
44
|
- This is the narrowest supertype of all names defined within a given namespace.
|
|
35
45
|
- Add `NotNamespacedBy` type, which matches an action/event name if and only if it is not prefixed by a given namespace ([#2051](https://github.com/MetaMask/core/pull/2051))
|
|
36
46
|
|
|
37
47
|
### Changed
|
|
48
|
+
|
|
38
49
|
- **BREAKING:** Alter controller messenger `ActionHandler` type so `Action` type parameter must satisfy (updated) `ActionConstraint` ([#1890](https://github.com/MetaMask/core/pull/1890))
|
|
39
50
|
- **BREAKING:** Alter controller messenger `ExtractActionParameters` utility type so `Action` type parameter must satisfy (updated) `ActionConstraint` ([#1890](https://github.com/MetaMask/core/pull/1890))
|
|
40
51
|
- **BREAKING:** Alter controller messenger `ExtractEventHandler` utility type so `Event` type parameter must satisfy `EventConstraint` ([#1890](https://github.com/MetaMask/core/pull/1890))
|
|
@@ -53,61 +64,86 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
53
64
|
- Bump `@metamask/utils` to ^8.2.0 ([#1957](https://github.com/MetaMask/core/pull/1957))
|
|
54
65
|
|
|
55
66
|
## [3.2.3]
|
|
67
|
+
|
|
56
68
|
### Changed
|
|
69
|
+
|
|
57
70
|
- Bump dependency on `@metamask/utils` to ^8.1.0 ([#1639](https://github.com/MetaMask/core/pull/1639))
|
|
58
71
|
|
|
59
72
|
## [3.2.2]
|
|
73
|
+
|
|
60
74
|
### Changed
|
|
75
|
+
|
|
61
76
|
- Update TypeScript to v4.8.x ([#1718](https://github.com/MetaMask/core/pull/1718))
|
|
62
77
|
|
|
63
78
|
## [3.2.1]
|
|
79
|
+
|
|
64
80
|
### Changed
|
|
81
|
+
|
|
65
82
|
- There are no consumer-facing changes to this package. This version is a part of a synchronized release across all packages in our monorepo.
|
|
66
83
|
|
|
67
84
|
## [3.2.0]
|
|
85
|
+
|
|
68
86
|
### Changed
|
|
69
|
-
|
|
87
|
+
|
|
88
|
+
- When deriving state, skip properties with invalid metadata ([#1529](https://github.com/MetaMask/core/pull/1529))
|
|
70
89
|
- The previous behavior was to throw an error
|
|
71
90
|
- An error is thrown in a timeout handler so that it can still be captured in the console, and by global unhandled error handlers.
|
|
72
91
|
- Update `@metamask/utils` to `^6.2.0` ([#1514](https://github.com/MetaMask/core/pull/1514))
|
|
73
92
|
|
|
74
93
|
## [3.1.0]
|
|
94
|
+
|
|
75
95
|
### Changed
|
|
96
|
+
|
|
76
97
|
- Prevent event publish from throwing error ([#1475](https://github.com/MetaMask/core/pull/1475))
|
|
77
98
|
- The controller messenger will no longer throw when an event subscriber throws an error. Calls to `publish` (either within controllers or on a messenger instance directly) will no longer throw errors.
|
|
78
99
|
- Errors are thrown in a timeout handler so that they can still be logged and captured.
|
|
79
100
|
|
|
80
101
|
## [3.0.0]
|
|
102
|
+
|
|
81
103
|
### Changed
|
|
104
|
+
|
|
82
105
|
- **BREAKING:** Bump to Node 16 ([#1262](https://github.com/MetaMask/core/pull/1262))
|
|
83
106
|
- Replace `@metamask/controller-utils` dependency with `@metamask/utils` ([#1370](https://github.com/MetaMask/core/pull/1370))
|
|
84
107
|
|
|
85
108
|
## [2.0.0]
|
|
109
|
+
|
|
86
110
|
### Removed
|
|
111
|
+
|
|
87
112
|
- **BREAKING:** Remove `isomorphic-fetch` ([#1106](https://github.com/MetaMask/controllers/pull/1106))
|
|
88
113
|
- Consumers must now import `isomorphic-fetch` or another polyfill themselves if they are running in an environment without `fetch`
|
|
89
114
|
|
|
90
115
|
## [1.1.2]
|
|
116
|
+
|
|
91
117
|
### Changed
|
|
118
|
+
|
|
92
119
|
- Rename this repository to `core` ([#1031](https://github.com/MetaMask/controllers/pull/1031))
|
|
93
|
-
- Update `@metamask/controller-utils` package ([#1041](https://github.com/MetaMask/controllers/pull/1041))
|
|
120
|
+
- Update `@metamask/controller-utils` package ([#1041](https://github.com/MetaMask/controllers/pull/1041))
|
|
94
121
|
|
|
95
122
|
## [1.1.1]
|
|
123
|
+
|
|
96
124
|
### Changed
|
|
125
|
+
|
|
97
126
|
- Relax dependency on `@metamask/controller-utils` (use `^` instead of `~`) ([#998](https://github.com/MetaMask/core/pull/998))
|
|
98
127
|
|
|
99
128
|
## [1.1.0]
|
|
129
|
+
|
|
100
130
|
### Added
|
|
131
|
+
|
|
101
132
|
- Add `applyPatches` function to BaseControllerV2 ([#980](https://github.com/MetaMask/core/pull/980))
|
|
102
133
|
|
|
103
134
|
### Changed
|
|
135
|
+
|
|
104
136
|
- Action and event handler types are now exported ([#987](https://github.com/MetaMask/core/pull/987))
|
|
105
137
|
- Update `update` function to expose patches ([#980](https://github.com/MetaMask/core/pull/980))
|
|
106
138
|
|
|
107
139
|
## [1.0.0]
|
|
140
|
+
|
|
108
141
|
### Added
|
|
142
|
+
|
|
109
143
|
- Initial release
|
|
144
|
+
|
|
110
145
|
- 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:
|
|
146
|
+
|
|
111
147
|
- `src/BaseController.ts`
|
|
112
148
|
- `src/BaseController.test.ts`
|
|
113
149
|
- `src/BaseControllerV2.ts`
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseControllerV1.d.ts","sourceRoot":"","sources":["../src/BaseControllerV1.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,QAAQ,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;AAE7C;;;;;GAKG;AAIH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;;GAKG;AAIH,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;GAQG;AACH,qBAAa,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,SAAS,SAAS;IACrE;;OAEG;IACH,aAAa,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"BaseControllerV1.d.ts","sourceRoot":"","sources":["../src/BaseControllerV1.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,QAAQ,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;AAE7C;;;;;GAKG;AAIH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;;GAKG;AAIH,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;GAQG;AACH,qBAAa,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,SAAS,SAAS;IACrE;;OAEG;IACH,aAAa,EAAE,CAAC,CAAe;IAE/B;;OAEG;IACH,YAAY,EAAE,CAAC,CAAe;IAE9B;;OAEG;IACH,QAAQ,UAAS;IAEjB;;OAEG;IACH,IAAI,SAAoB;IAExB,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAa;IAE3C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAa;IAE1C,OAAO,CAAC,cAAc,CAAyB;IAE/C,OAAO,CAAC,aAAa,CAAwB;IAE7C,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAqB;IAEvD;;;;;;OAMG;gBACS,MAAM,GAAE,OAAO,CAAC,CAAC,CAAM,EAAE,KAAK,GAAE,OAAO,CAAC,CAAC,CAAM;IAK3D;;;;;;OAMG;IACH,SAAS,CAAC,UAAU;IAQpB;;;;OAIG;IACH,IAAI,MAAM,MAET;IAED;;;;OAIG;IACH,IAAI,KAAK,MAER;IAED;;;;;;OAMG;IACH,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,SAAS,UAAQ,EAAE,UAAU,UAAO;IAwBlE;;OAEG;IACH,MAAM;IAUN;;;;OAIG;IACH,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC;IAI/B;;;;;OAKG;IACH,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC;IAMjC;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,SAAS,UAAQ;CAM5C;AAED,eAAe,gBAAgB,CAAC"}
|
package/dist/BaseControllerV1.js
CHANGED
|
@@ -38,7 +38,6 @@ class BaseControllerV1 {
|
|
|
38
38
|
this.internalConfig = this.defaultConfig;
|
|
39
39
|
this.internalState = this.defaultState;
|
|
40
40
|
this.internalListeners = [];
|
|
41
|
-
// Use assign since generics can't be spread: https://git.io/vpRhY
|
|
42
41
|
this.initialState = state;
|
|
43
42
|
this.initialConfig = config;
|
|
44
43
|
}
|
|
@@ -84,10 +83,9 @@ class BaseControllerV1 {
|
|
|
84
83
|
this.internalConfig = overwrite
|
|
85
84
|
? config
|
|
86
85
|
: Object.assign(this.internalConfig, config);
|
|
87
|
-
for (const
|
|
86
|
+
for (const key of Object.keys(this.internalConfig)) {
|
|
87
|
+
const value = this.internalConfig[key];
|
|
88
88
|
if (value !== undefined) {
|
|
89
|
-
// TODO: Replace `any` with type
|
|
90
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
89
|
this[key] = value;
|
|
92
90
|
}
|
|
93
91
|
}
|
|
@@ -95,11 +93,10 @@ class BaseControllerV1 {
|
|
|
95
93
|
else {
|
|
96
94
|
for (const key of Object.keys(config)) {
|
|
97
95
|
/* istanbul ignore else */
|
|
98
|
-
if (
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
this[key] = config[key];
|
|
96
|
+
if (this.internalConfig[key] !== undefined) {
|
|
97
|
+
const value = config[key];
|
|
98
|
+
this.internalConfig[key] = value;
|
|
99
|
+
this[key] = value;
|
|
103
100
|
}
|
|
104
101
|
}
|
|
105
102
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseControllerV1.js","sourceRoot":"","sources":["../src/BaseControllerV1.ts"],"names":[],"mappings":";;;AA+BA;;;;;;;;GAQG;AACH,MAAa,gBAAgB;IA+B3B;;;;;;OAMG;IACH,YAAY,SAAqB,
|
|
1
|
+
{"version":3,"file":"BaseControllerV1.js","sourceRoot":"","sources":["../src/BaseControllerV1.ts"],"names":[],"mappings":"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","sourcesContent":["/**\n * State change callbacks\n */\nexport type Listener<T> = (state: T) => void;\n\n/**\n * @type BaseConfig\n *\n * Base controller configuration\n * @property disabled - Determines if this controller is enabled\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface BaseConfig {\n disabled?: boolean;\n}\n\n/**\n * @type BaseState\n *\n * Base state representation\n * @property name - Unique name for this controller\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface BaseState {\n name?: string;\n}\n\n/**\n * @deprecated This class has been renamed to BaseControllerV1 and is no longer recommended for use for controllers. Please use BaseController (formerly BaseControllerV2) instead.\n *\n * Controller class that provides configuration, state management, and subscriptions.\n *\n * The core purpose of every controller is to maintain an internal data object\n * called \"state\". Each controller is responsible for its own state, and all global wallet state\n * is tracked in a controller as state.\n */\nexport class BaseControllerV1<C extends BaseConfig, S extends BaseState> {\n /**\n * Default options used to configure this controller\n */\n defaultConfig: C = {} as never;\n\n /**\n * Default state set on this controller\n */\n defaultState: S = {} as never;\n\n /**\n * Determines if listeners are notified of state changes\n */\n disabled = false;\n\n /**\n * Name of this controller used during composition\n */\n name = 'BaseController';\n\n private readonly initialConfig: Partial<C>;\n\n private readonly initialState: Partial<S>;\n\n private internalConfig: C = this.defaultConfig;\n\n private internalState: S = this.defaultState;\n\n private readonly internalListeners: Listener<S>[] = [];\n\n /**\n * Creates a BaseControllerV1 instance. Both initial state and initial\n * configuration options are merged with defaults upon initialization.\n *\n * @param config - Initial options used to configure this controller.\n * @param state - Initial state to set on this controller.\n */\n constructor(config: Partial<C> = {}, state: Partial<S> = {}) {\n this.initialState = state;\n this.initialConfig = config;\n }\n\n /**\n * Enables the controller. This sets each config option as a member\n * variable on this instance and triggers any defined setters. This\n * also sets initial state and triggers any listeners.\n *\n * @returns This controller instance.\n */\n protected initialize() {\n this.internalState = this.defaultState;\n this.internalConfig = this.defaultConfig;\n this.configure(this.initialConfig);\n this.update(this.initialState);\n return this;\n }\n\n /**\n * Retrieves current controller configuration options.\n *\n * @returns The current configuration.\n */\n get config() {\n return this.internalConfig;\n }\n\n /**\n * Retrieves current controller state.\n *\n * @returns The current state.\n */\n get state() {\n return this.internalState;\n }\n\n /**\n * Updates controller configuration.\n *\n * @param config - New configuration options.\n * @param overwrite - Overwrite config instead of merging.\n * @param fullUpdate - Boolean that defines if the update is partial or not.\n */\n configure(config: Partial<C>, overwrite = false, fullUpdate = true) {\n if (fullUpdate) {\n this.internalConfig = overwrite\n ? (config as C)\n : Object.assign(this.internalConfig, config);\n\n for (const key of Object.keys(this.internalConfig) as (keyof C)[]) {\n const value = this.internalConfig[key];\n if (value !== undefined) {\n (this as unknown as C)[key] = value;\n }\n }\n } else {\n for (const key of Object.keys(config) as (keyof C)[]) {\n /* istanbul ignore else */\n if (this.internalConfig[key] !== undefined) {\n const value = (config as C)[key];\n this.internalConfig[key] = value;\n (this as unknown as C)[key] = value;\n }\n }\n }\n }\n\n /**\n * Notifies all subscribed listeners of current state.\n */\n notify() {\n if (this.disabled) {\n return;\n }\n\n this.internalListeners.forEach((listener) => {\n listener(this.internalState);\n });\n }\n\n /**\n * Adds new listener to be notified of state changes.\n *\n * @param listener - The callback triggered when state changes.\n */\n subscribe(listener: Listener<S>) {\n this.internalListeners.push(listener);\n }\n\n /**\n * Removes existing listener from receiving state changes.\n *\n * @param listener - The callback to remove.\n * @returns `true` if a listener is found and unsubscribed.\n */\n unsubscribe(listener: Listener<S>) {\n const index = this.internalListeners.findIndex((cb) => listener === cb);\n index > -1 && this.internalListeners.splice(index, 1);\n return index > -1;\n }\n\n /**\n * Updates controller state.\n *\n * @param state - The new state.\n * @param overwrite - Overwrite state instead of merging.\n */\n update(state: Partial<S>, overwrite = false) {\n this.internalState = overwrite\n ? Object.assign({}, state as S)\n : Object.assign({}, this.internalState, state);\n this.notify();\n }\n}\n\nexport default BaseControllerV1;\n"]}
|
package/package.json
CHANGED