@metamask-previews/base-controller 8.4.2-preview-9fa15fd0 → 9.0.0-preview-46d2c977
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 +18 -4
- package/dist/BaseController.cjs +23 -57
- package/dist/BaseController.cjs.map +1 -1
- package/dist/BaseController.d.cts +16 -45
- package/dist/BaseController.d.cts.map +1 -1
- package/dist/BaseController.d.mts +16 -45
- package/dist/BaseController.d.mts.map +1 -1
- package/dist/BaseController.mjs +22 -53
- package/dist/BaseController.mjs.map +1 -1
- package/dist/index.cjs +1 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -6
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -6
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -14
- package/dist/Messenger.cjs +0 -279
- package/dist/Messenger.cjs.map +0 -1
- package/dist/Messenger.d.cts +0 -231
- package/dist/Messenger.d.cts.map +0 -1
- package/dist/Messenger.d.mts +0 -231
- package/dist/Messenger.d.mts.map +0 -1
- package/dist/Messenger.mjs +0 -275
- package/dist/Messenger.mjs.map +0 -1
- package/dist/RestrictedMessenger.cjs +0 -242
- package/dist/RestrictedMessenger.cjs.map +0 -1
- package/dist/RestrictedMessenger.d.cts +0 -200
- package/dist/RestrictedMessenger.d.cts.map +0 -1
- package/dist/RestrictedMessenger.d.mts +0 -200
- package/dist/RestrictedMessenger.d.mts.map +0 -1
- package/dist/RestrictedMessenger.mjs +0 -238
- package/dist/RestrictedMessenger.mjs.map +0 -1
- package/dist/next/BaseController.cjs +0 -168
- package/dist/next/BaseController.cjs.map +0 -1
- package/dist/next/BaseController.d.cts +0 -206
- package/dist/next/BaseController.d.cts.map +0 -1
- package/dist/next/BaseController.d.mts +0 -206
- package/dist/next/BaseController.d.mts.map +0 -1
- package/dist/next/BaseController.mjs +0 -163
- package/dist/next/BaseController.mjs.map +0 -1
- package/dist/next/index.cjs +0 -7
- package/dist/next/index.cjs.map +0 -1
- package/dist/next/index.d.cts +0 -3
- package/dist/next/index.d.cts.map +0 -1
- package/dist/next/index.d.mts +0 -3
- package/dist/next/index.d.mts.map +0 -1
- package/dist/next/index.mjs +0 -2
- package/dist/next/index.mjs.map +0 -1
- package/next.d.ts +0 -7
- package/next.js +0 -3
package/dist/BaseController.mjs
CHANGED
|
@@ -9,25 +9,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
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
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _BaseController_internalState;
|
|
12
|
+
var _BaseController_internalState, _BaseController_messenger;
|
|
13
13
|
import { enablePatches, produceWithPatches, applyPatches, freeze } from "immer";
|
|
14
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
15
|
/**
|
|
32
16
|
* Controller class that provides state management, subscriptions, and state metadata
|
|
33
17
|
*/
|
|
@@ -36,15 +20,28 @@ export class BaseController {
|
|
|
36
20
|
* Creates a BaseController instance.
|
|
37
21
|
*
|
|
38
22
|
* @param options - Controller options.
|
|
39
|
-
* @param options.messenger -
|
|
23
|
+
* @param options.messenger - The controller messenger.
|
|
40
24
|
* @param options.metadata - ControllerState metadata, describing how to "anonymize" the state, and which
|
|
41
25
|
* parts should be persisted.
|
|
42
26
|
* @param options.name - The name of the controller, used as a namespace for events and actions.
|
|
43
27
|
* @param options.state - Initial controller state.
|
|
44
28
|
*/
|
|
45
29
|
constructor({ messenger, metadata, name, state, }) {
|
|
30
|
+
/**
|
|
31
|
+
* The controller state.
|
|
32
|
+
*/
|
|
46
33
|
_BaseController_internalState.set(this, void 0);
|
|
47
|
-
|
|
34
|
+
/**
|
|
35
|
+
* The controller messenger.
|
|
36
|
+
*
|
|
37
|
+
* This is the same as the `messenger` property, but has a type that only lets us use
|
|
38
|
+
* actions and events that are part of the `BaseController` class.
|
|
39
|
+
*/
|
|
40
|
+
_BaseController_messenger.set(this, void 0);
|
|
41
|
+
// The parameter type validates that the expected actions/events are present
|
|
42
|
+
// We don't have a way to validate the type property because the type is invariant
|
|
43
|
+
__classPrivateFieldSet(this, _BaseController_messenger, messenger, "f");
|
|
44
|
+
this.messenger = messenger;
|
|
48
45
|
this.name = name;
|
|
49
46
|
// Here we use `freeze` from Immer to enforce that the state is deeply
|
|
50
47
|
// immutable. Note that this is a runtime check, not a compile-time check.
|
|
@@ -53,8 +50,8 @@ export class BaseController {
|
|
|
53
50
|
// `Immutable` does not handle recursive types such as our `Json` type.
|
|
54
51
|
__classPrivateFieldSet(this, _BaseController_internalState, freeze(state, true), "f");
|
|
55
52
|
this.metadata = metadata;
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
53
|
+
__classPrivateFieldGet(this, _BaseController_messenger, "f").registerActionHandler(`${name}:getState`, () => this.state);
|
|
54
|
+
__classPrivateFieldGet(this, _BaseController_messenger, "f").registerInitialEventPayload({
|
|
58
55
|
eventType: `${name}:stateChange`,
|
|
59
56
|
getPayload: () => [this.state, []],
|
|
60
57
|
});
|
|
@@ -88,7 +85,7 @@ export class BaseController {
|
|
|
88
85
|
// Protect against unnecessary state updates when there is no state diff.
|
|
89
86
|
if (patches.length > 0) {
|
|
90
87
|
__classPrivateFieldSet(this, _BaseController_internalState, nextState, "f");
|
|
91
|
-
this.
|
|
88
|
+
__classPrivateFieldGet(this, _BaseController_messenger, "f").publish(`${this.name}:stateChange`, nextState, patches);
|
|
92
89
|
}
|
|
93
90
|
return { nextState, patches, inversePatches };
|
|
94
91
|
}
|
|
@@ -102,7 +99,7 @@ export class BaseController {
|
|
|
102
99
|
applyPatches(patches) {
|
|
103
100
|
const nextState = applyPatches(__classPrivateFieldGet(this, _BaseController_internalState, "f"), patches);
|
|
104
101
|
__classPrivateFieldSet(this, _BaseController_internalState, nextState, "f");
|
|
105
|
-
this.
|
|
102
|
+
__classPrivateFieldGet(this, _BaseController_messenger, "f").publish(`${this.name}:stateChange`, nextState, patches);
|
|
106
103
|
}
|
|
107
104
|
/**
|
|
108
105
|
* Prepares the controller for garbage collection. This should be extended
|
|
@@ -114,38 +111,10 @@ export class BaseController {
|
|
|
114
111
|
* listeners from being garbage collected.
|
|
115
112
|
*/
|
|
116
113
|
destroy() {
|
|
117
|
-
this.
|
|
114
|
+
this.messenger.clearEventSubscriptions(`${this.name}:stateChange`);
|
|
118
115
|
}
|
|
119
116
|
}
|
|
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
|
-
* @deprecated Use `deriveStateFromMetadata` instead.
|
|
128
|
-
* @param state - The controller state.
|
|
129
|
-
* @param metadata - The controller state metadata, which describes how to derive the
|
|
130
|
-
* anonymized state.
|
|
131
|
-
* @param captureException - Reports an error to an error monitoring service.
|
|
132
|
-
* @returns The anonymized controller state.
|
|
133
|
-
*/
|
|
134
|
-
export function getAnonymizedState(state, metadata, captureException) {
|
|
135
|
-
return deriveStateFromMetadata(state, metadata, 'anonymous', captureException);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Returns the subset of state that should be persisted.
|
|
139
|
-
*
|
|
140
|
-
* @deprecated Use `deriveStateFromMetadata` instead.
|
|
141
|
-
* @param state - The controller state.
|
|
142
|
-
* @param metadata - The controller state metadata, which describes which pieces of state should be persisted.
|
|
143
|
-
* @param captureException - Reports an error to an error monitoring service.
|
|
144
|
-
* @returns The subset of controller state that should be persisted.
|
|
145
|
-
*/
|
|
146
|
-
export function getPersistentState(state, metadata, captureException) {
|
|
147
|
-
return deriveStateFromMetadata(state, metadata, 'persist', captureException);
|
|
148
|
-
}
|
|
117
|
+
_BaseController_internalState = new WeakMap(), _BaseController_messenger = new WeakMap();
|
|
149
118
|
/**
|
|
150
119
|
* Use the metadata to derive state according to the given metadata property.
|
|
151
120
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseController.mjs","sourceRoot":"","sources":["../src/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;AA+JD;;GAEG;AACH,MAAM,OAAO,cAAc;IA0BzB;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,QAAQ,EACR,IAAI,EACJ,KAAK,GAMN;QAjCD,gDAAgC;QAkC9B,IAAI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gB,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,8DAA8D;YAC9D,2HAA2H;YAC3H,IAAI,gBAAgB,EAAE;gBACpB,IAAI;oBACF,gBAAgB,CACd,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAC1D,CAAC;iBACH;gBAAC,OAAO,qBAAqB,EAAE;oBAC9B,OAAO,CAAC,KAAK,CACX,IAAI,KAAK,CAAC,8CAA8C,CAAC,EACzD,qBAAqB,CACtB,CAAC;oBACF,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;iBACtB;aACF;iBAAM;gBACL,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;aACtB;YACD,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 */\nexport type StatePropertyMetadata<ControllerState extends Json> = {\n /**\n * Indicates whether this property should be included in debug snapshots attached to Sentry\n * errors.\n *\n * Set this to false if the state may contain personally identifiable information, or if it's\n * too large to include in a debug snapshot.\n */\n anonymous: boolean | StateDeriver<ControllerState>;\n /**\n * Indicates whether this property should be included in state logs.\n *\n * Set this to false if the data should be kept hidden from support agents (e.g. if it contains\n * secret keys, or personally-identifiable information that is not useful for debugging).\n *\n * We do allow state logs to contain some personally identifiable information to assist with\n * diagnosing errors (e.g. transaction hashes, addresses), but we still attempt to limit the\n * data we expose to what is most useful for helping users.\n */\n includeInStateLogs?: boolean | StateDeriver<ControllerState>;\n /**\n * Indicates whether this property should be persisted.\n *\n * If true, the property will be persisted and saved between sessions.\n * If false, the property will not be saved between sessions, and it will always be missing from the `state` constructor parameter.\n */\n persist: boolean | StateDeriver<ControllerState>;\n /**\n * Indicates whether this property is used by the UI.\n *\n * If true, the property will be accessible from the UI.\n * If false, it will be inaccessible from the UI.\n *\n * Making a property accessible to the UI has a performance overhead, so it's better to set this\n * to `false` if it's not used in the UI, especially for properties that can be large in size.\n *\n * Note that we disallow the use of a state derivation function here to preserve type information\n * for the UI (the state deriver type always returns `Json`).\n */\n usedInUi?: boolean;\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 anonymous: boolean | StateDeriverConstraint;\n includeInStateLogs?: boolean | StateDeriverConstraint;\n persist: boolean | StateDeriverConstraint;\n usedInUi?: boolean;\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 * @deprecated Use `deriveStateFromMetadata` instead.\n * @param state - The controller state.\n * @param metadata - The controller state metadata, which describes how to derive the\n * anonymized state.\n * @param captureException - Reports an error to an error monitoring service.\n * @returns The anonymized controller state.\n */\nexport function getAnonymizedState<ControllerState extends StateConstraint>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n captureException?: (error: Error) => void,\n): Record<keyof ControllerState, Json> {\n return deriveStateFromMetadata(\n state,\n metadata,\n 'anonymous',\n captureException,\n );\n}\n\n/**\n * Returns the subset of state that should be persisted.\n *\n * @deprecated Use `deriveStateFromMetadata` instead.\n * @param state - The controller state.\n * @param metadata - The controller state metadata, which describes which pieces of state should be persisted.\n * @param captureException - Reports an error to an error monitoring service.\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 captureException?: (error: Error) => void,\n): Record<keyof ControllerState, Json> {\n return deriveStateFromMetadata(state, metadata, 'persist', captureException);\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 * @param captureException - Reports an error to an error monitoring service.\n * @returns The metadata-derived controller state.\n */\nexport function deriveStateFromMetadata<\n ControllerState extends StateConstraint,\n>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n metadataProperty: keyof StatePropertyMetadata<Json>,\n captureException?: (error: Error) => void,\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 // Capture error without interrupting state-related operations\n // See [ADR core#0016](https://github.com/MetaMask/decisions/blob/main/decisions/core/0016-core-classes-error-reporting.md)\n if (captureException) {\n try {\n captureException(\n error instanceof Error ? error : new Error(String(error)),\n );\n } catch (captureExceptionError) {\n console.error(\n new Error(`Error thrown when calling 'captureException'`),\n captureExceptionError,\n );\n console.error(error);\n }\n } else {\n console.error(error);\n }\n return derivedState;\n }\n }, {} as never);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"BaseController.mjs","sourceRoot":"","sources":["../src/BaseController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAQA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc;AAGhF,aAAa,EAAE,CAAC;AAmKhB;;GAEG;AACH,MAAM,OAAO,cAAc;IA4CzB;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,QAAQ,EACR,IAAI,EACJ,KAAK,GAgBN;QA7DD;;WAEG;QACH,gDAAgC;QAOhC;;;;;WAKG;QACM,4CAIP;QA0CA,4EAA4E;QAC5E,kFAAkF;QAClF,uBAAA,IAAI,6BAAc,SAIjB,MAAA,CAAC;QACF,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,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,uBAAA,IAAI,iCAAW,CAAC,qBAAqB,CAAC,GAAG,IAAI,WAAW,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAE5E,uBAAA,IAAI,iCAAW,CAAC,2BAA2B,CAAC;YAC1C,SA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ACrB;IACH,CAAC,EAAE,EAAW,CAAC,CAAC;AAClB,CAAC","sourcesContent":["import type {\n ActionConstraint,\n EventConstraint,\n Messenger,\n MessengerActions,\n MessengerEvents,\n} from '@metamask/messenger';\nimport type { Json, PublicInterface } from '@metamask/utils';\nimport { enablePatches, produceWithPatches, applyPatches, freeze } from 'immer';\nimport type { Draft, Patch } from 'immer';\n\nenablePatches();\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 */\nexport type StateChangeListener<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 */\nexport type StatePropertyMetadata<ControllerState extends Json> = {\n /**\n * Indicates whether this property should be included in debug snapshots attached to Sentry\n * errors.\n *\n * Set this to false if the state may contain personally identifiable information, or if it's\n * too large to include in a debug snapshot.\n */\n includeInDebugSnapshot: boolean | StateDeriver<ControllerState>;\n /**\n * Indicates whether this property should be included in state logs.\n *\n * Set this to false if the data should be kept hidden from support agents (e.g. if it contains\n * secret keys, or personally-identifiable information that is not useful for debugging).\n *\n * We do allow state logs to contain some personally identifiable information to assist with\n * diagnosing errors (e.g. transaction hashes, addresses), but we still attempt to limit the\n * data we expose to what is most useful for helping users.\n */\n includeInStateLogs: boolean | StateDeriver<ControllerState>;\n /**\n * Indicates whether this property should be persisted.\n *\n * If true, the property will be persisted and saved between sessions.\n * If false, the property will not be saved between sessions, and it will always be missing from the `state` constructor parameter.\n */\n persist: boolean | StateDeriver<ControllerState>;\n /**\n * Indicates whether this property is used by the UI.\n *\n * If true, the property will be accessible from the UI.\n * If false, it will be inaccessible from the UI.\n *\n * Making a property accessible to the UI has a performance overhead, so it's better to set this\n * to `false` if it's not used in the UI, especially for properties that can be large in size.\n *\n * Note that we disallow the use of a state derivation function here to preserve type information\n * for the UI (the state deriver type always returns `Json`).\n */\n usedInUi: boolean;\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 includeInDebugSnapshot: boolean | StateDeriverConstraint;\n includeInStateLogs: boolean | StateDeriverConstraint;\n persist: boolean | StateDeriverConstraint;\n usedInUi: boolean;\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<\n string,\n StateConstraint,\n // Use `any` to allow any parent to be set.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Messenger<string, ActionConstraint, EventConstraint, any>\n >\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 ControllerMessenger extends Messenger<\n ControllerName,\n ActionConstraint,\n EventConstraint,\n // Use `any` to allow any parent to be set. `any` is harmless in a type constraint anyway,\n // it's the one totally safe place to use it.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n >,\n> {\n /**\n * The controller state.\n */\n #internalState: ControllerState;\n\n /**\n * The controller messenger. This is used to interact with other parts of the application.\n */\n protected messenger: ControllerMessenger;\n\n /**\n * The controller messenger.\n *\n * This is the same as the `messenger` property, but has a type that only lets us use\n * actions and events that are part of the `BaseController` class.\n */\n readonly #messenger: Messenger<\n ControllerName,\n ControllerActions<ControllerName, ControllerState>,\n ControllerEvents<ControllerName, ControllerState>\n >;\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 - The controller messenger.\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: ControllerActions<\n ControllerName,\n ControllerState\n >['type'] extends MessengerActions<ControllerMessenger>['type']\n ? ControllerEvents<\n ControllerName,\n ControllerState\n >['type'] extends MessengerEvents<ControllerMessenger>['type']\n ? ControllerMessenger\n : never\n : never;\n metadata: StateMetadata<ControllerState>;\n name: ControllerName;\n state: ControllerState;\n }) {\n // The parameter type validates that the expected actions/events are present\n // We don't have a way to validate the type property because the type is invariant\n this.#messenger = messenger as unknown as Messenger<\n ControllerName,\n ControllerActions<ControllerName, ControllerState>,\n ControllerEvents<ControllerName, ControllerState>\n >;\n this.messenger = 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.#messenger.registerActionHandler(`${name}:getState`, () => this.state);\n\n this.#messenger.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.#messenger.publish(\n `${this.name}:stateChange` as const,\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.#messenger.publish(\n `${this.name}:stateChange` as const,\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.messenger.clearEventSubscriptions(`${this.name}:stateChange`);\n }\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 * @param captureException - Reports an error to an error monitoring service.\n * @returns The metadata-derived controller state.\n */\nexport function deriveStateFromMetadata<\n ControllerState extends StateConstraint,\n>(\n state: ControllerState,\n metadata: StateMetadata<ControllerState>,\n metadataProperty: keyof StatePropertyMetadata<Json>,\n captureException?: (error: Error) => void,\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 // Capture error without interrupting state-related operations\n // See [ADR core#0016](https://github.com/MetaMask/decisions/blob/main/decisions/core/0016-core-classes-error-reporting.md)\n if (captureException) {\n try {\n captureException(\n error instanceof Error ? error : new Error(String(error)),\n );\n } catch (captureExceptionError) {\n console.error(\n new Error(`Error thrown when calling 'captureException'`),\n captureExceptionError,\n );\n console.error(error);\n }\n } else {\n console.error(error);\n }\n return derivedState;\n }\n }, {} as never);\n}\n"]}
|
package/dist/index.cjs
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.deriveStateFromMetadata = exports.BaseController = void 0;
|
|
4
4
|
var BaseController_1 = require("./BaseController.cjs");
|
|
5
5
|
Object.defineProperty(exports, "BaseController", { enumerable: true, get: function () { return BaseController_1.BaseController; } });
|
|
6
6
|
Object.defineProperty(exports, "deriveStateFromMetadata", { enumerable: true, get: function () { return BaseController_1.deriveStateFromMetadata; } });
|
|
7
|
-
Object.defineProperty(exports, "getAnonymizedState", { enumerable: true, get: function () { return BaseController_1.getAnonymizedState; } });
|
|
8
|
-
Object.defineProperty(exports, "getPersistentState", { enumerable: true, get: function () { return BaseController_1.getPersistentState; } });
|
|
9
|
-
Object.defineProperty(exports, "isBaseController", { enumerable: true, get: function () { return BaseController_1.isBaseController; } });
|
|
10
|
-
var Messenger_1 = require("./Messenger.cjs");
|
|
11
|
-
Object.defineProperty(exports, "Messenger", { enumerable: true, get: function () { return Messenger_1.Messenger; } });
|
|
12
|
-
var RestrictedMessenger_1 = require("./RestrictedMessenger.cjs");
|
|
13
|
-
Object.defineProperty(exports, "RestrictedMessenger", { enumerable: true, get: function () { return RestrictedMessenger_1.RestrictedMessenger; } });
|
|
14
7
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAaA,
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAaA,uDAA2E;AAAlE,gHAAA,cAAc,OAAA;AAAE,yHAAA,uBAAuB,OAAA","sourcesContent":["export type {\n BaseControllerInstance,\n StateChangeListener,\n StateConstraint,\n StateDeriver,\n StateDeriverConstraint,\n StateMetadata,\n StateMetadataConstraint,\n StatePropertyMetadata,\n StatePropertyMetadataConstraint,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from './BaseController';\nexport { BaseController, deriveStateFromMetadata } from './BaseController';\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
export type { BaseControllerInstance,
|
|
2
|
-
export { BaseController, deriveStateFromMetadata
|
|
3
|
-
export type { ActionHandler, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, GenericEventHandler, SelectorFunction, ActionConstraint, EventConstraint, NamespacedBy, NotNamespacedBy, NamespacedName, } from "./Messenger.cjs";
|
|
4
|
-
export { Messenger } from "./Messenger.cjs";
|
|
5
|
-
export type { RestrictedMessengerConstraint } from "./RestrictedMessenger.cjs";
|
|
6
|
-
export { RestrictedMessenger } from "./RestrictedMessenger.cjs";
|
|
1
|
+
export type { BaseControllerInstance, StateChangeListener, StateConstraint, StateDeriver, StateDeriverConstraint, StateMetadata, StateMetadataConstraint, StatePropertyMetadata, StatePropertyMetadataConstraint, ControllerGetStateAction, ControllerStateChangeEvent, } from "./BaseController.cjs";
|
|
2
|
+
export { BaseController, deriveStateFromMetadata } from "./BaseController.cjs";
|
|
7
3
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,sBAAsB,EACtB,
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,sBAAsB,EACtB,mBAAmB,EACnB,eAAe,EACf,YAAY,EACZ,sBAAsB,EACtB,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,+BAA+B,EAC/B,wBAAwB,EACxB,0BAA0B,GAC3B,6BAAyB;AAC1B,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,6BAAyB"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
export type { BaseControllerInstance,
|
|
2
|
-
export { BaseController, deriveStateFromMetadata
|
|
3
|
-
export type { ActionHandler, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, GenericEventHandler, SelectorFunction, ActionConstraint, EventConstraint, NamespacedBy, NotNamespacedBy, NamespacedName, } from "./Messenger.mjs";
|
|
4
|
-
export { Messenger } from "./Messenger.mjs";
|
|
5
|
-
export type { RestrictedMessengerConstraint } from "./RestrictedMessenger.mjs";
|
|
6
|
-
export { RestrictedMessenger } from "./RestrictedMessenger.mjs";
|
|
1
|
+
export type { BaseControllerInstance, StateChangeListener, StateConstraint, StateDeriver, StateDeriverConstraint, StateMetadata, StateMetadataConstraint, StatePropertyMetadata, StatePropertyMetadataConstraint, ControllerGetStateAction, ControllerStateChangeEvent, } from "./BaseController.mjs";
|
|
2
|
+
export { BaseController, deriveStateFromMetadata } from "./BaseController.mjs";
|
|
7
3
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,sBAAsB,EACtB,
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,sBAAsB,EACtB,mBAAmB,EACnB,eAAe,EACf,YAAY,EACZ,sBAAsB,EACtB,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,+BAA+B,EAC/B,wBAAwB,EACxB,0BAA0B,GAC3B,6BAAyB;AAC1B,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,6BAAyB"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
export { BaseController, deriveStateFromMetadata
|
|
2
|
-
export { Messenger } from "./Messenger.mjs";
|
|
3
|
-
export { RestrictedMessenger } from "./RestrictedMessenger.mjs";
|
|
1
|
+
export { BaseController, deriveStateFromMetadata } from "./BaseController.mjs";
|
|
4
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,6BAAyB","sourcesContent":["export type {\n BaseControllerInstance,\n StateChangeListener,\n StateConstraint,\n StateDeriver,\n StateDeriverConstraint,\n StateMetadata,\n StateMetadataConstraint,\n StatePropertyMetadata,\n StatePropertyMetadataConstraint,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from './BaseController';\nexport { BaseController, deriveStateFromMetadata } from './BaseController';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/base-controller",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0-preview-46d2c977",
|
|
4
4
|
"description": "Provides scaffolding for controllers as well a communication system for all controllers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -26,24 +26,12 @@
|
|
|
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
|
-
},
|
|
39
29
|
"./package.json": "./package.json"
|
|
40
30
|
},
|
|
41
31
|
"main": "./dist/index.cjs",
|
|
42
32
|
"types": "./dist/index.d.cts",
|
|
43
33
|
"files": [
|
|
44
|
-
"dist/"
|
|
45
|
-
"next.js",
|
|
46
|
-
"next.d.ts"
|
|
34
|
+
"dist/"
|
|
47
35
|
],
|
|
48
36
|
"scripts": {
|
|
49
37
|
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
|
package/dist/Messenger.cjs
DELETED
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
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");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var _Messenger_actions, _Messenger_events, _Messenger_initialEventPayloadGetters, _Messenger_eventPayloadCache;
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.Messenger = void 0;
|
|
10
|
-
const RestrictedMessenger_1 = require("./RestrictedMessenger.cjs");
|
|
11
|
-
/**
|
|
12
|
-
* A message broker for "actions" and "events".
|
|
13
|
-
*
|
|
14
|
-
* The messenger allows registering functions as 'actions' that can be called elsewhere,
|
|
15
|
-
* and it allows publishing and subscribing to events. Both actions and events are identified by
|
|
16
|
-
* unique strings.
|
|
17
|
-
*
|
|
18
|
-
* @template Action - A type union of all Action types.
|
|
19
|
-
* @template Event - A type union of all Event types.
|
|
20
|
-
*/
|
|
21
|
-
class Messenger {
|
|
22
|
-
constructor() {
|
|
23
|
-
_Messenger_actions.set(this, new Map());
|
|
24
|
-
_Messenger_events.set(this, new Map());
|
|
25
|
-
/**
|
|
26
|
-
* A map of functions for getting the initial event payload.
|
|
27
|
-
*
|
|
28
|
-
* Used only for events that represent state changes.
|
|
29
|
-
*/
|
|
30
|
-
_Messenger_initialEventPayloadGetters.set(this, new Map());
|
|
31
|
-
/**
|
|
32
|
-
* A cache of selector return values for their respective handlers.
|
|
33
|
-
*/
|
|
34
|
-
_Messenger_eventPayloadCache.set(this, new Map());
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Register an action handler.
|
|
38
|
-
*
|
|
39
|
-
* This will make the registered function available to call via the `call` method.
|
|
40
|
-
*
|
|
41
|
-
* @param actionType - The action type. This is a unique identifier for this action.
|
|
42
|
-
* @param handler - The action handler. This function gets called when the `call` method is
|
|
43
|
-
* invoked with the given action type.
|
|
44
|
-
* @throws Will throw when a handler has been registered for this action type already.
|
|
45
|
-
* @template ActionType - A type union of Action type strings.
|
|
46
|
-
*/
|
|
47
|
-
registerActionHandler(actionType, handler) {
|
|
48
|
-
if (__classPrivateFieldGet(this, _Messenger_actions, "f").has(actionType)) {
|
|
49
|
-
throw new Error(`A handler for ${actionType} has already been registered`);
|
|
50
|
-
}
|
|
51
|
-
__classPrivateFieldGet(this, _Messenger_actions, "f").set(actionType, handler);
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Registers action handlers for a list of methods on a messenger client
|
|
55
|
-
*
|
|
56
|
-
* @param messengerClient - The object that is expected to make use of the messenger.
|
|
57
|
-
* @param methodNames - The names of the methods on the messenger client to register as action
|
|
58
|
-
* handlers.
|
|
59
|
-
* @template MessengerClient - The type expected to make use of the messenger.
|
|
60
|
-
* @template MethodNames - The type union of method names to register as action handlers.
|
|
61
|
-
*/
|
|
62
|
-
registerMethodActionHandlers(messengerClient, methodNames) {
|
|
63
|
-
for (const methodName of methodNames) {
|
|
64
|
-
const method = messengerClient[methodName];
|
|
65
|
-
if (typeof method === 'function') {
|
|
66
|
-
const actionType = `${messengerClient.name}:${methodName}`;
|
|
67
|
-
this.registerActionHandler(actionType, method.bind(messengerClient));
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Unregister an action handler.
|
|
73
|
-
*
|
|
74
|
-
* This will prevent this action from being called.
|
|
75
|
-
*
|
|
76
|
-
* @param actionType - The action type. This is a unique identifier for this action.
|
|
77
|
-
* @template ActionType - A type union of Action type strings.
|
|
78
|
-
*/
|
|
79
|
-
unregisterActionHandler(actionType) {
|
|
80
|
-
__classPrivateFieldGet(this, _Messenger_actions, "f").delete(actionType);
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Unregister all action handlers.
|
|
84
|
-
*
|
|
85
|
-
* This prevents all actions from being called.
|
|
86
|
-
*/
|
|
87
|
-
clearActions() {
|
|
88
|
-
__classPrivateFieldGet(this, _Messenger_actions, "f").clear();
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Call an action.
|
|
92
|
-
*
|
|
93
|
-
* This function will call the action handler corresponding to the given action type, passing
|
|
94
|
-
* along any parameters given.
|
|
95
|
-
*
|
|
96
|
-
* @param actionType - The action type. This is a unique identifier for this action.
|
|
97
|
-
* @param params - The action parameters. These must match the type of the parameters of the
|
|
98
|
-
* registered action handler.
|
|
99
|
-
* @throws Will throw when no handler has been registered for the given type.
|
|
100
|
-
* @template ActionType - A type union of Action type strings.
|
|
101
|
-
* @returns The action return value.
|
|
102
|
-
*/
|
|
103
|
-
call(actionType, ...params) {
|
|
104
|
-
const handler = __classPrivateFieldGet(this, _Messenger_actions, "f").get(actionType);
|
|
105
|
-
if (!handler) {
|
|
106
|
-
throw new Error(`A handler for ${actionType} has not been registered`);
|
|
107
|
-
}
|
|
108
|
-
return handler(...params);
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Register a function for getting the initial payload for an event.
|
|
112
|
-
*
|
|
113
|
-
* This is used for events that represent a state change, where the payload is the state.
|
|
114
|
-
* Registering a function for getting the payload allows event selectors to have a point of
|
|
115
|
-
* comparison the first time state changes.
|
|
116
|
-
*
|
|
117
|
-
* @param args - The arguments to this function
|
|
118
|
-
* @param args.eventType - The event type to register a payload for.
|
|
119
|
-
* @param args.getPayload - A function for retrieving the event payload.
|
|
120
|
-
* @template EventType - A type union of Event type strings.
|
|
121
|
-
*/
|
|
122
|
-
registerInitialEventPayload({ eventType, getPayload, }) {
|
|
123
|
-
__classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").set(eventType, getPayload);
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Publish an event.
|
|
127
|
-
*
|
|
128
|
-
* Publishes the given payload to all subscribers of the given event type.
|
|
129
|
-
*
|
|
130
|
-
* Note that this method should never throw directly. Any errors from
|
|
131
|
-
* subscribers are captured and re-thrown in a timeout handler.
|
|
132
|
-
*
|
|
133
|
-
* @param eventType - The event type. This is a unique identifier for this event.
|
|
134
|
-
* @param payload - The event payload. The type of the parameters for each event handler must
|
|
135
|
-
* match the type of this payload.
|
|
136
|
-
* @template EventType - A type union of Event type strings.
|
|
137
|
-
*/
|
|
138
|
-
publish(eventType, ...payload) {
|
|
139
|
-
const subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
140
|
-
if (subscribers) {
|
|
141
|
-
for (const [handler, selector] of subscribers.entries()) {
|
|
142
|
-
try {
|
|
143
|
-
if (selector) {
|
|
144
|
-
const previousValue = __classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").get(handler);
|
|
145
|
-
const newValue = selector(...payload);
|
|
146
|
-
if (newValue !== previousValue) {
|
|
147
|
-
__classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").set(handler, newValue);
|
|
148
|
-
handler(newValue, previousValue);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
handler(...payload);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
catch (error) {
|
|
156
|
-
// Throw error after timeout so that it is capured as a console error
|
|
157
|
-
// (and by Sentry) without interrupting the event publishing.
|
|
158
|
-
setTimeout(() => {
|
|
159
|
-
throw error;
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
subscribe(eventType, handler, selector) {
|
|
166
|
-
let subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
167
|
-
if (!subscribers) {
|
|
168
|
-
subscribers = new Map();
|
|
169
|
-
__classPrivateFieldGet(this, _Messenger_events, "f").set(eventType, subscribers);
|
|
170
|
-
}
|
|
171
|
-
// Widen type of event handler by dropping ReturnType parameter.
|
|
172
|
-
//
|
|
173
|
-
// We need to drop it here because it's used as the parameter to the event handler, and
|
|
174
|
-
// functions in general are contravarient over the parameter type. This means the type is no
|
|
175
|
-
// longer valid once it's added to a broader type union with other handlers (because as far
|
|
176
|
-
// as TypeScript knows, we might call the handler with output from a different selector).
|
|
177
|
-
//
|
|
178
|
-
// This cast means the type system is not guaranteeing the handler is called with the matching
|
|
179
|
-
// input selector return value. The parameter types do ensure they match when `subscribe` is
|
|
180
|
-
// called, but past that point we need to make sure of that with manual review and tests
|
|
181
|
-
// instead.
|
|
182
|
-
const widenedHandler = handler;
|
|
183
|
-
subscribers.set(widenedHandler, selector);
|
|
184
|
-
if (selector) {
|
|
185
|
-
const getPayload = __classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").get(eventType);
|
|
186
|
-
if (getPayload) {
|
|
187
|
-
const initialValue = selector(...getPayload());
|
|
188
|
-
__classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").set(widenedHandler, initialValue);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Unsubscribe from an event.
|
|
194
|
-
*
|
|
195
|
-
* Unregisters the given function as an event handler for the given event.
|
|
196
|
-
*
|
|
197
|
-
* @param eventType - The event type. This is a unique identifier for this event.
|
|
198
|
-
* @param handler - The event handler to unregister.
|
|
199
|
-
* @throws Will throw when the given event handler is not registered for this event.
|
|
200
|
-
* @template EventType - A type union of Event type strings.
|
|
201
|
-
* @template SelectorReturnValue - The selector return value.
|
|
202
|
-
*/
|
|
203
|
-
unsubscribe(eventType, handler) {
|
|
204
|
-
const subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
205
|
-
// Widen type of event handler by dropping ReturnType parameter.
|
|
206
|
-
//
|
|
207
|
-
// We need to drop it here because it's used as the parameter to the event handler, and
|
|
208
|
-
// functions in general are contravarient over the parameter type. This means the type is no
|
|
209
|
-
// longer valid once it's added to a broader type union with other handlers (because as far
|
|
210
|
-
// as TypeScript knows, we might call the handler with output from a different selector).
|
|
211
|
-
//
|
|
212
|
-
// This poses no risk in this case, since we never call the handler past this point.
|
|
213
|
-
const widenedHandler = handler;
|
|
214
|
-
if (!subscribers || !subscribers.has(widenedHandler)) {
|
|
215
|
-
throw new Error(`Subscription not found for event: ${eventType}`);
|
|
216
|
-
}
|
|
217
|
-
const selector = subscribers.get(widenedHandler);
|
|
218
|
-
if (selector) {
|
|
219
|
-
__classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").delete(widenedHandler);
|
|
220
|
-
}
|
|
221
|
-
subscribers.delete(widenedHandler);
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Clear subscriptions for a specific event.
|
|
225
|
-
*
|
|
226
|
-
* This will remove all subscribed handlers for this event.
|
|
227
|
-
*
|
|
228
|
-
* @param eventType - The event type. This is a unique identifier for this event.
|
|
229
|
-
* @template EventType - A type union of Event type strings.
|
|
230
|
-
*/
|
|
231
|
-
clearEventSubscriptions(eventType) {
|
|
232
|
-
__classPrivateFieldGet(this, _Messenger_events, "f").delete(eventType);
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Clear all subscriptions.
|
|
236
|
-
*
|
|
237
|
-
* This will remove all subscribed handlers for all events.
|
|
238
|
-
*/
|
|
239
|
-
clearSubscriptions() {
|
|
240
|
-
__classPrivateFieldGet(this, _Messenger_events, "f").clear();
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Get a restricted messenger
|
|
244
|
-
*
|
|
245
|
-
* Returns a wrapper around the messenger instance that restricts access to actions and events.
|
|
246
|
-
* The provided allowlists grant the ability to call the listed actions and subscribe to the
|
|
247
|
-
* listed events. The "name" provided grants ownership of any actions and events under that
|
|
248
|
-
* namespace. Ownership allows registering actions and publishing events, as well as
|
|
249
|
-
* unregistering actions and clearing event subscriptions.
|
|
250
|
-
*
|
|
251
|
-
* @param options - Messenger options.
|
|
252
|
-
* @param options.name - The name of the thing this messenger will be handed to (e.g. the
|
|
253
|
-
* controller name). This grants "ownership" of actions and events under this namespace to the
|
|
254
|
-
* restricted messenger returned.
|
|
255
|
-
* @param options.allowedActions - The list of actions that this restricted messenger should be
|
|
256
|
-
* allowed to call.
|
|
257
|
-
* @param options.allowedEvents - The list of events that this restricted messenger should be
|
|
258
|
-
* allowed to subscribe to.
|
|
259
|
-
* @template Namespace - The namespace for this messenger. Typically this is the name of the
|
|
260
|
-
* module that this messenger has been created for. The authority to publish events and register
|
|
261
|
-
* actions under this namespace is granted to this restricted messenger instance.
|
|
262
|
-
* @template AllowedAction - A type union of the 'type' string for any allowed actions.
|
|
263
|
-
* This must not include internal actions that are in the messenger's namespace.
|
|
264
|
-
* @template AllowedEvent - A type union of the 'type' string for any allowed events.
|
|
265
|
-
* This must not include internal events that are in the messenger's namespace.
|
|
266
|
-
* @returns The restricted messenger.
|
|
267
|
-
*/
|
|
268
|
-
getRestricted({ name, allowedActions, allowedEvents, }) {
|
|
269
|
-
return new RestrictedMessenger_1.RestrictedMessenger({
|
|
270
|
-
messenger: this,
|
|
271
|
-
name,
|
|
272
|
-
allowedActions,
|
|
273
|
-
allowedEvents,
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
exports.Messenger = Messenger;
|
|
278
|
-
_Messenger_actions = new WeakMap(), _Messenger_events = new WeakMap(), _Messenger_initialEventPayloadGetters = new WeakMap(), _Messenger_eventPayloadCache = new WeakMap();
|
|
279
|
-
//# sourceMappingURL=Messenger.cjs.map
|