@metamask-previews/base-controller 4.1.1-preview.dad68d4 → 4.1.1-preview.eb2135e
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 +22 -0
- package/dist/BaseControllerV1.js +9 -151
- package/dist/BaseControllerV1.js.map +1 -1
- package/dist/BaseControllerV1.mjs +10 -0
- package/dist/BaseControllerV1.mjs.map +1 -0
- package/dist/BaseControllerV2.js +11 -161
- package/dist/BaseControllerV2.js.map +1 -1
- package/dist/BaseControllerV2.mjs +12 -0
- package/dist/BaseControllerV2.mjs.map +1 -0
- package/dist/ControllerMessenger.js +8 -237
- package/dist/ControllerMessenger.js.map +1 -1
- package/dist/ControllerMessenger.mjs +9 -0
- package/dist/ControllerMessenger.mjs.map +1 -0
- package/dist/RestrictedControllerMessenger.js +7 -223
- package/dist/RestrictedControllerMessenger.js.map +1 -1
- package/dist/RestrictedControllerMessenger.mjs +8 -0
- package/dist/RestrictedControllerMessenger.mjs.map +1 -0
- package/dist/chunk-AOA2V5WQ.mjs +239 -0
- package/dist/chunk-AOA2V5WQ.mjs.map +1 -0
- package/dist/chunk-CRIROOT2.js +141 -0
- package/dist/chunk-CRIROOT2.js.map +1 -0
- package/dist/chunk-EJ5YPJOH.mjs +251 -0
- package/dist/chunk-EJ5YPJOH.mjs.map +1 -0
- package/dist/chunk-G42723LG.js +239 -0
- package/dist/chunk-G42723LG.js.map +1 -0
- package/dist/chunk-NINOM2XQ.js +136 -0
- package/dist/chunk-NINOM2XQ.js.map +1 -0
- package/dist/chunk-NZOIZH6C.mjs +136 -0
- package/dist/chunk-NZOIZH6C.mjs.map +1 -0
- package/dist/chunk-U2WUIHS7.mjs +141 -0
- package/dist/chunk-U2WUIHS7.mjs.map +1 -0
- package/dist/chunk-UJFCPTF3.js +251 -0
- package/dist/chunk-UJFCPTF3.js.map +1 -0
- package/dist/chunk-XUI43LEZ.mjs +30 -0
- package/dist/chunk-XUI43LEZ.mjs.map +1 -0
- package/dist/chunk-Z4BLTVTB.js +30 -0
- package/dist/chunk-Z4BLTVTB.js.map +1 -0
- package/dist/index.js +23 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +24 -0
- package/dist/index.mjs.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/BaseControllerV1.d.ts.map +1 -0
- package/dist/{BaseControllerV2.d.ts → types/BaseControllerV2.d.ts} +16 -20
- package/dist/types/BaseControllerV2.d.ts.map +1 -0
- package/dist/{ControllerMessenger.d.ts → types/ControllerMessenger.d.ts} +7 -7
- package/dist/types/ControllerMessenger.d.ts.map +1 -0
- package/dist/{RestrictedControllerMessenger.d.ts → types/RestrictedControllerMessenger.d.ts} +3 -3
- package/dist/types/RestrictedControllerMessenger.d.ts.map +1 -0
- package/dist/{index.d.ts → types/index.d.ts} +1 -1
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +10 -1
- package/dist/BaseControllerV1.d.ts.map +0 -1
- package/dist/BaseControllerV2.d.ts.map +0 -1
- package/dist/ControllerMessenger.d.ts.map +0 -1
- package/dist/RestrictedControllerMessenger.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- /package/dist/{BaseControllerV1.d.ts → types/BaseControllerV1.d.ts} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Add and export type `StateConstraint`, which is an alias for `Record<string, Json>`, and represents the narrowest supertype encompassing the state of all controllers.
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
- **BREAKING:** Narrow the generic constraint of the `ControllerState` parameter from `Record<string, unknown>` to `Record<string, Json>` for types `ControllerGetStateAction`, `ControllerStateChangeEvent`, `ControllerActions`, `ControllerEvents`.
|
|
17
|
+
- **BREAKING:** Narrow the return type of the function `deriveStateFromMetadata<ControllerState>` from `Record<string, Json>` to `Record<keyof ControllerState, Json>`.
|
|
18
|
+
- **BREAKING:** The `getRestricted` method of the `ControllerMessenger` class has major API changes that may require updates to its existing instances in downstream code. These changes align the type-level and runtime behavior of `getRestricted`, so that omitted or empty inputs consistently represent a set of empty allowlists. ([$4013](https://github.com/MetaMask/core/pull/4013))
|
|
19
|
+
- If the `AllowedActions`, `AllowedEvents` generic parameters are omitted, they are always assumed to be `never`.
|
|
20
|
+
- Previously, omission of these generic parameters resulted in the full allowlists for the controller being inferred as type constraints for the `allowedActions`, `allowedEvents` function parameters.
|
|
21
|
+
- If the function parameters `allowedActions`, `allowedEvents` are a non-empty array, their corresponding type names must be explicitly passed into generic parameters `AllowedActions`, `AllowedEvents` to avoid type errors.
|
|
22
|
+
- This may cause some duplication of allowlists between type-level and value-level code.
|
|
23
|
+
- This requirement is only relevant for TypeScript code. A JavaScript consumer only needs to pass in the correct value-level function parameters. Because of this, these changes should not affect downstream JavaScript code, but may be disruptive to TypeScript code.
|
|
24
|
+
- `getRestricted` is still able to flag `AllowedActions` and `AllowedEvents` members that should not be included in the allowlists, based on the `Action`, `Event` generic arguments passed into the `ControllerMessenger` instance.
|
|
25
|
+
- **BREAKING:** The `RestrictedControllerMessenger` class constructor now expects `allowedActions` and `allowedEvents` as required options. ([$4013](https://github.com/MetaMask/core/pull/4013))
|
|
26
|
+
- Convert interface `StatePropertyMetadata<T extends Json>` into a type.
|
|
27
|
+
|
|
28
|
+
### Removed
|
|
29
|
+
|
|
30
|
+
- **BREAKING:** Remove the deprecated `subscribe` class field from `BaseControllerV2`.
|
|
31
|
+
|
|
10
32
|
## [4.1.1]
|
|
11
33
|
|
|
12
34
|
### Changed
|
package/dist/BaseControllerV1.js
CHANGED
|
@@ -1,152 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* called "state". Each controller is responsible for its own state, and all global wallet state
|
|
11
|
-
* is tracked in a controller as state.
|
|
12
|
-
*/
|
|
13
|
-
class BaseControllerV1 {
|
|
14
|
-
/**
|
|
15
|
-
* Creates a BaseControllerV1 instance. Both initial state and initial
|
|
16
|
-
* configuration options are merged with defaults upon initialization.
|
|
17
|
-
*
|
|
18
|
-
* @param config - Initial options used to configure this controller.
|
|
19
|
-
* @param state - Initial state to set on this controller.
|
|
20
|
-
*/
|
|
21
|
-
constructor(config = {}, state = {}) {
|
|
22
|
-
/**
|
|
23
|
-
* Default options used to configure this controller
|
|
24
|
-
*/
|
|
25
|
-
this.defaultConfig = {};
|
|
26
|
-
/**
|
|
27
|
-
* Default state set on this controller
|
|
28
|
-
*/
|
|
29
|
-
this.defaultState = {};
|
|
30
|
-
/**
|
|
31
|
-
* Determines if listeners are notified of state changes
|
|
32
|
-
*/
|
|
33
|
-
this.disabled = false;
|
|
34
|
-
/**
|
|
35
|
-
* Name of this controller used during composition
|
|
36
|
-
*/
|
|
37
|
-
this.name = 'BaseController';
|
|
38
|
-
this.internalConfig = this.defaultConfig;
|
|
39
|
-
this.internalState = this.defaultState;
|
|
40
|
-
this.internalListeners = [];
|
|
41
|
-
// Use assign since generics can't be spread: https://git.io/vpRhY
|
|
42
|
-
this.initialState = state;
|
|
43
|
-
this.initialConfig = config;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Enables the controller. This sets each config option as a member
|
|
47
|
-
* variable on this instance and triggers any defined setters. This
|
|
48
|
-
* also sets initial state and triggers any listeners.
|
|
49
|
-
*
|
|
50
|
-
* @returns This controller instance.
|
|
51
|
-
*/
|
|
52
|
-
initialize() {
|
|
53
|
-
this.internalState = this.defaultState;
|
|
54
|
-
this.internalConfig = this.defaultConfig;
|
|
55
|
-
this.configure(this.initialConfig);
|
|
56
|
-
this.update(this.initialState);
|
|
57
|
-
return this;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Retrieves current controller configuration options.
|
|
61
|
-
*
|
|
62
|
-
* @returns The current configuration.
|
|
63
|
-
*/
|
|
64
|
-
get config() {
|
|
65
|
-
return this.internalConfig;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Retrieves current controller state.
|
|
69
|
-
*
|
|
70
|
-
* @returns The current state.
|
|
71
|
-
*/
|
|
72
|
-
get state() {
|
|
73
|
-
return this.internalState;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Updates controller configuration.
|
|
77
|
-
*
|
|
78
|
-
* @param config - New configuration options.
|
|
79
|
-
* @param overwrite - Overwrite config instead of merging.
|
|
80
|
-
* @param fullUpdate - Boolean that defines if the update is partial or not.
|
|
81
|
-
*/
|
|
82
|
-
configure(config, overwrite = false, fullUpdate = true) {
|
|
83
|
-
if (fullUpdate) {
|
|
84
|
-
this.internalConfig = overwrite
|
|
85
|
-
? config
|
|
86
|
-
: Object.assign(this.internalConfig, config);
|
|
87
|
-
for (const [key, value] of Object.entries(this.internalConfig)) {
|
|
88
|
-
if (value !== undefined) {
|
|
89
|
-
// TODO: Replace `any` with type
|
|
90
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
-
this[key] = value;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
for (const key of Object.keys(config)) {
|
|
97
|
-
/* istanbul ignore else */
|
|
98
|
-
if (typeof this.internalConfig[key] !== 'undefined') {
|
|
99
|
-
this.internalConfig[key] = config[key];
|
|
100
|
-
// TODO: Replace `any` with type
|
|
101
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
102
|
-
this[key] = config[key];
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Notifies all subscribed listeners of current state.
|
|
109
|
-
*/
|
|
110
|
-
notify() {
|
|
111
|
-
if (this.disabled) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
this.internalListeners.forEach((listener) => {
|
|
115
|
-
listener(this.internalState);
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Adds new listener to be notified of state changes.
|
|
120
|
-
*
|
|
121
|
-
* @param listener - The callback triggered when state changes.
|
|
122
|
-
*/
|
|
123
|
-
subscribe(listener) {
|
|
124
|
-
this.internalListeners.push(listener);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Removes existing listener from receiving state changes.
|
|
128
|
-
*
|
|
129
|
-
* @param listener - The callback to remove.
|
|
130
|
-
* @returns `true` if a listener is found and unsubscribed.
|
|
131
|
-
*/
|
|
132
|
-
unsubscribe(listener) {
|
|
133
|
-
const index = this.internalListeners.findIndex((cb) => listener === cb);
|
|
134
|
-
index > -1 && this.internalListeners.splice(index, 1);
|
|
135
|
-
return index > -1;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Updates controller state.
|
|
139
|
-
*
|
|
140
|
-
* @param state - The new state.
|
|
141
|
-
* @param overwrite - Overwrite state instead of merging.
|
|
142
|
-
*/
|
|
143
|
-
update(state, overwrite = false) {
|
|
144
|
-
this.internalState = overwrite
|
|
145
|
-
? Object.assign({}, state)
|
|
146
|
-
: Object.assign({}, this.internalState, state);
|
|
147
|
-
this.notify();
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
exports.BaseControllerV1 = BaseControllerV1;
|
|
151
|
-
exports.default = BaseControllerV1;
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkNINOM2XQjs = require('./chunk-NINOM2XQ.js');
|
|
5
|
+
require('./chunk-Z4BLTVTB.js');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
exports.BaseControllerV1 = _chunkNINOM2XQjs.BaseControllerV1; exports.default = _chunkNINOM2XQjs.BaseControllerV1_default;
|
|
152
10
|
//# sourceMappingURL=BaseControllerV1.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/BaseControllerV2.js
CHANGED
|
@@ -1,162 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
var _BaseController_internalState;
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.getPersistentState = exports.getAnonymizedState = exports.BaseController = void 0;
|
|
16
|
-
const immer_1 = require("immer");
|
|
17
|
-
(0, immer_1.enablePatches)();
|
|
18
|
-
/**
|
|
19
|
-
* Controller class that provides state management, subscriptions, and state metadata
|
|
20
|
-
*/
|
|
21
|
-
class BaseController {
|
|
22
|
-
/**
|
|
23
|
-
* Creates a BaseController instance.
|
|
24
|
-
*
|
|
25
|
-
* @param options - Controller options.
|
|
26
|
-
* @param options.messenger - Controller messaging system.
|
|
27
|
-
* @param options.metadata - ControllerState metadata, describing how to "anonymize" the state, and which
|
|
28
|
-
* parts should be persisted.
|
|
29
|
-
* @param options.name - The name of the controller, used as a namespace for events and actions.
|
|
30
|
-
* @param options.state - Initial controller state.
|
|
31
|
-
*/
|
|
32
|
-
constructor({ messenger, metadata, name, state, }) {
|
|
33
|
-
_BaseController_internalState.set(this, void 0);
|
|
34
|
-
this.messagingSystem = messenger;
|
|
35
|
-
this.name = name;
|
|
36
|
-
__classPrivateFieldSet(this, _BaseController_internalState, state, "f");
|
|
37
|
-
this.metadata = metadata;
|
|
38
|
-
this.messagingSystem.registerActionHandler(`${name}:getState`, () => this.state);
|
|
39
|
-
this.messagingSystem.registerInitialEventPayload({
|
|
40
|
-
eventType: `${name}:stateChange`,
|
|
41
|
-
getPayload: () => [this.state, []],
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Retrieves current controller state.
|
|
46
|
-
*
|
|
47
|
-
* @returns The current state.
|
|
48
|
-
*/
|
|
49
|
-
get state() {
|
|
50
|
-
return __classPrivateFieldGet(this, _BaseController_internalState, "f");
|
|
51
|
-
}
|
|
52
|
-
set state(_) {
|
|
53
|
-
throw new Error(`Controller state cannot be directly mutated; use 'update' method instead.`);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Updates controller state. Accepts a callback that is passed a draft copy
|
|
57
|
-
* of the controller state. If a value is returned, it is set as the new
|
|
58
|
-
* state. Otherwise, any changes made within that callback to the draft are
|
|
59
|
-
* applied to the controller state.
|
|
60
|
-
*
|
|
61
|
-
* @param callback - Callback for updating state, passed a draft state
|
|
62
|
-
* object. Return a new state object or mutate the draft to update state.
|
|
63
|
-
* @returns An object that has the next state, patches applied in the update and inverse patches to
|
|
64
|
-
* rollback the update.
|
|
65
|
-
*/
|
|
66
|
-
update(callback) {
|
|
67
|
-
// We run into ts2589, "infinite type depth", if we don't cast
|
|
68
|
-
// produceWithPatches here.
|
|
69
|
-
const [nextState, patches, inversePatches] = immer_1.produceWithPatches(__classPrivateFieldGet(this, _BaseController_internalState, "f"), callback);
|
|
70
|
-
__classPrivateFieldSet(this, _BaseController_internalState, nextState, "f");
|
|
71
|
-
this.messagingSystem.publish(`${this.name}:stateChange`, nextState, patches);
|
|
72
|
-
return { nextState, patches, inversePatches };
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Applies immer patches to the current state. The patches come from the
|
|
76
|
-
* update function itself and can either be normal or inverse patches.
|
|
77
|
-
*
|
|
78
|
-
* @param patches - An array of immer patches that are to be applied to make
|
|
79
|
-
* or undo changes.
|
|
80
|
-
*/
|
|
81
|
-
applyPatches(patches) {
|
|
82
|
-
const nextState = (0, immer_1.applyPatches)(__classPrivateFieldGet(this, _BaseController_internalState, "f"), patches);
|
|
83
|
-
__classPrivateFieldSet(this, _BaseController_internalState, nextState, "f");
|
|
84
|
-
this.messagingSystem.publish(`${this.name}:stateChange`, nextState, patches);
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Prepares the controller for garbage collection. This should be extended
|
|
88
|
-
* by any subclasses to clean up any additional connections or events.
|
|
89
|
-
*
|
|
90
|
-
* The only cleanup performed here is to remove listeners. While technically
|
|
91
|
-
* this is not required to ensure this instance is garbage collected, it at
|
|
92
|
-
* least ensures this instance won't be responsible for preventing the
|
|
93
|
-
* listeners from being garbage collected.
|
|
94
|
-
*/
|
|
95
|
-
destroy() {
|
|
96
|
-
this.messagingSystem.clearEventSubscriptions(`${this.name}:stateChange`);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
exports.BaseController = BaseController;
|
|
100
|
-
_BaseController_internalState = new WeakMap();
|
|
101
|
-
/**
|
|
102
|
-
* Returns an anonymized representation of the controller state.
|
|
103
|
-
*
|
|
104
|
-
* By "anonymized" we mean that it should not contain any information that could be personally
|
|
105
|
-
* identifiable.
|
|
106
|
-
*
|
|
107
|
-
* @param state - The controller state.
|
|
108
|
-
* @param metadata - The controller state metadata, which describes how to derive the
|
|
109
|
-
* anonymized state.
|
|
110
|
-
* @returns The anonymized controller state.
|
|
111
|
-
*/
|
|
112
|
-
function getAnonymizedState(state, metadata) {
|
|
113
|
-
return deriveStateFromMetadata(state, metadata, 'anonymous');
|
|
114
|
-
}
|
|
115
|
-
exports.getAnonymizedState = getAnonymizedState;
|
|
116
|
-
/**
|
|
117
|
-
* Returns the subset of state that should be persisted.
|
|
118
|
-
*
|
|
119
|
-
* @param state - The controller state.
|
|
120
|
-
* @param metadata - The controller state metadata, which describes which pieces of state should be persisted.
|
|
121
|
-
* @returns The subset of controller state that should be persisted.
|
|
122
|
-
*/
|
|
123
|
-
function getPersistentState(state, metadata) {
|
|
124
|
-
return deriveStateFromMetadata(state, metadata, 'persist');
|
|
125
|
-
}
|
|
126
|
-
exports.getPersistentState = getPersistentState;
|
|
127
|
-
/**
|
|
128
|
-
* Use the metadata to derive state according to the given metadata property.
|
|
129
|
-
*
|
|
130
|
-
* @param state - The full controller state.
|
|
131
|
-
* @param metadata - The controller metadata.
|
|
132
|
-
* @param metadataProperty - The metadata property to use to derive state.
|
|
133
|
-
* @returns The metadata-derived controller state.
|
|
134
|
-
*/
|
|
135
|
-
function deriveStateFromMetadata(state, metadata, metadataProperty) {
|
|
136
|
-
return Object.keys(state).reduce((persistedState, key) => {
|
|
137
|
-
try {
|
|
138
|
-
const stateMetadata = metadata[key];
|
|
139
|
-
if (!stateMetadata) {
|
|
140
|
-
throw new Error(`No metadata found for '${String(key)}'`);
|
|
141
|
-
}
|
|
142
|
-
const propertyMetadata = stateMetadata[metadataProperty];
|
|
143
|
-
const stateProperty = state[key];
|
|
144
|
-
if (typeof propertyMetadata === 'function') {
|
|
145
|
-
persistedState[key] = propertyMetadata(stateProperty);
|
|
146
|
-
}
|
|
147
|
-
else if (propertyMetadata) {
|
|
148
|
-
persistedState[key] = stateProperty;
|
|
149
|
-
}
|
|
150
|
-
return persistedState;
|
|
151
|
-
}
|
|
152
|
-
catch (error) {
|
|
153
|
-
// Throw error after timeout so that it is captured as a console error
|
|
154
|
-
// (and by Sentry) without interrupting state-related operations
|
|
155
|
-
setTimeout(() => {
|
|
156
|
-
throw error;
|
|
157
|
-
});
|
|
158
|
-
return persistedState;
|
|
159
|
-
}
|
|
160
|
-
}, {});
|
|
161
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunkCRIROOT2js = require('./chunk-CRIROOT2.js');
|
|
6
|
+
require('./chunk-Z4BLTVTB.js');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
exports.BaseController = _chunkCRIROOT2js.BaseController; exports.getAnonymizedState = _chunkCRIROOT2js.getAnonymizedState; exports.getPersistentState = _chunkCRIROOT2js.getPersistentState;
|
|
162
12
|
//# sourceMappingURL=BaseControllerV2.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseControllerV2.js","sourceRoot":"","sources":["../src/BaseControllerV2.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,iCAAwE;AAMxE,IAAA,qBAAa,GAAE,CAAC;AAiFhB;;GAEG;AACH,MAAa,cAAc;IAmCzB;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,QAAQ,EACR,IAAI,EACJ,KAAK,GAMN;QA5CD,gDAAgC;QA6C9B,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,uBAAA,IAAI,iCAAkB,KAAK,MAAA,CAAC;QAC5B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAEzB,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,IAAI,WAAW,EAClB,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CACjB,CAAC;QAEF,IAAI,CAAC,eAAe,CAAC,2BAA2B,CAAC;YAC/C,SAAS,EAAE,GAAG,IAAI,cAAc;YAChC,UAAU,EAAE,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC;SACnC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,IAAI,KAAK;QACP,OAAO,uBAAA,IAAI,qCAAe,CAAC;IAC7B,CAAC;IAED,IAAI,KAAK,CAAC,CAAC;QACT,MAAM,IAAI,KAAK,CACb,2EAA2E,CAC5E,CAAC;IACJ,CAAC;IAED;;;;;;;;;;OAUG;IACO,MAAM,CACd,QAAmE;QAMnE,8DAA8D;QAC9D,2BAA2B;QAC3B,MAAM,CAAC,SAAS,EAAE,OAAO,EAAE,cAAc,CAAC,GACxC,0BAID,CAAC,uBAAA,IAAI,qCAAe,EAAE,QAAQ,CAAC,CAAC;QAEjC,uBAAA,IAAI,iCAAkB,SAAS,MAAA,CAAC;QAChC,IAAI,CAAC,eAAe,CAAC,OAAO,CAC1B,GAAG,IAAI,CAAC,IAAI,cAAc,EAC1B,SAAS,EACT,OAAO,CACR,CAAC;QAEF,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC;IAChD,CAAC;IAED;;;;;;OAMG;IACO,YAAY,CAAC,OAAgB;QACrC,MAAM,SAAS,GAAG,IAAA,oBAAY,EAAC,uBAAA,IAAI,qCAAe,EAAE,OAAO,CAAC,CAAC;QAC7D,uBAAA,IAAI,iCAAkB,SAAS,MAAA,CAAC;QAChC,IAAI,CAAC,eAAe,CAAC,OAAO,CAC1B,GAAG,IAAI,CAAC,IAAI,cAAc,EAC1B,SAAS,EACT,OAAO,CACR,CAAC;IACJ,CAAC;IAED;;;;;;;;OAQG;IACO,OAAO;QACf,IAAI,CAAC,eAAe,CAAC,uBAAuB,CAAC,GAAG,IAAI,CAAC,IAAI,cAAc,CAAC,CAAC;IAC3E,CAAC;CACF;AAzJD,wCAyJC;;AAED;;;;;;;;;;GAUG;AACH,SAAgB,kBAAkB,CAGhC,KAAsB,EACtB,QAAwC;IAExC,OAAO,uBAAuB,CAAC,KAAK,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;AAC/D,CAAC;AAPD,gDAOC;AAED;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAGhC,KAAsB,EACtB,QAAwC;IAExC,OAAO,uBAAuB,CAAC,KAAK,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;AAC7D,CAAC;AAPD,gDAOC;AAED;;;;;;;GAOG;AACH,SAAS,uBAAuB,CAC9B,KAAsB,EACtB,QAAwC,EACxC,gBAAyC;IAEzC,OAAQ,MAAM,CAAC,IAAI,CAAC,KAAK,CAA+B,CAAC,MAAM,CAE7D,CAAC,cAAc,EAAE,GAAG,EAAE,EAAE;QACxB,IAAI;YACF,MAAM,aAAa,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;YACpC,IAAI,CAAC,aAAa,EAAE;gBAClB,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;aAC3D;YACD,MAAM,gBAAgB,GAAG,aAAa,CAAC,gBAAgB,CAAC,CAAC;YACzD,MAAM,aAAa,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;YACjC,IAAI,OAAO,gBAAgB,KAAK,UAAU,EAAE;gBAC1C,cAAc,CAAC,GAAG,CAAC,GAAG,gBAAgB,CAAC,aAAa,CAAC,CAAC;aACvD;iBAAM,IAAI,gBAAgB,EAAE;gBAC3B,cAAc,CAAC,GAAG,CAAC,GAAG,aAAa,CAAC;aACrC;YACD,OAAO,cAAc,CAAC;SACvB;QAAC,OAAO,KAAK,EAAE;YACd,sEAAsE;YACtE,gEAAgE;YAChE,UAAU,CAAC,GAAG,EAAE;gBACd,MAAM,KAAK,CAAC;YACd,CAAC,CAAC,CAAC;YACH,OAAO,cAAc,CAAC;SACvB;IACH,CAAC,EAAE,EAAE,CAAwC,CAAC;AAChD,CAAC","sourcesContent":["import type { Json } from '@metamask/utils';\nimport { enablePatches, produceWithPatches, applyPatches } from 'immer';\nimport type { Draft, Patch } from 'immer';\n\nimport type { ActionConstraint, EventConstraint } from './ControllerMessenger';\nimport type { RestrictedControllerMessenger } from './RestrictedControllerMessenger';\n\nenablePatches();\n\n/**\n * A state change listener.\n *\n * This function will get called for each state change, and is given a copy of\n * the new state along with a set of patches describing the changes since the\n * last update.\n *\n * @param state - The new controller state.\n * @param patches - A list of patches describing any changes (see here for more\n * information: https://immerjs.github.io/immer/docs/patches)\n */\nexport type Listener<T> = (state: T, patches: Patch[]) => void;\n\n/**\n * An function to derive state.\n *\n * This function will accept one piece of the controller state (one property),\n * and will return some derivation of that state.\n *\n * @param value - A piece of controller state.\n * @returns Something derived from controller state.\n */\nexport type StateDeriver<T extends Json> = (value: T) => Json;\n\n/**\n * State metadata.\n *\n * This metadata describes which parts of state should be persisted, and how to\n * get an anonymized representation of the state.\n */\nexport type StateMetadata<T extends Record<string, Json>> = {\n [P in keyof T]: StatePropertyMetadata<T[P]>;\n};\n\n/**\n * Metadata for a single state property\n *\n * @property persist - Indicates whether this property should be persisted\n * (`true` for persistent, `false` for transient), or is set to a function\n * that derives the persistent state from the state.\n * @property anonymous - Indicates whether this property is already anonymous,\n * (`true` for anonymous, `false` if it has potential to be personally\n * identifiable), or is set to a function that returns an anonymized\n * representation of this state.\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 StatePropertyMetadata<T extends Json> {\n persist: boolean | StateDeriver<T>;\n anonymous: boolean | StateDeriver<T>;\n}\n\nexport type ControllerGetStateAction<\n ControllerName extends string,\n ControllerState extends Record<string, unknown>,\n> = {\n type: `${ControllerName}:getState`;\n handler: () => ControllerState;\n};\n\nexport type ControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends Record<string, unknown>,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\nexport type ControllerActions<\n ControllerName extends string,\n ControllerState extends Record<string, unknown>,\n> = ControllerGetStateAction<ControllerName, ControllerState>;\n\nexport type ControllerEvents<\n ControllerName extends string,\n ControllerState extends Record<string, unknown>,\n> = ControllerStateChangeEvent<ControllerName, ControllerState>;\n\n/**\n * Controller class that provides state management, subscriptions, and state metadata\n */\nexport class BaseController<\n ControllerName extends string,\n ControllerState extends Record<string, Json>,\n messenger extends RestrictedControllerMessenger<\n ControllerName,\n ActionConstraint | ControllerActions<ControllerName, ControllerState>,\n EventConstraint | ControllerEvents<ControllerName, ControllerState>,\n string,\n string\n >,\n> {\n #internalState: ControllerState;\n\n protected messagingSystem: messenger;\n\n /**\n * The name of the controller.\n *\n * This is used by the ComposableController to construct a composed application state.\n */\n public readonly name: ControllerName;\n\n public readonly metadata: StateMetadata<ControllerState>;\n\n /**\n * The existence of the `subscribe` property is how the ComposableController used to detect\n * whether a controller extends the old BaseController or the new one. We set it to `undefined` here to\n * ensure the ComposableController never mistakes them for an older style controller.\n *\n * This property is no longer used, and will be removed in a future release.\n *\n * @deprecated This will be removed in a future release\n */\n public readonly subscribe: undefined;\n\n /**\n * Creates a BaseController instance.\n *\n * @param options - Controller options.\n * @param options.messenger - Controller messaging system.\n * @param options.metadata - ControllerState metadata, describing how to \"anonymize\" the state, and which\n * parts should be persisted.\n * @param options.name - The name of the controller, used as a namespace for events and actions.\n * @param options.state - Initial controller state.\n */\n constructor({\n messenger,\n metadata,\n name,\n state,\n }: {\n messenger: messenger;\n metadata: StateMetadata<ControllerState>;\n name: ControllerName;\n state: ControllerState;\n }) {\n this.messagingSystem = messenger;\n this.name = name;\n this.#internalState = state;\n this.metadata = metadata;\n\n this.messagingSystem.registerActionHandler(\n `${name}:getState`,\n () => this.state,\n );\n\n this.messagingSystem.registerInitialEventPayload({\n eventType: `${name}:stateChange`,\n getPayload: () => [this.state, []],\n });\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 set state(_) {\n throw new Error(\n `Controller state cannot be directly mutated; use 'update' method instead.`,\n );\n }\n\n /**\n * Updates controller state. Accepts a callback that is passed a draft copy\n * of the controller state. If a value is returned, it is set as the new\n * state. Otherwise, any changes made within that callback to the draft are\n * applied to the controller state.\n *\n * @param callback - Callback for updating state, passed a draft state\n * object. Return a new state object or mutate the draft to update state.\n * @returns An object that has the next state, patches applied in the update and inverse patches to\n * rollback the update.\n */\n protected update(\n callback: (state: Draft<ControllerState>) => void | ControllerState,\n ): {\n nextState: ControllerState;\n patches: Patch[];\n inversePatches: Patch[];\n } {\n // We run into ts2589, \"infinite type depth\", if we don't cast\n // produceWithPatches here.\n const [nextState, patches, inversePatches] = (\n produceWithPatches as unknown as (\n state: ControllerState,\n cb: typeof callback,\n ) => [ControllerState, Patch[], Patch[]]\n )(this.#internalState, callback);\n\n this.#internalState = nextState;\n this.messagingSystem.publish(\n `${this.name}:stateChange`,\n nextState,\n patches,\n );\n\n return { nextState, patches, inversePatches };\n }\n\n /**\n * Applies immer patches to the current state. The patches come from the\n * update function itself and can either be normal or inverse patches.\n *\n * @param patches - An array of immer patches that are to be applied to make\n * or undo changes.\n */\n protected applyPatches(patches: Patch[]) {\n const nextState = applyPatches(this.#internalState, patches);\n this.#internalState = nextState;\n this.messagingSystem.publish(\n `${this.name}:stateChange`,\n nextState,\n patches,\n );\n }\n\n /**\n * Prepares the controller for garbage collection. This should be extended\n * by any subclasses to clean up any additional connections or events.\n *\n * The only cleanup performed here is to remove listeners. While technically\n * this is not required to ensure this instance is garbage collected, it at\n * least ensures this instance won't be responsible for preventing the\n * listeners from being garbage collected.\n */\n protected destroy() {\n this.messagingSystem.clearEventSubscriptions(`${this.name}:stateChange`);\n }\n}\n\n/**\n * Returns an anonymized representation of the controller state.\n *\n * By \"anonymized\" we mean that it should not contain any information that could be personally\n * identifiable.\n *\n * @param state - The controller state.\n * @param metadata - The controller state metadata, which describes how to derive the\n * anonymized state.\n * @returns The anonymized controller state.\n */\nexport function getAnonymizedState<\n ControllerState extends Record<string, Json>,\n>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n): Record<string, Json> {\n return deriveStateFromMetadata(state, metadata, 'anonymous');\n}\n\n/**\n * Returns the subset of state that should be persisted.\n *\n * @param state - The controller state.\n * @param metadata - The controller state metadata, which describes which pieces of state should be persisted.\n * @returns The subset of controller state that should be persisted.\n */\nexport function getPersistentState<\n ControllerState extends Record<string, Json>,\n>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n): Record<string, Json> {\n return deriveStateFromMetadata(state, metadata, 'persist');\n}\n\n/**\n * Use the metadata to derive state according to the given metadata property.\n *\n * @param state - The full controller state.\n * @param metadata - The controller metadata.\n * @param metadataProperty - The metadata property to use to derive state.\n * @returns The metadata-derived controller state.\n */\nfunction deriveStateFromMetadata<ControllerState extends Record<string, Json>>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n metadataProperty: 'anonymous' | 'persist',\n): Record<string, Json> {\n return (Object.keys(state) as (keyof ControllerState)[]).reduce<\n Partial<Record<keyof ControllerState, Json>>\n >((persistedState, key) => {\n try {\n const stateMetadata = metadata[key];\n if (!stateMetadata) {\n throw new Error(`No metadata found for '${String(key)}'`);\n }\n const propertyMetadata = stateMetadata[metadataProperty];\n const stateProperty = state[key];\n if (typeof propertyMetadata === 'function') {\n persistedState[key] = propertyMetadata(stateProperty);\n } else if (propertyMetadata) {\n persistedState[key] = stateProperty;\n }\n return persistedState;\n } catch (error) {\n // Throw error after timeout so that it is captured as a console error\n // (and by Sentry) without interrupting state-related operations\n setTimeout(() => {\n throw error;\n });\n return persistedState;\n }\n }, {}) as Record<keyof ControllerState, Json>;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseController,
|
|
3
|
+
getAnonymizedState,
|
|
4
|
+
getPersistentState
|
|
5
|
+
} from "./chunk-U2WUIHS7.mjs";
|
|
6
|
+
import "./chunk-XUI43LEZ.mjs";
|
|
7
|
+
export {
|
|
8
|
+
BaseController,
|
|
9
|
+
getAnonymizedState,
|
|
10
|
+
getPersistentState
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=BaseControllerV2.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|