@metamask-previews/base-controller 8.1.0-preview-50d4945e → 8.1.0-preview-7dfc3145
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 +5 -0
- package/dist/next/BaseController.cjs +187 -0
- package/dist/next/BaseController.cjs.map +1 -0
- package/dist/next/BaseController.d.cts +189 -0
- package/dist/next/BaseController.d.cts.map +1 -0
- package/dist/next/BaseController.d.mts +189 -0
- package/dist/next/BaseController.d.mts.map +1 -0
- package/dist/next/BaseController.mjs +180 -0
- package/dist/next/BaseController.mjs.map +1 -0
- package/dist/next/index.cjs +9 -0
- package/dist/next/index.cjs.map +1 -0
- package/dist/next/index.d.cts +3 -0
- package/dist/next/index.d.cts.map +1 -0
- package/dist/next/index.d.mts +3 -0
- package/dist/next/index.d.mts.map +1 -0
- package/dist/next/index.mjs +2 -0
- package/dist/next/index.mjs.map +1 -0
- package/next.js +3 -0
- package/package.json +13 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,11 @@ 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 experimental `next` export for testing upcoming breaking changes ([#6316](https://github.com/MetaMask/core/pull/6316))
|
|
13
|
+
- Note that this should generally not be used, and further breaking changes may be made under this export without a corresponding major version bump for this package.
|
|
14
|
+
|
|
10
15
|
## [8.1.0]
|
|
11
16
|
|
|
12
17
|
### Added
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
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");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
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");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _BaseController_internalState;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.getPersistentState = exports.getAnonymizedState = exports.BaseController = exports.isBaseController = void 0;
|
|
16
|
+
const immer_1 = require("immer");
|
|
17
|
+
(0, immer_1.enablePatches)();
|
|
18
|
+
/**
|
|
19
|
+
* Determines if the given controller is an instance of `BaseController`
|
|
20
|
+
*
|
|
21
|
+
* @param controller - Controller instance to check
|
|
22
|
+
* @returns True if the controller is an instance of `BaseController`
|
|
23
|
+
*/
|
|
24
|
+
function isBaseController(controller) {
|
|
25
|
+
return (typeof controller === 'object' &&
|
|
26
|
+
controller !== null &&
|
|
27
|
+
'name' in controller &&
|
|
28
|
+
typeof controller.name === 'string' &&
|
|
29
|
+
'state' in controller &&
|
|
30
|
+
typeof controller.state === 'object' &&
|
|
31
|
+
'metadata' in controller &&
|
|
32
|
+
typeof controller.metadata === 'object');
|
|
33
|
+
}
|
|
34
|
+
exports.isBaseController = isBaseController;
|
|
35
|
+
/**
|
|
36
|
+
* Controller class that provides state management, subscriptions, and state metadata
|
|
37
|
+
*/
|
|
38
|
+
class BaseController {
|
|
39
|
+
/**
|
|
40
|
+
* Creates a BaseController instance.
|
|
41
|
+
*
|
|
42
|
+
* @param options - Controller options.
|
|
43
|
+
* @param options.messenger - Controller messaging system.
|
|
44
|
+
* @param options.metadata - ControllerState metadata, describing how to "anonymize" the state, and which
|
|
45
|
+
* parts should be persisted.
|
|
46
|
+
* @param options.name - The name of the controller, used as a namespace for events and actions.
|
|
47
|
+
* @param options.state - Initial controller state.
|
|
48
|
+
*/
|
|
49
|
+
constructor({ messenger, metadata, name, state, }) {
|
|
50
|
+
_BaseController_internalState.set(this, void 0);
|
|
51
|
+
this.messagingSystem = messenger;
|
|
52
|
+
this.name = name;
|
|
53
|
+
// Here we use `freeze` from Immer to enforce that the state is deeply
|
|
54
|
+
// immutable. Note that this is a runtime check, not a compile-time check.
|
|
55
|
+
// That is, unlike `Object.freeze`, this does not narrow the type
|
|
56
|
+
// recursively to `Readonly`. The equivalent in Immer is `Immutable`, but
|
|
57
|
+
// `Immutable` does not handle recursive types such as our `Json` type.
|
|
58
|
+
__classPrivateFieldSet(this, _BaseController_internalState, (0, immer_1.freeze)(state, true), "f");
|
|
59
|
+
this.metadata = metadata;
|
|
60
|
+
this.messagingSystem.registerActionHandler(`${name}:getState`, () => this.state);
|
|
61
|
+
this.messagingSystem.registerInitialEventPayload({
|
|
62
|
+
eventType: `${name}:stateChange`,
|
|
63
|
+
getPayload: () => [this.state, []],
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Retrieves current controller state.
|
|
68
|
+
*
|
|
69
|
+
* @returns The current state.
|
|
70
|
+
*/
|
|
71
|
+
get state() {
|
|
72
|
+
return __classPrivateFieldGet(this, _BaseController_internalState, "f");
|
|
73
|
+
}
|
|
74
|
+
set state(_) {
|
|
75
|
+
throw new Error(`Controller state cannot be directly mutated; use 'update' method instead.`);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Updates controller state. Accepts a callback that is passed a draft copy
|
|
79
|
+
* of the controller state. If a value is returned, it is set as the new
|
|
80
|
+
* state. Otherwise, any changes made within that callback to the draft are
|
|
81
|
+
* applied to the controller state.
|
|
82
|
+
*
|
|
83
|
+
* @param callback - Callback for updating state, passed a draft state
|
|
84
|
+
* object. Return a new state object or mutate the draft to update state.
|
|
85
|
+
* @returns An object that has the next state, patches applied in the update and inverse patches to
|
|
86
|
+
* rollback the update.
|
|
87
|
+
*/
|
|
88
|
+
update(callback) {
|
|
89
|
+
// We run into ts2589, "infinite type depth", if we don't cast
|
|
90
|
+
// produceWithPatches here.
|
|
91
|
+
const [nextState, patches, inversePatches] = immer_1.produceWithPatches(__classPrivateFieldGet(this, _BaseController_internalState, "f"), callback);
|
|
92
|
+
// Protect against unnecessary state updates when there is no state diff.
|
|
93
|
+
if (patches.length > 0) {
|
|
94
|
+
__classPrivateFieldSet(this, _BaseController_internalState, nextState, "f");
|
|
95
|
+
this.messagingSystem.publish(`${this.name}:stateChange`, nextState, patches);
|
|
96
|
+
}
|
|
97
|
+
return { nextState, patches, inversePatches };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Applies immer patches to the current state. The patches come from the
|
|
101
|
+
* update function itself and can either be normal or inverse patches.
|
|
102
|
+
*
|
|
103
|
+
* @param patches - An array of immer patches that are to be applied to make
|
|
104
|
+
* or undo changes.
|
|
105
|
+
*/
|
|
106
|
+
applyPatches(patches) {
|
|
107
|
+
const nextState = (0, immer_1.applyPatches)(__classPrivateFieldGet(this, _BaseController_internalState, "f"), patches);
|
|
108
|
+
__classPrivateFieldSet(this, _BaseController_internalState, nextState, "f");
|
|
109
|
+
this.messagingSystem.publish(`${this.name}:stateChange`, nextState, patches);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Prepares the controller for garbage collection. This should be extended
|
|
113
|
+
* by any subclasses to clean up any additional connections or events.
|
|
114
|
+
*
|
|
115
|
+
* The only cleanup performed here is to remove listeners. While technically
|
|
116
|
+
* this is not required to ensure this instance is garbage collected, it at
|
|
117
|
+
* least ensures this instance won't be responsible for preventing the
|
|
118
|
+
* listeners from being garbage collected.
|
|
119
|
+
*/
|
|
120
|
+
destroy() {
|
|
121
|
+
this.messagingSystem.clearEventSubscriptions(`${this.name}:stateChange`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
exports.BaseController = BaseController;
|
|
125
|
+
_BaseController_internalState = new WeakMap();
|
|
126
|
+
/**
|
|
127
|
+
* Returns an anonymized representation of the controller state.
|
|
128
|
+
*
|
|
129
|
+
* By "anonymized" we mean that it should not contain any information that could be personally
|
|
130
|
+
* identifiable.
|
|
131
|
+
*
|
|
132
|
+
* @param state - The controller state.
|
|
133
|
+
* @param metadata - The controller state metadata, which describes how to derive the
|
|
134
|
+
* anonymized state.
|
|
135
|
+
* @returns The anonymized controller state.
|
|
136
|
+
*/
|
|
137
|
+
function getAnonymizedState(state, metadata) {
|
|
138
|
+
return deriveStateFromMetadata(state, metadata, 'anonymous');
|
|
139
|
+
}
|
|
140
|
+
exports.getAnonymizedState = getAnonymizedState;
|
|
141
|
+
/**
|
|
142
|
+
* Returns the subset of state that should be persisted.
|
|
143
|
+
*
|
|
144
|
+
* @param state - The controller state.
|
|
145
|
+
* @param metadata - The controller state metadata, which describes which pieces of state should be persisted.
|
|
146
|
+
* @returns The subset of controller state that should be persisted.
|
|
147
|
+
*/
|
|
148
|
+
function getPersistentState(state, metadata) {
|
|
149
|
+
return deriveStateFromMetadata(state, metadata, 'persist');
|
|
150
|
+
}
|
|
151
|
+
exports.getPersistentState = getPersistentState;
|
|
152
|
+
/**
|
|
153
|
+
* Use the metadata to derive state according to the given metadata property.
|
|
154
|
+
*
|
|
155
|
+
* @param state - The full controller state.
|
|
156
|
+
* @param metadata - The controller metadata.
|
|
157
|
+
* @param metadataProperty - The metadata property to use to derive state.
|
|
158
|
+
* @returns The metadata-derived controller state.
|
|
159
|
+
*/
|
|
160
|
+
function deriveStateFromMetadata(state, metadata, metadataProperty) {
|
|
161
|
+
return Object.keys(state).reduce((derivedState, key) => {
|
|
162
|
+
try {
|
|
163
|
+
const stateMetadata = metadata[key];
|
|
164
|
+
if (!stateMetadata) {
|
|
165
|
+
throw new Error(`No metadata found for '${String(key)}'`);
|
|
166
|
+
}
|
|
167
|
+
const propertyMetadata = stateMetadata[metadataProperty];
|
|
168
|
+
const stateProperty = state[key];
|
|
169
|
+
if (typeof propertyMetadata === 'function') {
|
|
170
|
+
derivedState[key] = propertyMetadata(stateProperty);
|
|
171
|
+
}
|
|
172
|
+
else if (propertyMetadata) {
|
|
173
|
+
derivedState[key] = stateProperty;
|
|
174
|
+
}
|
|
175
|
+
return derivedState;
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
// Throw error after timeout so that it is captured as a console error
|
|
179
|
+
// (and by Sentry) without interrupting state-related operations
|
|
180
|
+
setTimeout(() => {
|
|
181
|
+
throw error;
|
|
182
|
+
});
|
|
183
|
+
return derivedState;
|
|
184
|
+
}
|
|
185
|
+
}, {});
|
|
186
|
+
}
|
|
187
|
+
//# sourceMappingURL=BaseController.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseController.cjs","sourceRoot":"","sources":["../../src/next/BaseController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,iCAAgF;AAShF,IAAA,qBAAa,GAAE,CAAC;AAEhB;;;;;GAKG;AACH,SAAgB,gBAAgB,CAC9B,UAAmB;IAEnB,OAAO,CACL,OAAO,UAAU,KAAK,QAAQ;QAC9B,UAAU,KAAK,IAAI;QACnB,MAAM,IAAI,UAAU;QACpB,OAAO,UAAU,CAAC,IAAI,KAAK,QAAQ;QACnC,OAAO,IAAI,UAAU;QACrB,OAAO,UAAU,CAAC,KAAK,KAAK,QAAQ;QACpC,UAAU,IAAI,UAAU;QACxB,OAAO,UAAU,CAAC,QAAQ,KAAK,QAAQ,CACxC,CAAC;AACJ,CAAC;AAbD,4CAaC;AAiID;;GAEG;AACH,MAAa,cAAc;IA0BzB;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,QAAQ,EACR,IAAI,EACJ,KAAK,GAMN;QAjCD,gDAAgC;QAkC9B,IAAI,CAAC,eAAe,GAA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,CAAC,CAAC;YACjC,IAAI,OAAO,gBAAgB,KAAK,UAAU,EAAE;gBAC1C,YAAY,CAAC,GAAG,CAAC,GAAG,gBAAgB,CAAC,aAAa,CAAC,CAAC;aACrD;iBAAM,IAAI,gBAAgB,EAAE;gBAC3B,YAAY,CAAC,GAAG,CAAC,GAAG,aAAa,CAAC;aACnC;YACD,OAAO,YAAY,CAAC;SACrB;QAAC,OAAO,KAAK,EAAE;YACd,sEAAsE;YACtE,gEAAgE;YAChE,UAAU,CAAC,GAAG,EAAE;gBACd,MAAM,KAAK,CAAC;YACd,CAAC,CAAC,CAAC;YACH,OAAO,YAAY,CAAC;SACrB;IACH,CAAC,EAAE,EAAW,CAAC,CAAC;AAClB,CAAC","sourcesContent":["import type { Json, PublicInterface } from '@metamask/utils';\nimport { enablePatches, produceWithPatches, applyPatches, freeze } from 'immer';\nimport type { Draft, Patch } from 'immer';\n\nimport type { ActionConstraint, EventConstraint } from '../Messenger';\nimport type {\n RestrictedMessenger,\n RestrictedMessengerConstraint,\n} from '../RestrictedMessenger';\n\nenablePatches();\n\n/**\n * Determines if the given controller is an instance of `BaseController`\n *\n * @param controller - Controller instance to check\n * @returns True if the controller is an instance of `BaseController`\n */\nexport function isBaseController(\n controller: unknown,\n): controller is BaseControllerInstance {\n return (\n typeof controller === 'object' &&\n controller !== null &&\n 'name' in controller &&\n typeof controller.name === 'string' &&\n 'state' in controller &&\n typeof controller.state === 'object' &&\n 'metadata' in controller &&\n typeof controller.metadata === 'object'\n );\n}\n\n/**\n * A type that constrains the state of all controllers.\n *\n * In other words, the narrowest supertype encompassing all controller state.\n */\nexport type StateConstraint = Record<string, Json>;\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 */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\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 */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\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 */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport type StateMetadata<T extends StateConstraint> = {\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// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport type StatePropertyMetadata<T extends Json> = {\n persist: boolean | StateDeriver<T>;\n anonymous: boolean | StateDeriver<T>;\n};\n\n/**\n * A universal supertype of `StateDeriver` types.\n * This type can be assigned to any `StateDeriver` type.\n */\nexport type StateDeriverConstraint = (value: never) => Json;\n\n/**\n * A universal supertype of `StatePropertyMetadata` types.\n * This type can be assigned to any `StatePropertyMetadata` type.\n */\nexport type StatePropertyMetadataConstraint = {\n [P in 'anonymous' | 'persist']: boolean | StateDeriverConstraint;\n};\n\n/**\n * A universal supertype of `StateMetadata` types.\n * This type can be assigned to any `StateMetadata` type.\n */\nexport type StateMetadataConstraint = Record<\n string,\n StatePropertyMetadataConstraint\n>;\n\n/**\n * The widest subtype of all controller instances that inherit from `BaseController` (formerly `BaseControllerV2`).\n * Any `BaseController` subclass instance can be assigned to this type.\n */\nexport type BaseControllerInstance = Omit<\n PublicInterface<\n BaseController<string, StateConstraint, RestrictedMessengerConstraint>\n >,\n 'metadata'\n> & {\n metadata: StateMetadataConstraint;\n};\n\nexport type ControllerGetStateAction<\n ControllerName extends string,\n ControllerState extends StateConstraint,\n> = {\n type: `${ControllerName}:getState`;\n handler: () => ControllerState;\n};\n\nexport type ControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraint,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\nexport type ControllerActions<\n ControllerName extends string,\n ControllerState extends StateConstraint,\n> = ControllerGetStateAction<ControllerName, ControllerState>;\n\nexport type ControllerEvents<\n ControllerName extends string,\n ControllerState extends StateConstraint,\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 StateConstraint,\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n messenger extends RestrictedMessenger<\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 * 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 // Here we use `freeze` from Immer to enforce that the state is deeply\n // immutable. Note that this is a runtime check, not a compile-time check.\n // That is, unlike `Object.freeze`, this does not narrow the type\n // recursively to `Readonly`. The equivalent in Immer is `Immutable`, but\n // `Immutable` does not handle recursive types such as our `Json` type.\n this.#internalState = freeze(state, true);\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 // Protect against unnecessary state updates when there is no state diff.\n if (patches.length > 0) {\n this.#internalState = nextState;\n this.messagingSystem.publish(\n `${this.name}:stateChange`,\n nextState,\n patches,\n );\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<ControllerState extends StateConstraint>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n): Record<keyof ControllerState, 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<ControllerState extends StateConstraint>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n): Record<keyof ControllerState, 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 StateConstraint>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n metadataProperty: 'anonymous' | 'persist',\n): Record<keyof ControllerState, Json> {\n return (Object.keys(state) as (keyof ControllerState)[]).reduce<\n Record<keyof ControllerState, Json>\n >((derivedState, 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 derivedState[key] = propertyMetadata(stateProperty);\n } else if (propertyMetadata) {\n derivedState[key] = stateProperty;\n }\n return derivedState;\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 derivedState;\n }\n }, {} as never);\n}\n"]}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { Json, PublicInterface } from "@metamask/utils";
|
|
2
|
+
import type { Draft, Patch } from "immer";
|
|
3
|
+
import type { ActionConstraint, EventConstraint } from "../Messenger.cjs";
|
|
4
|
+
import type { RestrictedMessenger, RestrictedMessengerConstraint } from "../RestrictedMessenger.cjs";
|
|
5
|
+
/**
|
|
6
|
+
* Determines if the given controller is an instance of `BaseController`
|
|
7
|
+
*
|
|
8
|
+
* @param controller - Controller instance to check
|
|
9
|
+
* @returns True if the controller is an instance of `BaseController`
|
|
10
|
+
*/
|
|
11
|
+
export declare function isBaseController(controller: unknown): controller is BaseControllerInstance;
|
|
12
|
+
/**
|
|
13
|
+
* A type that constrains the state of all controllers.
|
|
14
|
+
*
|
|
15
|
+
* In other words, the narrowest supertype encompassing all controller state.
|
|
16
|
+
*/
|
|
17
|
+
export type StateConstraint = Record<string, Json>;
|
|
18
|
+
/**
|
|
19
|
+
* A state change listener.
|
|
20
|
+
*
|
|
21
|
+
* This function will get called for each state change, and is given a copy of
|
|
22
|
+
* the new state along with a set of patches describing the changes since the
|
|
23
|
+
* last update.
|
|
24
|
+
*
|
|
25
|
+
* @param state - The new controller state.
|
|
26
|
+
* @param patches - A list of patches describing any changes (see here for more
|
|
27
|
+
* information: https://immerjs.github.io/immer/docs/patches)
|
|
28
|
+
*/
|
|
29
|
+
export type Listener<T> = (state: T, patches: Patch[]) => void;
|
|
30
|
+
/**
|
|
31
|
+
* An function to derive state.
|
|
32
|
+
*
|
|
33
|
+
* This function will accept one piece of the controller state (one property),
|
|
34
|
+
* and will return some derivation of that state.
|
|
35
|
+
*
|
|
36
|
+
* @param value - A piece of controller state.
|
|
37
|
+
* @returns Something derived from controller state.
|
|
38
|
+
*/
|
|
39
|
+
export type StateDeriver<T extends Json> = (value: T) => Json;
|
|
40
|
+
/**
|
|
41
|
+
* State metadata.
|
|
42
|
+
*
|
|
43
|
+
* This metadata describes which parts of state should be persisted, and how to
|
|
44
|
+
* get an anonymized representation of the state.
|
|
45
|
+
*/
|
|
46
|
+
export type StateMetadata<T extends StateConstraint> = {
|
|
47
|
+
[P in keyof T]-?: StatePropertyMetadata<T[P]>;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Metadata for a single state property
|
|
51
|
+
*
|
|
52
|
+
* @property persist - Indicates whether this property should be persisted
|
|
53
|
+
* (`true` for persistent, `false` for transient), or is set to a function
|
|
54
|
+
* that derives the persistent state from the state.
|
|
55
|
+
* @property anonymous - Indicates whether this property is already anonymous,
|
|
56
|
+
* (`true` for anonymous, `false` if it has potential to be personally
|
|
57
|
+
* identifiable), or is set to a function that returns an anonymized
|
|
58
|
+
* representation of this state.
|
|
59
|
+
*/
|
|
60
|
+
export type StatePropertyMetadata<T extends Json> = {
|
|
61
|
+
persist: boolean | StateDeriver<T>;
|
|
62
|
+
anonymous: boolean | StateDeriver<T>;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* A universal supertype of `StateDeriver` types.
|
|
66
|
+
* This type can be assigned to any `StateDeriver` type.
|
|
67
|
+
*/
|
|
68
|
+
export type StateDeriverConstraint = (value: never) => Json;
|
|
69
|
+
/**
|
|
70
|
+
* A universal supertype of `StatePropertyMetadata` types.
|
|
71
|
+
* This type can be assigned to any `StatePropertyMetadata` type.
|
|
72
|
+
*/
|
|
73
|
+
export type StatePropertyMetadataConstraint = {
|
|
74
|
+
[P in 'anonymous' | 'persist']: boolean | StateDeriverConstraint;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* A universal supertype of `StateMetadata` types.
|
|
78
|
+
* This type can be assigned to any `StateMetadata` type.
|
|
79
|
+
*/
|
|
80
|
+
export type StateMetadataConstraint = Record<string, StatePropertyMetadataConstraint>;
|
|
81
|
+
/**
|
|
82
|
+
* The widest subtype of all controller instances that inherit from `BaseController` (formerly `BaseControllerV2`).
|
|
83
|
+
* Any `BaseController` subclass instance can be assigned to this type.
|
|
84
|
+
*/
|
|
85
|
+
export type BaseControllerInstance = Omit<PublicInterface<BaseController<string, StateConstraint, RestrictedMessengerConstraint>>, 'metadata'> & {
|
|
86
|
+
metadata: StateMetadataConstraint;
|
|
87
|
+
};
|
|
88
|
+
export type ControllerGetStateAction<ControllerName extends string, ControllerState extends StateConstraint> = {
|
|
89
|
+
type: `${ControllerName}:getState`;
|
|
90
|
+
handler: () => ControllerState;
|
|
91
|
+
};
|
|
92
|
+
export type ControllerStateChangeEvent<ControllerName extends string, ControllerState extends StateConstraint> = {
|
|
93
|
+
type: `${ControllerName}:stateChange`;
|
|
94
|
+
payload: [ControllerState, Patch[]];
|
|
95
|
+
};
|
|
96
|
+
export type ControllerActions<ControllerName extends string, ControllerState extends StateConstraint> = ControllerGetStateAction<ControllerName, ControllerState>;
|
|
97
|
+
export type ControllerEvents<ControllerName extends string, ControllerState extends StateConstraint> = ControllerStateChangeEvent<ControllerName, ControllerState>;
|
|
98
|
+
/**
|
|
99
|
+
* Controller class that provides state management, subscriptions, and state metadata
|
|
100
|
+
*/
|
|
101
|
+
export declare class BaseController<ControllerName extends string, ControllerState extends StateConstraint, messenger extends RestrictedMessenger<ControllerName, ActionConstraint | ControllerActions<ControllerName, ControllerState>, EventConstraint | ControllerEvents<ControllerName, ControllerState>, string, string>> {
|
|
102
|
+
#private;
|
|
103
|
+
protected messagingSystem: messenger;
|
|
104
|
+
/**
|
|
105
|
+
* The name of the controller.
|
|
106
|
+
*
|
|
107
|
+
* This is used by the ComposableController to construct a composed application state.
|
|
108
|
+
*/
|
|
109
|
+
readonly name: ControllerName;
|
|
110
|
+
readonly metadata: StateMetadata<ControllerState>;
|
|
111
|
+
/**
|
|
112
|
+
* Creates a BaseController instance.
|
|
113
|
+
*
|
|
114
|
+
* @param options - Controller options.
|
|
115
|
+
* @param options.messenger - Controller messaging system.
|
|
116
|
+
* @param options.metadata - ControllerState metadata, describing how to "anonymize" the state, and which
|
|
117
|
+
* parts should be persisted.
|
|
118
|
+
* @param options.name - The name of the controller, used as a namespace for events and actions.
|
|
119
|
+
* @param options.state - Initial controller state.
|
|
120
|
+
*/
|
|
121
|
+
constructor({ messenger, metadata, name, state, }: {
|
|
122
|
+
messenger: messenger;
|
|
123
|
+
metadata: StateMetadata<ControllerState>;
|
|
124
|
+
name: ControllerName;
|
|
125
|
+
state: ControllerState;
|
|
126
|
+
});
|
|
127
|
+
/**
|
|
128
|
+
* Retrieves current controller state.
|
|
129
|
+
*
|
|
130
|
+
* @returns The current state.
|
|
131
|
+
*/
|
|
132
|
+
get state(): ControllerState;
|
|
133
|
+
set state(_: ControllerState);
|
|
134
|
+
/**
|
|
135
|
+
* Updates controller state. Accepts a callback that is passed a draft copy
|
|
136
|
+
* of the controller state. If a value is returned, it is set as the new
|
|
137
|
+
* state. Otherwise, any changes made within that callback to the draft are
|
|
138
|
+
* applied to the controller state.
|
|
139
|
+
*
|
|
140
|
+
* @param callback - Callback for updating state, passed a draft state
|
|
141
|
+
* object. Return a new state object or mutate the draft to update state.
|
|
142
|
+
* @returns An object that has the next state, patches applied in the update and inverse patches to
|
|
143
|
+
* rollback the update.
|
|
144
|
+
*/
|
|
145
|
+
protected update(callback: (state: Draft<ControllerState>) => void | ControllerState): {
|
|
146
|
+
nextState: ControllerState;
|
|
147
|
+
patches: Patch[];
|
|
148
|
+
inversePatches: Patch[];
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Applies immer patches to the current state. The patches come from the
|
|
152
|
+
* update function itself and can either be normal or inverse patches.
|
|
153
|
+
*
|
|
154
|
+
* @param patches - An array of immer patches that are to be applied to make
|
|
155
|
+
* or undo changes.
|
|
156
|
+
*/
|
|
157
|
+
protected applyPatches(patches: Patch[]): void;
|
|
158
|
+
/**
|
|
159
|
+
* Prepares the controller for garbage collection. This should be extended
|
|
160
|
+
* by any subclasses to clean up any additional connections or events.
|
|
161
|
+
*
|
|
162
|
+
* The only cleanup performed here is to remove listeners. While technically
|
|
163
|
+
* this is not required to ensure this instance is garbage collected, it at
|
|
164
|
+
* least ensures this instance won't be responsible for preventing the
|
|
165
|
+
* listeners from being garbage collected.
|
|
166
|
+
*/
|
|
167
|
+
protected destroy(): void;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Returns an anonymized representation of the controller state.
|
|
171
|
+
*
|
|
172
|
+
* By "anonymized" we mean that it should not contain any information that could be personally
|
|
173
|
+
* identifiable.
|
|
174
|
+
*
|
|
175
|
+
* @param state - The controller state.
|
|
176
|
+
* @param metadata - The controller state metadata, which describes how to derive the
|
|
177
|
+
* anonymized state.
|
|
178
|
+
* @returns The anonymized controller state.
|
|
179
|
+
*/
|
|
180
|
+
export declare function getAnonymizedState<ControllerState extends StateConstraint>(state: ControllerState, metadata: StateMetadata<ControllerState>): Record<keyof ControllerState, Json>;
|
|
181
|
+
/**
|
|
182
|
+
* Returns the subset of state that should be persisted.
|
|
183
|
+
*
|
|
184
|
+
* @param state - The controller state.
|
|
185
|
+
* @param metadata - The controller state metadata, which describes which pieces of state should be persisted.
|
|
186
|
+
* @returns The subset of controller state that should be persisted.
|
|
187
|
+
*/
|
|
188
|
+
export declare function getPersistentState<ControllerState extends StateConstraint>(state: ControllerState, metadata: StateMetadata<ControllerState>): Record<keyof ControllerState, Json>;
|
|
189
|
+
//# sourceMappingURL=BaseController.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseController.d.cts","sourceRoot":"","sources":["../../src/next/BaseController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,eAAe,EAAE,wBAAwB;AAE7D,OAAO,KAAK,EAAE,KAA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QAAQ,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC;IAEzD;;;;;;;;;OASG;gBACS,EACV,SAAS,EACT,QAAQ,EACR,IAAI,EACJ,KAAK,GACN,EAAE;QACD,SAAS,EAAE,SAAS,CAAC;QACrB,QAAQ,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC;QACzC,IAAI,EAAE,cAAc,CAAC;QACrB,KAAK,EAAE,eAAe,CAAC;KACxB;IAsBD;;;;OAIG;IACH,IAAI,KAAK,oBAER;IAED,IAAI,KAAK,CAAC,CAAC,iBAAA,EAIV;IAED;;;;;;;;;;OAUG;IACH,SAAS,CAAC,MAAM,CACd,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,eAAe,CAAC,KAAK,IAAI,GAAG,eAAe,GAClE;QACD,SAAS,EAAE,eAAe,CAAC;QAC3B,OAAO,EAAE,KAAK,EAAE,CAAC;QACjB,cAAc,EAAE,KAAK,EAAE,CAAC;KACzB;IAuBD;;;;;;OAMG;IACH,SAAS,CAAC,YAAY,CAAC,OAAO,EAAE,KAAK,EAAE;IAUvC;;;;;;;;OAQG;IACH,SAAS,CAAC,OAAO;CAGlB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,eAAe,SAAS,eAAe,EACxE,KAAK,EAAE,eAAe,EACtB,QAAQ,EAAE,aAAa,CAAC,eAAe,CAAC,GACvC,MAAM,CAAC,MAAM,eAAe,EAAE,IAAI,CAAC,CAErC;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,eAAe,SAAS,eAAe,EACxE,KAAK,EAAE,eAAe,EACtB,QAAQ,EAAE,aAAa,CAAC,eAAe,CAAC,GACvC,MAAM,CAAC,MAAM,eAAe,EAAE,IAAI,CAAC,CAErC"}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { Json, PublicInterface } from "@metamask/utils";
|
|
2
|
+
import type { Draft, Patch } from "immer";
|
|
3
|
+
import type { ActionConstraint, EventConstraint } from "../Messenger.mjs";
|
|
4
|
+
import type { RestrictedMessenger, RestrictedMessengerConstraint } from "../RestrictedMessenger.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* Determines if the given controller is an instance of `BaseController`
|
|
7
|
+
*
|
|
8
|
+
* @param controller - Controller instance to check
|
|
9
|
+
* @returns True if the controller is an instance of `BaseController`
|
|
10
|
+
*/
|
|
11
|
+
export declare function isBaseController(controller: unknown): controller is BaseControllerInstance;
|
|
12
|
+
/**
|
|
13
|
+
* A type that constrains the state of all controllers.
|
|
14
|
+
*
|
|
15
|
+
* In other words, the narrowest supertype encompassing all controller state.
|
|
16
|
+
*/
|
|
17
|
+
export type StateConstraint = Record<string, Json>;
|
|
18
|
+
/**
|
|
19
|
+
* A state change listener.
|
|
20
|
+
*
|
|
21
|
+
* This function will get called for each state change, and is given a copy of
|
|
22
|
+
* the new state along with a set of patches describing the changes since the
|
|
23
|
+
* last update.
|
|
24
|
+
*
|
|
25
|
+
* @param state - The new controller state.
|
|
26
|
+
* @param patches - A list of patches describing any changes (see here for more
|
|
27
|
+
* information: https://immerjs.github.io/immer/docs/patches)
|
|
28
|
+
*/
|
|
29
|
+
export type Listener<T> = (state: T, patches: Patch[]) => void;
|
|
30
|
+
/**
|
|
31
|
+
* An function to derive state.
|
|
32
|
+
*
|
|
33
|
+
* This function will accept one piece of the controller state (one property),
|
|
34
|
+
* and will return some derivation of that state.
|
|
35
|
+
*
|
|
36
|
+
* @param value - A piece of controller state.
|
|
37
|
+
* @returns Something derived from controller state.
|
|
38
|
+
*/
|
|
39
|
+
export type StateDeriver<T extends Json> = (value: T) => Json;
|
|
40
|
+
/**
|
|
41
|
+
* State metadata.
|
|
42
|
+
*
|
|
43
|
+
* This metadata describes which parts of state should be persisted, and how to
|
|
44
|
+
* get an anonymized representation of the state.
|
|
45
|
+
*/
|
|
46
|
+
export type StateMetadata<T extends StateConstraint> = {
|
|
47
|
+
[P in keyof T]-?: StatePropertyMetadata<T[P]>;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Metadata for a single state property
|
|
51
|
+
*
|
|
52
|
+
* @property persist - Indicates whether this property should be persisted
|
|
53
|
+
* (`true` for persistent, `false` for transient), or is set to a function
|
|
54
|
+
* that derives the persistent state from the state.
|
|
55
|
+
* @property anonymous - Indicates whether this property is already anonymous,
|
|
56
|
+
* (`true` for anonymous, `false` if it has potential to be personally
|
|
57
|
+
* identifiable), or is set to a function that returns an anonymized
|
|
58
|
+
* representation of this state.
|
|
59
|
+
*/
|
|
60
|
+
export type StatePropertyMetadata<T extends Json> = {
|
|
61
|
+
persist: boolean | StateDeriver<T>;
|
|
62
|
+
anonymous: boolean | StateDeriver<T>;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* A universal supertype of `StateDeriver` types.
|
|
66
|
+
* This type can be assigned to any `StateDeriver` type.
|
|
67
|
+
*/
|
|
68
|
+
export type StateDeriverConstraint = (value: never) => Json;
|
|
69
|
+
/**
|
|
70
|
+
* A universal supertype of `StatePropertyMetadata` types.
|
|
71
|
+
* This type can be assigned to any `StatePropertyMetadata` type.
|
|
72
|
+
*/
|
|
73
|
+
export type StatePropertyMetadataConstraint = {
|
|
74
|
+
[P in 'anonymous' | 'persist']: boolean | StateDeriverConstraint;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* A universal supertype of `StateMetadata` types.
|
|
78
|
+
* This type can be assigned to any `StateMetadata` type.
|
|
79
|
+
*/
|
|
80
|
+
export type StateMetadataConstraint = Record<string, StatePropertyMetadataConstraint>;
|
|
81
|
+
/**
|
|
82
|
+
* The widest subtype of all controller instances that inherit from `BaseController` (formerly `BaseControllerV2`).
|
|
83
|
+
* Any `BaseController` subclass instance can be assigned to this type.
|
|
84
|
+
*/
|
|
85
|
+
export type BaseControllerInstance = Omit<PublicInterface<BaseController<string, StateConstraint, RestrictedMessengerConstraint>>, 'metadata'> & {
|
|
86
|
+
metadata: StateMetadataConstraint;
|
|
87
|
+
};
|
|
88
|
+
export type ControllerGetStateAction<ControllerName extends string, ControllerState extends StateConstraint> = {
|
|
89
|
+
type: `${ControllerName}:getState`;
|
|
90
|
+
handler: () => ControllerState;
|
|
91
|
+
};
|
|
92
|
+
export type ControllerStateChangeEvent<ControllerName extends string, ControllerState extends StateConstraint> = {
|
|
93
|
+
type: `${ControllerName}:stateChange`;
|
|
94
|
+
payload: [ControllerState, Patch[]];
|
|
95
|
+
};
|
|
96
|
+
export type ControllerActions<ControllerName extends string, ControllerState extends StateConstraint> = ControllerGetStateAction<ControllerName, ControllerState>;
|
|
97
|
+
export type ControllerEvents<ControllerName extends string, ControllerState extends StateConstraint> = ControllerStateChangeEvent<ControllerName, ControllerState>;
|
|
98
|
+
/**
|
|
99
|
+
* Controller class that provides state management, subscriptions, and state metadata
|
|
100
|
+
*/
|
|
101
|
+
export declare class BaseController<ControllerName extends string, ControllerState extends StateConstraint, messenger extends RestrictedMessenger<ControllerName, ActionConstraint | ControllerActions<ControllerName, ControllerState>, EventConstraint | ControllerEvents<ControllerName, ControllerState>, string, string>> {
|
|
102
|
+
#private;
|
|
103
|
+
protected messagingSystem: messenger;
|
|
104
|
+
/**
|
|
105
|
+
* The name of the controller.
|
|
106
|
+
*
|
|
107
|
+
* This is used by the ComposableController to construct a composed application state.
|
|
108
|
+
*/
|
|
109
|
+
readonly name: ControllerName;
|
|
110
|
+
readonly metadata: StateMetadata<ControllerState>;
|
|
111
|
+
/**
|
|
112
|
+
* Creates a BaseController instance.
|
|
113
|
+
*
|
|
114
|
+
* @param options - Controller options.
|
|
115
|
+
* @param options.messenger - Controller messaging system.
|
|
116
|
+
* @param options.metadata - ControllerState metadata, describing how to "anonymize" the state, and which
|
|
117
|
+
* parts should be persisted.
|
|
118
|
+
* @param options.name - The name of the controller, used as a namespace for events and actions.
|
|
119
|
+
* @param options.state - Initial controller state.
|
|
120
|
+
*/
|
|
121
|
+
constructor({ messenger, metadata, name, state, }: {
|
|
122
|
+
messenger: messenger;
|
|
123
|
+
metadata: StateMetadata<ControllerState>;
|
|
124
|
+
name: ControllerName;
|
|
125
|
+
state: ControllerState;
|
|
126
|
+
});
|
|
127
|
+
/**
|
|
128
|
+
* Retrieves current controller state.
|
|
129
|
+
*
|
|
130
|
+
* @returns The current state.
|
|
131
|
+
*/
|
|
132
|
+
get state(): ControllerState;
|
|
133
|
+
set state(_: ControllerState);
|
|
134
|
+
/**
|
|
135
|
+
* Updates controller state. Accepts a callback that is passed a draft copy
|
|
136
|
+
* of the controller state. If a value is returned, it is set as the new
|
|
137
|
+
* state. Otherwise, any changes made within that callback to the draft are
|
|
138
|
+
* applied to the controller state.
|
|
139
|
+
*
|
|
140
|
+
* @param callback - Callback for updating state, passed a draft state
|
|
141
|
+
* object. Return a new state object or mutate the draft to update state.
|
|
142
|
+
* @returns An object that has the next state, patches applied in the update and inverse patches to
|
|
143
|
+
* rollback the update.
|
|
144
|
+
*/
|
|
145
|
+
protected update(callback: (state: Draft<ControllerState>) => void | ControllerState): {
|
|
146
|
+
nextState: ControllerState;
|
|
147
|
+
patches: Patch[];
|
|
148
|
+
inversePatches: Patch[];
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Applies immer patches to the current state. The patches come from the
|
|
152
|
+
* update function itself and can either be normal or inverse patches.
|
|
153
|
+
*
|
|
154
|
+
* @param patches - An array of immer patches that are to be applied to make
|
|
155
|
+
* or undo changes.
|
|
156
|
+
*/
|
|
157
|
+
protected applyPatches(patches: Patch[]): void;
|
|
158
|
+
/**
|
|
159
|
+
* Prepares the controller for garbage collection. This should be extended
|
|
160
|
+
* by any subclasses to clean up any additional connections or events.
|
|
161
|
+
*
|
|
162
|
+
* The only cleanup performed here is to remove listeners. While technically
|
|
163
|
+
* this is not required to ensure this instance is garbage collected, it at
|
|
164
|
+
* least ensures this instance won't be responsible for preventing the
|
|
165
|
+
* listeners from being garbage collected.
|
|
166
|
+
*/
|
|
167
|
+
protected destroy(): void;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Returns an anonymized representation of the controller state.
|
|
171
|
+
*
|
|
172
|
+
* By "anonymized" we mean that it should not contain any information that could be personally
|
|
173
|
+
* identifiable.
|
|
174
|
+
*
|
|
175
|
+
* @param state - The controller state.
|
|
176
|
+
* @param metadata - The controller state metadata, which describes how to derive the
|
|
177
|
+
* anonymized state.
|
|
178
|
+
* @returns The anonymized controller state.
|
|
179
|
+
*/
|
|
180
|
+
export declare function getAnonymizedState<ControllerState extends StateConstraint>(state: ControllerState, metadata: StateMetadata<ControllerState>): Record<keyof ControllerState, Json>;
|
|
181
|
+
/**
|
|
182
|
+
* Returns the subset of state that should be persisted.
|
|
183
|
+
*
|
|
184
|
+
* @param state - The controller state.
|
|
185
|
+
* @param metadata - The controller state metadata, which describes which pieces of state should be persisted.
|
|
186
|
+
* @returns The subset of controller state that should be persisted.
|
|
187
|
+
*/
|
|
188
|
+
export declare function getPersistentState<ControllerState extends StateConstraint>(state: ControllerState, metadata: StateMetadata<ControllerState>): Record<keyof ControllerState, Json>;
|
|
189
|
+
//# sourceMappingURL=BaseController.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseController.d.mts","sourceRoot":"","sources":["../../src/next/BaseController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,eAAe,EAAE,wBAAwB;AAE7D,OAAO,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,cAAc;AAE1C,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,yBAAqB;AACtE,OAAO,KAAK,EACV,mBAAmB,EACnB,6BAA6B,EAC9B,mCAA+B;AAIhC;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,OAAO,GAClB,UAAU,IAAI,sBAAsB,CAWtC;AAED;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAEnD;;;;;;;;;;GAUG;AAGH,MAAM,MAAM,QAA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H,SAAS,CAAC,MAAM,CACd,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,eAAe,CAAC,KAAK,IAAI,GAAG,eAAe,GAClE;QACD,SAAS,EAAE,eAAe,CAAC;QAC3B,OAAO,EAAE,KAAK,EAAE,CAAC;QACjB,cAAc,EAAE,KAAK,EAAE,CAAC;KACzB;IAuBD;;;;;;OAMG;IACH,SAAS,CAAC,YAAY,CAAC,OAAO,EAAE,KAAK,EAAE;IAUvC;;;;;;;;OAQG;IACH,SAAS,CAAC,OAAO;CAGlB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,eAAe,SAAS,eAAe,EACxE,KAAK,EAAE,eAAe,EACtB,QAAQ,EAAE,aAAa,CAAC,eAAe,CAAC,GACvC,MAAM,CAAC,MAAM,eAAe,EAAE,IAAI,CAAC,CAErC;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,eAAe,SAAS,eAAe,EACxE,KAAK,EAAE,eAAe,EACtB,QAAQ,EAAE,aAAa,CAAC,eAAe,CAAC,GACvC,MAAM,CAAC,MAAM,eAAe,EAAE,IAAI,CAAC,CAErC"}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
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");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
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");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _BaseController_internalState;
|
|
13
|
+
import { enablePatches, produceWithPatches, applyPatches, freeze } from "immer";
|
|
14
|
+
enablePatches();
|
|
15
|
+
/**
|
|
16
|
+
* Determines if the given controller is an instance of `BaseController`
|
|
17
|
+
*
|
|
18
|
+
* @param controller - Controller instance to check
|
|
19
|
+
* @returns True if the controller is an instance of `BaseController`
|
|
20
|
+
*/
|
|
21
|
+
export function isBaseController(controller) {
|
|
22
|
+
return (typeof controller === 'object' &&
|
|
23
|
+
controller !== null &&
|
|
24
|
+
'name' in controller &&
|
|
25
|
+
typeof controller.name === 'string' &&
|
|
26
|
+
'state' in controller &&
|
|
27
|
+
typeof controller.state === 'object' &&
|
|
28
|
+
'metadata' in controller &&
|
|
29
|
+
typeof controller.metadata === 'object');
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Controller class that provides state management, subscriptions, and state metadata
|
|
33
|
+
*/
|
|
34
|
+
export class BaseController {
|
|
35
|
+
/**
|
|
36
|
+
* Creates a BaseController instance.
|
|
37
|
+
*
|
|
38
|
+
* @param options - Controller options.
|
|
39
|
+
* @param options.messenger - Controller messaging system.
|
|
40
|
+
* @param options.metadata - ControllerState metadata, describing how to "anonymize" the state, and which
|
|
41
|
+
* parts should be persisted.
|
|
42
|
+
* @param options.name - The name of the controller, used as a namespace for events and actions.
|
|
43
|
+
* @param options.state - Initial controller state.
|
|
44
|
+
*/
|
|
45
|
+
constructor({ messenger, metadata, name, state, }) {
|
|
46
|
+
_BaseController_internalState.set(this, void 0);
|
|
47
|
+
this.messagingSystem = messenger;
|
|
48
|
+
this.name = name;
|
|
49
|
+
// Here we use `freeze` from Immer to enforce that the state is deeply
|
|
50
|
+
// immutable. Note that this is a runtime check, not a compile-time check.
|
|
51
|
+
// That is, unlike `Object.freeze`, this does not narrow the type
|
|
52
|
+
// recursively to `Readonly`. The equivalent in Immer is `Immutable`, but
|
|
53
|
+
// `Immutable` does not handle recursive types such as our `Json` type.
|
|
54
|
+
__classPrivateFieldSet(this, _BaseController_internalState, freeze(state, true), "f");
|
|
55
|
+
this.metadata = metadata;
|
|
56
|
+
this.messagingSystem.registerActionHandler(`${name}:getState`, () => this.state);
|
|
57
|
+
this.messagingSystem.registerInitialEventPayload({
|
|
58
|
+
eventType: `${name}:stateChange`,
|
|
59
|
+
getPayload: () => [this.state, []],
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Retrieves current controller state.
|
|
64
|
+
*
|
|
65
|
+
* @returns The current state.
|
|
66
|
+
*/
|
|
67
|
+
get state() {
|
|
68
|
+
return __classPrivateFieldGet(this, _BaseController_internalState, "f");
|
|
69
|
+
}
|
|
70
|
+
set state(_) {
|
|
71
|
+
throw new Error(`Controller state cannot be directly mutated; use 'update' method instead.`);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Updates controller state. Accepts a callback that is passed a draft copy
|
|
75
|
+
* of the controller state. If a value is returned, it is set as the new
|
|
76
|
+
* state. Otherwise, any changes made within that callback to the draft are
|
|
77
|
+
* applied to the controller state.
|
|
78
|
+
*
|
|
79
|
+
* @param callback - Callback for updating state, passed a draft state
|
|
80
|
+
* object. Return a new state object or mutate the draft to update state.
|
|
81
|
+
* @returns An object that has the next state, patches applied in the update and inverse patches to
|
|
82
|
+
* rollback the update.
|
|
83
|
+
*/
|
|
84
|
+
update(callback) {
|
|
85
|
+
// We run into ts2589, "infinite type depth", if we don't cast
|
|
86
|
+
// produceWithPatches here.
|
|
87
|
+
const [nextState, patches, inversePatches] = produceWithPatches(__classPrivateFieldGet(this, _BaseController_internalState, "f"), callback);
|
|
88
|
+
// Protect against unnecessary state updates when there is no state diff.
|
|
89
|
+
if (patches.length > 0) {
|
|
90
|
+
__classPrivateFieldSet(this, _BaseController_internalState, nextState, "f");
|
|
91
|
+
this.messagingSystem.publish(`${this.name}:stateChange`, nextState, patches);
|
|
92
|
+
}
|
|
93
|
+
return { nextState, patches, inversePatches };
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Applies immer patches to the current state. The patches come from the
|
|
97
|
+
* update function itself and can either be normal or inverse patches.
|
|
98
|
+
*
|
|
99
|
+
* @param patches - An array of immer patches that are to be applied to make
|
|
100
|
+
* or undo changes.
|
|
101
|
+
*/
|
|
102
|
+
applyPatches(patches) {
|
|
103
|
+
const nextState = applyPatches(__classPrivateFieldGet(this, _BaseController_internalState, "f"), patches);
|
|
104
|
+
__classPrivateFieldSet(this, _BaseController_internalState, nextState, "f");
|
|
105
|
+
this.messagingSystem.publish(`${this.name}:stateChange`, nextState, patches);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Prepares the controller for garbage collection. This should be extended
|
|
109
|
+
* by any subclasses to clean up any additional connections or events.
|
|
110
|
+
*
|
|
111
|
+
* The only cleanup performed here is to remove listeners. While technically
|
|
112
|
+
* this is not required to ensure this instance is garbage collected, it at
|
|
113
|
+
* least ensures this instance won't be responsible for preventing the
|
|
114
|
+
* listeners from being garbage collected.
|
|
115
|
+
*/
|
|
116
|
+
destroy() {
|
|
117
|
+
this.messagingSystem.clearEventSubscriptions(`${this.name}:stateChange`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
_BaseController_internalState = new WeakMap();
|
|
121
|
+
/**
|
|
122
|
+
* Returns an anonymized representation of the controller state.
|
|
123
|
+
*
|
|
124
|
+
* By "anonymized" we mean that it should not contain any information that could be personally
|
|
125
|
+
* identifiable.
|
|
126
|
+
*
|
|
127
|
+
* @param state - The controller state.
|
|
128
|
+
* @param metadata - The controller state metadata, which describes how to derive the
|
|
129
|
+
* anonymized state.
|
|
130
|
+
* @returns The anonymized controller state.
|
|
131
|
+
*/
|
|
132
|
+
export function getAnonymizedState(state, metadata) {
|
|
133
|
+
return deriveStateFromMetadata(state, metadata, 'anonymous');
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Returns the subset of state that should be persisted.
|
|
137
|
+
*
|
|
138
|
+
* @param state - The controller state.
|
|
139
|
+
* @param metadata - The controller state metadata, which describes which pieces of state should be persisted.
|
|
140
|
+
* @returns The subset of controller state that should be persisted.
|
|
141
|
+
*/
|
|
142
|
+
export function getPersistentState(state, metadata) {
|
|
143
|
+
return deriveStateFromMetadata(state, metadata, 'persist');
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Use the metadata to derive state according to the given metadata property.
|
|
147
|
+
*
|
|
148
|
+
* @param state - The full controller state.
|
|
149
|
+
* @param metadata - The controller metadata.
|
|
150
|
+
* @param metadataProperty - The metadata property to use to derive state.
|
|
151
|
+
* @returns The metadata-derived controller state.
|
|
152
|
+
*/
|
|
153
|
+
function deriveStateFromMetadata(state, metadata, metadataProperty) {
|
|
154
|
+
return Object.keys(state).reduce((derivedState, key) => {
|
|
155
|
+
try {
|
|
156
|
+
const stateMetadata = metadata[key];
|
|
157
|
+
if (!stateMetadata) {
|
|
158
|
+
throw new Error(`No metadata found for '${String(key)}'`);
|
|
159
|
+
}
|
|
160
|
+
const propertyMetadata = stateMetadata[metadataProperty];
|
|
161
|
+
const stateProperty = state[key];
|
|
162
|
+
if (typeof propertyMetadata === 'function') {
|
|
163
|
+
derivedState[key] = propertyMetadata(stateProperty);
|
|
164
|
+
}
|
|
165
|
+
else if (propertyMetadata) {
|
|
166
|
+
derivedState[key] = stateProperty;
|
|
167
|
+
}
|
|
168
|
+
return derivedState;
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
// Throw error after timeout so that it is captured as a console error
|
|
172
|
+
// (and by Sentry) without interrupting state-related operations
|
|
173
|
+
setTimeout(() => {
|
|
174
|
+
throw error;
|
|
175
|
+
});
|
|
176
|
+
return derivedState;
|
|
177
|
+
}
|
|
178
|
+
}, {});
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=BaseController.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseController.mjs","sourceRoot":"","sources":["../../src/next/BaseController.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc;AAShF,aAAa,EAAE,CAAC;AAEhB;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAC9B,UAAmB;IAEnB,OAAO,CACL,OAAO,UAAU,KAAK,QAAQ;QAC9B,UAAU,KAAK,IAAI;QACnB,MAAM,IAAI,UAAU;QACpB,OAAO,UAAU,CAAC,IAAI,KAAK,QAAQ;QACnC,OAAO,IAAI,UAAU;QACrB,OAAO,UAAU,CAAC,KAAK,KAAK,QAAQ;QACpC,UAAU,IAAI,UAAU;QACxB,OAAO,UAAU,CAAC,QAAQ,KAAK,QAAQ,CACxC,CAAC;AACJ,CAAC;AAiID;;GAEG;AACH,MAAM,OAAO,cAAc;IA0BzB;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,QAAQ,EACR,IAAI,EACJ,KAAK,GAMN;QAjCD,gDAAgC;QAkC9B,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,sEAAsE;QACtE,0EAA0E;QAC1E,iEAAiE;QACjE,yEAAyE;QACzE,uEAAuE;QACvE,uBAAA,IAAI,iCAAkB,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,MAAA,CAAC;QAC1C,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,GA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ACtB,QAAwC;IAExC,OAAO,uBAAuB,CAAC,KAAK,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;AAC/D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAChC,KAAsB,EACtB,QAAwC;IAExC,OAAO,uBAAuB,CAAC,KAAK,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;AAC7D,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,uBAAuB,CAC9B,KAAsB,EACtB,QAAwC,EACxC,gBAAyC;IAEzC,OAAQ,MAAM,CAAC,IAAI,CAAC,KAAK,CAA+B,CAAC,MAAM,CAE7D,CAAC,YAAY,EAAE,GAAG,EAAE,EAAE;QACtB,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,YAAY,CAAC,GAAG,CAAC,GAAG,gBAAgB,CAAC,aAAa,CAAC,CAAC;aACrD;iBAAM,IAAI,gBAAgB,EAAE;gBAC3B,YAAY,CAAC,GAAG,CAAC,GAAG,aAAa,CAAC;aACnC;YACD,OAAO,YAAY,CAAC;SACrB;QAAC,OAAO,KAAK,EAAE;YACd,sEAAsE;YACtE,gEAAgE;YAChE,UAAU,CAAC,GAAG,EAAE;gBACd,MAAM,KAAK,CAAC;YACd,CAAC,CAAC,CAAC;YACH,OAAO,YAAY,CAAC;SACrB;IACH,CAAC,EAAE,EAAW,CAAC,CAAC;AAClB,CAAC","sourcesContent":["import type { Json, PublicInterface } from '@metamask/utils';\nimport { enablePatches, produceWithPatches, applyPatches, freeze } from 'immer';\nimport type { Draft, Patch } from 'immer';\n\nimport type { ActionConstraint, EventConstraint } from '../Messenger';\nimport type {\n RestrictedMessenger,\n RestrictedMessengerConstraint,\n} from '../RestrictedMessenger';\n\nenablePatches();\n\n/**\n * Determines if the given controller is an instance of `BaseController`\n *\n * @param controller - Controller instance to check\n * @returns True if the controller is an instance of `BaseController`\n */\nexport function isBaseController(\n controller: unknown,\n): controller is BaseControllerInstance {\n return (\n typeof controller === 'object' &&\n controller !== null &&\n 'name' in controller &&\n typeof controller.name === 'string' &&\n 'state' in controller &&\n typeof controller.state === 'object' &&\n 'metadata' in controller &&\n typeof controller.metadata === 'object'\n );\n}\n\n/**\n * A type that constrains the state of all controllers.\n *\n * In other words, the narrowest supertype encompassing all controller state.\n */\nexport type StateConstraint = Record<string, Json>;\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 */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\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 */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\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 */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport type StateMetadata<T extends StateConstraint> = {\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// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport type StatePropertyMetadata<T extends Json> = {\n persist: boolean | StateDeriver<T>;\n anonymous: boolean | StateDeriver<T>;\n};\n\n/**\n * A universal supertype of `StateDeriver` types.\n * This type can be assigned to any `StateDeriver` type.\n */\nexport type StateDeriverConstraint = (value: never) => Json;\n\n/**\n * A universal supertype of `StatePropertyMetadata` types.\n * This type can be assigned to any `StatePropertyMetadata` type.\n */\nexport type StatePropertyMetadataConstraint = {\n [P in 'anonymous' | 'persist']: boolean | StateDeriverConstraint;\n};\n\n/**\n * A universal supertype of `StateMetadata` types.\n * This type can be assigned to any `StateMetadata` type.\n */\nexport type StateMetadataConstraint = Record<\n string,\n StatePropertyMetadataConstraint\n>;\n\n/**\n * The widest subtype of all controller instances that inherit from `BaseController` (formerly `BaseControllerV2`).\n * Any `BaseController` subclass instance can be assigned to this type.\n */\nexport type BaseControllerInstance = Omit<\n PublicInterface<\n BaseController<string, StateConstraint, RestrictedMessengerConstraint>\n >,\n 'metadata'\n> & {\n metadata: StateMetadataConstraint;\n};\n\nexport type ControllerGetStateAction<\n ControllerName extends string,\n ControllerState extends StateConstraint,\n> = {\n type: `${ControllerName}:getState`;\n handler: () => ControllerState;\n};\n\nexport type ControllerStateChangeEvent<\n ControllerName extends string,\n ControllerState extends StateConstraint,\n> = {\n type: `${ControllerName}:stateChange`;\n payload: [ControllerState, Patch[]];\n};\n\nexport type ControllerActions<\n ControllerName extends string,\n ControllerState extends StateConstraint,\n> = ControllerGetStateAction<ControllerName, ControllerState>;\n\nexport type ControllerEvents<\n ControllerName extends string,\n ControllerState extends StateConstraint,\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 StateConstraint,\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n messenger extends RestrictedMessenger<\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 * 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 // Here we use `freeze` from Immer to enforce that the state is deeply\n // immutable. Note that this is a runtime check, not a compile-time check.\n // That is, unlike `Object.freeze`, this does not narrow the type\n // recursively to `Readonly`. The equivalent in Immer is `Immutable`, but\n // `Immutable` does not handle recursive types such as our `Json` type.\n this.#internalState = freeze(state, true);\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 // Protect against unnecessary state updates when there is no state diff.\n if (patches.length > 0) {\n this.#internalState = nextState;\n this.messagingSystem.publish(\n `${this.name}:stateChange`,\n nextState,\n patches,\n );\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<ControllerState extends StateConstraint>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n): Record<keyof ControllerState, 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<ControllerState extends StateConstraint>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n): Record<keyof ControllerState, 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 StateConstraint>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n metadataProperty: 'anonymous' | 'persist',\n): Record<keyof ControllerState, Json> {\n return (Object.keys(state) as (keyof ControllerState)[]).reduce<\n Record<keyof ControllerState, Json>\n >((derivedState, 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 derivedState[key] = propertyMetadata(stateProperty);\n } else if (propertyMetadata) {\n derivedState[key] = stateProperty;\n }\n return derivedState;\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 derivedState;\n }\n }, {} as never);\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBaseController = exports.getPersistentState = exports.getAnonymizedState = exports.BaseController = void 0;
|
|
4
|
+
var BaseController_1 = require("./BaseController.cjs");
|
|
5
|
+
Object.defineProperty(exports, "BaseController", { enumerable: true, get: function () { return BaseController_1.BaseController; } });
|
|
6
|
+
Object.defineProperty(exports, "getAnonymizedState", { enumerable: true, get: function () { return BaseController_1.getAnonymizedState; } });
|
|
7
|
+
Object.defineProperty(exports, "getPersistentState", { enumerable: true, get: function () { return BaseController_1.getPersistentState; } });
|
|
8
|
+
Object.defineProperty(exports, "isBaseController", { enumerable: true, get: function () { return BaseController_1.isBaseController; } });
|
|
9
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/next/index.ts"],"names":[],"mappings":";;;AAaA,uDAK0B;AAJxB,gHAAA,cAAc,OAAA;AACd,oHAAA,kBAAkB,OAAA;AAClB,oHAAA,kBAAkB,OAAA;AAClB,kHAAA,gBAAgB,OAAA","sourcesContent":["export type {\n BaseControllerInstance,\n Listener as ListenerV2,\n StateConstraint,\n StateDeriver,\n StateDeriverConstraint,\n StateMetadata,\n StateMetadataConstraint,\n StatePropertyMetadata,\n StatePropertyMetadataConstraint,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from './BaseController';\nexport {\n BaseController,\n getAnonymizedState,\n getPersistentState,\n isBaseController,\n} from './BaseController';\n"]}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export type { BaseControllerInstance, Listener as ListenerV2, StateConstraint, StateDeriver, StateDeriverConstraint, StateMetadata, StateMetadataConstraint, StatePropertyMetadata, StatePropertyMetadataConstraint, ControllerGetStateAction, ControllerStateChangeEvent, } from "./BaseController.cjs";
|
|
2
|
+
export { BaseController, getAnonymizedState, getPersistentState, isBaseController, } from "./BaseController.cjs";
|
|
3
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/next/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,sBAAsB,EACtB,QAAQ,IAAI,UAAU,EACtB,eAAe,EACf,YAAY,EACZ,sBAAsB,EACtB,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,+BAA+B,EAC/B,wBAAwB,EACxB,0BAA0B,GAC3B,6BAAyB;AAC1B,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,GACjB,6BAAyB"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export type { BaseControllerInstance, Listener as ListenerV2, StateConstraint, StateDeriver, StateDeriverConstraint, StateMetadata, StateMetadataConstraint, StatePropertyMetadata, StatePropertyMetadataConstraint, ControllerGetStateAction, ControllerStateChangeEvent, } from "./BaseController.mjs";
|
|
2
|
+
export { BaseController, getAnonymizedState, getPersistentState, isBaseController, } from "./BaseController.mjs";
|
|
3
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/next/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,sBAAsB,EACtB,QAAQ,IAAI,UAAU,EACtB,eAAe,EACf,YAAY,EACZ,sBAAsB,EACtB,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,+BAA+B,EAC/B,wBAAwB,EACxB,0BAA0B,GAC3B,6BAAyB;AAC1B,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,GACjB,6BAAyB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/next/index.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,EACjB,6BAAyB","sourcesContent":["export type {\n BaseControllerInstance,\n Listener as ListenerV2,\n StateConstraint,\n StateDeriver,\n StateDeriverConstraint,\n StateMetadata,\n StateMetadataConstraint,\n StatePropertyMetadata,\n StatePropertyMetadataConstraint,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from './BaseController';\nexport {\n BaseController,\n getAnonymizedState,\n getPersistentState,\n isBaseController,\n} from './BaseController';\n"]}
|
package/next.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/base-controller",
|
|
3
|
-
"version": "8.1.0-preview-
|
|
3
|
+
"version": "8.1.0-preview-7dfc3145",
|
|
4
4
|
"description": "Provides scaffolding for controllers as well a communication system for all controllers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -26,12 +26,23 @@
|
|
|
26
26
|
"default": "./dist/index.cjs"
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
|
+
"./next": {
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./dist/next/index.d.mts",
|
|
32
|
+
"default": "./dist/next/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./dist/next/index.d.cts",
|
|
36
|
+
"default": "./dist/next/index.cjs"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
29
39
|
"./package.json": "./package.json"
|
|
30
40
|
},
|
|
31
41
|
"main": "./dist/index.cjs",
|
|
32
42
|
"types": "./dist/index.d.cts",
|
|
33
43
|
"files": [
|
|
34
|
-
"dist/"
|
|
44
|
+
"dist/",
|
|
45
|
+
"next.js"
|
|
35
46
|
],
|
|
36
47
|
"scripts": {
|
|
37
48
|
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
|