@metamask-previews/base-controller 6.0.2-preview-5dfbc0bb → 6.0.2-preview-b5937ee4
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/dist/BaseControllerV1.js +4 -2
- package/dist/BaseControllerV1.mjs +5 -3
- package/dist/BaseControllerV2.js +4 -2
- package/dist/BaseControllerV2.mjs +5 -3
- package/dist/ControllerMessenger.js +3 -3
- package/dist/ControllerMessenger.mjs +2 -2
- package/dist/RestrictedControllerMessenger.js +2 -2
- package/dist/RestrictedControllerMessenger.mjs +1 -1
- package/dist/{chunk-IURK6TKD.js → chunk-C7UY7MQN.js} +6 -2
- package/dist/chunk-C7UY7MQN.js.map +1 -0
- package/dist/{chunk-EJ5YPJOH.mjs → chunk-GHGTKX3N.mjs} +1 -1
- package/dist/chunk-GHGTKX3N.mjs.map +1 -0
- package/dist/{chunk-AOA2V5WQ.mjs → chunk-K2UEDATB.mjs} +2 -2
- package/dist/{chunk-G42723LG.js → chunk-PQONVO7O.js} +3 -3
- package/dist/{chunk-6MDMAJCP.mjs → chunk-QL4BF6DD.mjs} +5 -1
- package/dist/chunk-QL4BF6DD.mjs.map +1 -0
- package/dist/{chunk-UJFCPTF3.js → chunk-WJADFPC6.js} +1 -1
- package/dist/chunk-WJADFPC6.js.map +1 -0
- package/dist/{chunk-FTHCJ6HY.js → chunk-Z5HOIP7I.js} +6 -2
- package/dist/chunk-Z5HOIP7I.js.map +1 -0
- package/dist/{chunk-FECCSQA2.mjs → chunk-ZQJW5EWL.mjs} +5 -1
- package/dist/chunk-ZQJW5EWL.mjs.map +1 -0
- package/dist/index.js +9 -5
- package/dist/index.mjs +11 -7
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/BaseControllerV1.d.ts +24 -0
- package/dist/types/BaseControllerV1.d.ts.map +1 -1
- package/dist/types/BaseControllerV2.d.ts +43 -2
- package/dist/types/BaseControllerV2.d.ts.map +1 -1
- package/dist/types/RestrictedControllerMessenger.d.ts +8 -0
- package/dist/types/RestrictedControllerMessenger.d.ts.map +1 -1
- package/dist/types/index.d.ts +8 -6
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +2 -1
- package/dist/chunk-6MDMAJCP.mjs.map +0 -1
- package/dist/chunk-EJ5YPJOH.mjs.map +0 -1
- package/dist/chunk-FECCSQA2.mjs.map +0 -1
- package/dist/chunk-FTHCJ6HY.js.map +0 -1
- package/dist/chunk-IURK6TKD.js.map +0 -1
- package/dist/chunk-UJFCPTF3.js.map +0 -1
- /package/dist/{chunk-AOA2V5WQ.mjs.map → chunk-K2UEDATB.mjs.map} +0 -0
- /package/dist/{chunk-G42723LG.js.map → chunk-PQONVO7O.js.map} +0 -0
package/dist/BaseControllerV1.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
var _chunkZ5HOIP7Ijs = require('./chunk-Z5HOIP7I.js');
|
|
5
6
|
require('./chunk-Z4BLTVTB.js');
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
|
|
11
|
+
exports.BaseControllerV1 = _chunkZ5HOIP7Ijs.BaseControllerV1; exports.default = _chunkZ5HOIP7Ijs.BaseControllerV1_default; exports.isBaseControllerV1 = _chunkZ5HOIP7Ijs.isBaseControllerV1;
|
|
10
12
|
//# sourceMappingURL=BaseControllerV1.js.map
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BaseControllerV1,
|
|
3
|
-
BaseControllerV1_default
|
|
4
|
-
|
|
3
|
+
BaseControllerV1_default,
|
|
4
|
+
isBaseControllerV1
|
|
5
|
+
} from "./chunk-ZQJW5EWL.mjs";
|
|
5
6
|
import "./chunk-XUI43LEZ.mjs";
|
|
6
7
|
export {
|
|
7
8
|
BaseControllerV1,
|
|
8
|
-
BaseControllerV1_default as default
|
|
9
|
+
BaseControllerV1_default as default,
|
|
10
|
+
isBaseControllerV1
|
|
9
11
|
};
|
|
10
12
|
//# sourceMappingURL=BaseControllerV1.mjs.map
|
package/dist/BaseControllerV2.js
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
var _chunkC7UY7MQNjs = require('./chunk-C7UY7MQN.js');
|
|
6
7
|
require('./chunk-Z4BLTVTB.js');
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
|
|
13
|
+
exports.BaseController = _chunkC7UY7MQNjs.BaseController; exports.getAnonymizedState = _chunkC7UY7MQNjs.getAnonymizedState; exports.getPersistentState = _chunkC7UY7MQNjs.getPersistentState; exports.isBaseController = _chunkC7UY7MQNjs.isBaseController;
|
|
12
14
|
//# sourceMappingURL=BaseControllerV2.js.map
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BaseController,
|
|
3
3
|
getAnonymizedState,
|
|
4
|
-
getPersistentState
|
|
5
|
-
|
|
4
|
+
getPersistentState,
|
|
5
|
+
isBaseController
|
|
6
|
+
} from "./chunk-QL4BF6DD.mjs";
|
|
6
7
|
import "./chunk-XUI43LEZ.mjs";
|
|
7
8
|
export {
|
|
8
9
|
BaseController,
|
|
9
10
|
getAnonymizedState,
|
|
10
|
-
getPersistentState
|
|
11
|
+
getPersistentState,
|
|
12
|
+
isBaseController
|
|
11
13
|
};
|
|
12
14
|
//# sourceMappingURL=BaseControllerV2.mjs.map
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
require('./chunk-
|
|
3
|
+
var _chunkPQONVO7Ojs = require('./chunk-PQONVO7O.js');
|
|
4
|
+
require('./chunk-WJADFPC6.js');
|
|
5
5
|
require('./chunk-Z4BLTVTB.js');
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
exports.ControllerMessenger =
|
|
8
|
+
exports.ControllerMessenger = _chunkPQONVO7Ojs.ControllerMessenger;
|
|
9
9
|
//# sourceMappingURL=ControllerMessenger.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkWJADFPC6js = require('./chunk-WJADFPC6.js');
|
|
4
4
|
require('./chunk-Z4BLTVTB.js');
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
exports.RestrictedControllerMessenger =
|
|
7
|
+
exports.RestrictedControllerMessenger = _chunkWJADFPC6js.RestrictedControllerMessenger;
|
|
8
8
|
//# sourceMappingURL=RestrictedControllerMessenger.js.map
|
|
@@ -7,6 +7,9 @@ var _chunkZ4BLTVTBjs = require('./chunk-Z4BLTVTB.js');
|
|
|
7
7
|
// src/BaseControllerV2.ts
|
|
8
8
|
var _immer = require('immer');
|
|
9
9
|
_immer.enablePatches.call(void 0, );
|
|
10
|
+
function isBaseController(controller) {
|
|
11
|
+
return "name" in controller && typeof controller.name === "string" && "state" in controller && typeof controller.state === "object" && "metadata" in controller && typeof controller.metadata === "object";
|
|
12
|
+
}
|
|
10
13
|
var _internalState;
|
|
11
14
|
var BaseController = class {
|
|
12
15
|
/**
|
|
@@ -137,5 +140,6 @@ function deriveStateFromMetadata(state, metadata, metadataProperty) {
|
|
|
137
140
|
|
|
138
141
|
|
|
139
142
|
|
|
140
|
-
|
|
141
|
-
|
|
143
|
+
|
|
144
|
+
exports.isBaseController = isBaseController; exports.BaseController = BaseController; exports.getAnonymizedState = getAnonymizedState; exports.getPersistentState = getPersistentState;
|
|
145
|
+
//# sourceMappingURL=chunk-C7UY7MQN.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/BaseControllerV2.ts"],"names":[],"mappings":";;;;;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aAAa,SAAkB;AACvC,UAAM,YAAY,aAAa,mBAAK,iBAAgB,OAAO;AAC3D,uBAAK,gBAAiB;AACtB,SAAK,gBAAgB;AAAA,MACnB,GAAG,KAAK,IAAI;AAAA,MACZ;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWU,UAAU;AAClB,SAAK,gBAAgB,wBAAwB,GAAG,KAAK,IAAI,cAAc;AAAA,EACzE;AACF;AAxIE;AAqJK,SAAS,mBACd,OACA,UACqC;AACrC,SAAO,wBAAwB,OAAO,UAAU,WAAW;AAC7D;AASO,SAAS,mBACd,OACA,UACqC;AACrC,SAAO,wBAAwB,OAAO,UAAU,SAAS;AAC3D;AAUA,SAAS,wBACP,OACA,UACA,kBACqC;AACrC,SAAQ,OAAO,KAAK,KAAK,EAAgC,OAEvD,CAAC,cAAc,QAAQ;AACvB,QAAI;AACF,YAAM,gBAAgB,SAAS,GAAG;AAClC,UAAI,CAAC,eAAe;AAClB,cAAM,IAAI,MAAM,0BAA0B,OAAO,GAAG,CAAC,GAAG;AAAA,MAC1D;AACA,YAAM,mBAAmB,cAAc,gBAAgB;AACvD,YAAM,gBAAgB,MAAM,GAAG;AAC/B,UAAI,OAAO,qBAAqB,YAAY;AAC1C,qBAAa,GAAG,IAAI,iBAAiB,aAAa;AAAA,MACpD,WAAW,kBAAkB;AAC3B,qBAAa,GAAG,IAAI;AAAA,MACtB;AACA,aAAO;AAAA,IACT,SAAS,OAAO;AAGd,iBAAW,MAAM;AACf,cAAM;AAAA,MACR,CAAC;AACD,aAAO;AAAA,IACT;AAAA,EACF,GAAG,CAAC,CAAU;AAChB","sourcesContent":["import type { Json, PublicInterface } from '@metamask/utils';\nimport { enablePatches, produceWithPatches, applyPatches, freeze } from 'immer';\nimport type { Draft, Patch } from 'immer';\n\nimport type {\n BaseControllerV1Instance,\n StateConstraint as StateConstraintV1,\n} from './BaseControllerV1';\nimport type { ActionConstraint, EventConstraint } from './ControllerMessenger';\nimport type {\n RestrictedControllerMessenger,\n RestrictedControllerMessengerConstraint,\n} from './RestrictedControllerMessenger';\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: ControllerInstance,\n): controller is BaseControllerInstance {\n return (\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 universal supertype for the controller state object, encompassing both `BaseControllerV1` and `BaseControllerV2` state.\n */\n// TODO: Remove once BaseControllerV2 migrations are completed for all controllers.\nexport type LegacyControllerStateConstraint =\n | StateConstraintV1\n | StateConstraint;\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<\n string,\n StateConstraint,\n RestrictedControllerMessengerConstraint\n >\n >,\n 'metadata'\n> & {\n metadata: StateMetadataConstraint;\n};\n\n/**\n * A widest subtype of all controller instances that inherit from `BaseController` (formerly `BaseControllerV2`) or `BaseControllerV1`.\n * Any `BaseController` or `BaseControllerV1` subclass instance can be assigned to this type.\n */\n// TODO: Remove once BaseControllerV2 migrations are completed for all controllers.\nexport type ControllerInstance =\n | BaseControllerV1Instance\n | BaseControllerInstance;\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 RestrictedControllerMessenger<\n ControllerName,\n ActionConstraint | ControllerActions<ControllerName, ControllerState>,\n EventConstraint | ControllerEvents<ControllerName, ControllerState>,\n string,\n string\n >,\n> {\n #internalState: ControllerState;\n\n protected messagingSystem: messenger;\n\n /**\n * The name of the controller.\n *\n * This is used by the ComposableController to construct a composed application state.\n */\n public readonly name: ControllerName;\n\n public readonly metadata: StateMetadata<ControllerState>;\n\n /**\n * 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 this.#internalState = nextState;\n this.messagingSystem.publish(\n `${this.name}:stateChange`,\n nextState,\n patches,\n );\n\n return { nextState, patches, inversePatches };\n }\n\n /**\n * Applies immer patches to the current state. The patches come from the\n * update function itself and can either be normal or inverse patches.\n *\n * @param patches - An array of immer patches that are to be applied to make\n * or undo changes.\n */\n protected applyPatches(patches: Patch[]) {\n const nextState = applyPatches(this.#internalState, patches);\n this.#internalState = nextState;\n this.messagingSystem.publish(\n `${this.name}:stateChange`,\n nextState,\n patches,\n );\n }\n\n /**\n * Prepares the controller for garbage collection. This should be extended\n * by any subclasses to clean up any additional connections or events.\n *\n * The only cleanup performed here is to remove listeners. While technically\n * this is not required to ensure this instance is garbage collected, it at\n * least ensures this instance won't be responsible for preventing the\n * listeners from being garbage collected.\n */\n protected destroy() {\n this.messagingSystem.clearEventSubscriptions(`${this.name}:stateChange`);\n }\n}\n\n/**\n * Returns an anonymized representation of the controller state.\n *\n * By \"anonymized\" we mean that it should not contain any information that could be personally\n * identifiable.\n *\n * @param state - The controller state.\n * @param metadata - The controller state metadata, which describes how to derive the\n * anonymized state.\n * @returns The anonymized controller state.\n */\nexport function getAnonymizedState<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 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/RestrictedControllerMessenger.ts"],"sourcesContent":["import type {\n ActionConstraint,\n ActionHandler,\n ControllerMessenger,\n EventConstraint,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n NamespacedName,\n NotNamespacedBy,\n SelectorEventHandler,\n SelectorFunction,\n} from './ControllerMessenger';\n\n/**\n * A universal supertype of all `RestrictedControllerMessenger` instances.\n * This type can be assigned to any `RestrictedControllerMessenger` type.\n *\n * @template ControllerName - Name of the controller. Optionally can be used to\n * narrow this type to a constraint for the messenger of a specific controller.\n */\nexport type RestrictedControllerMessengerConstraint<\n ControllerName extends string = string,\n> = RestrictedControllerMessenger<\n ControllerName,\n ActionConstraint,\n EventConstraint,\n string,\n string\n>;\n\n/**\n * A restricted controller messenger.\n *\n * This acts as a wrapper around the controller messenger instance that restricts access to actions\n * and events.\n *\n * @template Namespace - The namespace for this messenger. Typically this is the name of the controller or\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * This must not include internal actions that are in the messenger's namespace.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * This must not include internal events that are in the messenger's namespace.\n */\nexport class RestrictedControllerMessenger<\n Namespace extends string,\n Action extends ActionConstraint,\n Event extends EventConstraint,\n AllowedAction extends string,\n AllowedEvent extends string,\n> {\n readonly #controllerMessenger: ControllerMessenger<\n ActionConstraint,\n EventConstraint\n >;\n\n readonly #controllerName: Namespace;\n\n readonly #allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n\n readonly #allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n\n /**\n * Constructs a restricted controller messenger\n *\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - The controller options.\n * @param options.controllerMessenger - The controller messenger instance that is being wrapped.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted controller messenger returned.\n * @param options.allowedActions - The list of actions that this restricted controller messenger\n * should be alowed to call.\n * @param options.allowedEvents - The list of events that this restricted controller messenger\n * should be allowed to subscribe to.\n */\n constructor({\n controllerMessenger,\n name,\n allowedActions,\n allowedEvents,\n }: {\n controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;\n name: Namespace;\n allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n }) {\n this.#controllerMessenger = controllerMessenger;\n this.#controllerName = name;\n this.#allowedActions = allowedActions;\n this.#allowedEvents = allowedEvents;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action type this handler is registered under *must* be in the current namespace.\n *\n * @param action - The action type. This is a unqiue identifier for this action.\n * @param handler - The action handler. This function gets called when the `call` method is\n * invoked with the given action type.\n * @throws Will throw if an action handler that is not in the current namespace is being registered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n registerActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType, handler: ActionHandler<Action, ActionType>) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${\n this.#controllerName\n }:'`,\n );\n }\n this.#controllerMessenger.registerActionHandler(action, handler);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * The action type being unregistered *must* be in the current namespace.\n *\n * @param action - The action type. This is a unique identifier for this action.\n * @throws Will throw if an action handler that is not in the current namespace is being unregistered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n unregisterActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${\n this.#controllerName\n }:'`,\n );\n }\n this.#controllerMessenger.unregisterActionHandler(action);\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * The action type being called must be on the action allowlist.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @param params - The action parameters. These must match the type of the parameters of the\n * registered action handler.\n * @throws Will throw when no handler has been registered for the given type.\n * @template ActionType - A type union of allowed Action type strings.\n * @returns The action return value.\n */\n call<\n ActionType extends\n | AllowedAction\n | (Action['type'] & NamespacedName<Namespace>),\n >(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n if (!this.#isAllowedAction(actionType)) {\n throw new Error(`Action missing from allow list: ${actionType}`);\n }\n const response = this.#controllerMessenger.call<ActionType>(\n actionType,\n ...params,\n );\n\n return response;\n }\n\n /**\n * Register a function for getting the initial payload for an event.\n *\n * This is used for events that represent a state change, where the payload is the state.\n * Registering a function for getting the payload allows event selectors to have a point of\n * comparison the first time state changes.\n *\n * The event type *must* be in the current namespace\n *\n * @param args - The arguments to this function\n * @param args.eventType - The event type to register a payload for.\n * @param args.getPayload - A function for retrieving the event payload.\n */\n registerInitialEventPayload<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(eventType)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.registerInitialEventPayload({\n eventType,\n getPayload,\n });\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * The event type being published *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @throws Will throw if an event that is not in the current namespace is being published.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n publish<EventType extends Event['type'] & NamespacedName<Namespace>>(\n event: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.publish(event, ...payload);\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this controller messenger.\n * @template EventType - A type union of Event type strings.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(eventType: EventType, handler: ExtractEventHandler<Event, EventType>): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this controller messenger.\n * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n event: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n\n if (selector) {\n return this.#controllerMessenger.subscribe(event, handler, selector);\n }\n return this.#controllerMessenger.subscribe(event, handler);\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * The event type being unsubscribed to must be on the event allowlist.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw if the given event is not an allowed event for this controller messenger.\n * @template EventType - A type union of allowed Event type strings.\n */\n unsubscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(event: EventType, handler: ExtractEventHandler<Event, EventType>) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n this.#controllerMessenger.unsubscribe(event, handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * The event type being cleared *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @throws Will throw if a subscription for an event that is not in the current namespace is being cleared.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n clearEventSubscriptions<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >(event: EventType) {\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed clearing events prefixed by '${this.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.clearEventSubscriptions(event);\n }\n\n /**\n * Determine whether the given event type is allowed. Event types are\n * allowed if they are in the current namespace or on the list of\n * allowed events.\n *\n * @param eventType - The event type to check.\n * @returns Whether the event type is allowed.\n */\n #isAllowedEvent(\n eventType: Event['type'],\n ): eventType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedEvent> {\n // Safely upcast to allow runtime check\n const allowedEvents: string[] | null = this.#allowedEvents;\n return (\n this.#isInCurrentNamespace(eventType) ||\n (allowedEvents !== null && allowedEvents.includes(eventType))\n );\n }\n\n /**\n * Determine whether the given action type is allowed. Action types\n * are allowed if they are in the current namespace or on the list of\n * allowed actions.\n *\n * @param actionType - The action type to check.\n * @returns Whether the action type is allowed.\n */\n #isAllowedAction(\n actionType: Action['type'],\n ): actionType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedAction> {\n // Safely upcast to allow runtime check\n const allowedActions: string[] | null = this.#allowedActions;\n return (\n this.#isInCurrentNamespace(actionType) ||\n (allowedActions !== null && allowedActions.includes(actionType))\n );\n }\n\n /**\n * Determine whether the given name is within the current namespace.\n *\n * @param name - The name to check\n * @returns Whether the name is within the current namespace\n */\n #isInCurrentNamespace(name: string): name is NamespacedName<Namespace> {\n return name.startsWith(`${this.#controllerName}:`);\n }\n}\n"],"mappings":";;;;;;;;AAAA;AAgDO,IAAM,gCAAN,MAML;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA8BA,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAKG;AAoRH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAqBA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAnWA,uBAAS,sBAAT;AAKA,uBAAS,iBAAT;AAEA,uBAAS,iBAAT;AAEA,uBAAS,gBAAT;AA+BE,uBAAK,sBAAuB;AAC5B,uBAAK,iBAAkB;AACvB,uBAAK,iBAAkB;AACvB,uBAAK,gBAAiB;AAAA,EACxB;AAAA;A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,SACd,YAG4C;AAE5C,QAAM,iBAAkC,mBAAK;AAC7C,SACE,sBAAK,gDAAL,WAA2B,eAC1B,mBAAmB,QAAQ,eAAe,SAAS,UAAU;AAElE;AAQA;AAAA,0BAAqB,SAAC,MAAiD;AACrE,SAAO,KAAK,WAAW,GAAG,mBAAK,gBAAe,GAAG;AACnD;","names":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
RestrictedControllerMessenger
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-GHGTKX3N.mjs";
|
|
4
4
|
import {
|
|
5
5
|
__privateAdd,
|
|
6
6
|
__privateGet
|
|
@@ -236,4 +236,4 @@ _eventPayloadCache = new WeakMap();
|
|
|
236
236
|
export {
|
|
237
237
|
ControllerMessenger
|
|
238
238
|
};
|
|
239
|
-
//# sourceMappingURL=chunk-
|
|
239
|
+
//# sourceMappingURL=chunk-K2UEDATB.mjs.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkWJADFPC6js = require('./chunk-WJADFPC6.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
@@ -220,7 +220,7 @@ var ControllerMessenger = class {
|
|
|
220
220
|
allowedActions,
|
|
221
221
|
allowedEvents
|
|
222
222
|
}) {
|
|
223
|
-
return new (0,
|
|
223
|
+
return new (0, _chunkWJADFPC6js.RestrictedControllerMessenger)({
|
|
224
224
|
controllerMessenger: this,
|
|
225
225
|
name,
|
|
226
226
|
allowedActions,
|
|
@@ -236,4 +236,4 @@ _eventPayloadCache = new WeakMap();
|
|
|
236
236
|
|
|
237
237
|
|
|
238
238
|
exports.ControllerMessenger = ControllerMessenger;
|
|
239
|
-
//# sourceMappingURL=chunk-
|
|
239
|
+
//# sourceMappingURL=chunk-PQONVO7O.js.map
|
|
@@ -7,6 +7,9 @@ import {
|
|
|
7
7
|
// src/BaseControllerV2.ts
|
|
8
8
|
import { enablePatches, produceWithPatches, applyPatches, freeze } from "immer";
|
|
9
9
|
enablePatches();
|
|
10
|
+
function isBaseController(controller) {
|
|
11
|
+
return "name" in controller && typeof controller.name === "string" && "state" in controller && typeof controller.state === "object" && "metadata" in controller && typeof controller.metadata === "object";
|
|
12
|
+
}
|
|
10
13
|
var _internalState;
|
|
11
14
|
var BaseController = class {
|
|
12
15
|
/**
|
|
@@ -134,8 +137,9 @@ function deriveStateFromMetadata(state, metadata, metadataProperty) {
|
|
|
134
137
|
}
|
|
135
138
|
|
|
136
139
|
export {
|
|
140
|
+
isBaseController,
|
|
137
141
|
BaseController,
|
|
138
142
|
getAnonymizedState,
|
|
139
143
|
getPersistentState
|
|
140
144
|
};
|
|
141
|
-
//# sourceMappingURL=chunk-
|
|
145
|
+
//# sourceMappingURL=chunk-QL4BF6DD.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/BaseControllerV2.ts"],"sourcesContent":["import type { Json, PublicInterface } from '@metamask/utils';\nimport { enablePatches, produceWithPatches, applyPatches, freeze } from 'immer';\nimport type { Draft, Patch } from 'immer';\n\nimport type {\n BaseControllerV1Instance,\n StateConstraint as StateConstraintV1,\n} from './BaseControllerV1';\nimport type { ActionConstraint, EventConstraint } from './ControllerMessenger';\nimport type {\n RestrictedControllerMessenger,\n RestrictedControllerMessengerConstraint,\n} from './RestrictedControllerMessenger';\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: ControllerInstance,\n): controller is BaseControllerInstance {\n return (\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 universal supertype for the controller state object, encompassing both `BaseControllerV1` and `BaseControllerV2` state.\n */\n// TODO: Remove once BaseControllerV2 migrations are completed for all controllers.\nexport type LegacyControllerStateConstraint =\n | StateConstraintV1\n | StateConstraint;\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<\n string,\n StateConstraint,\n RestrictedControllerMessengerConstraint\n >\n >,\n 'metadata'\n> & {\n metadata: StateMetadataConstraint;\n};\n\n/**\n * A widest subtype of all controller instances that inherit from `BaseController` (formerly `BaseControllerV2`) or `BaseControllerV1`.\n * Any `BaseController` or `BaseControllerV1` subclass instance can be assigned to this type.\n */\n// TODO: Remove once BaseControllerV2 migrations are completed for all controllers.\nexport type ControllerInstance =\n | BaseControllerV1Instance\n | BaseControllerInstance;\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 RestrictedControllerMessenger<\n ControllerName,\n ActionConstraint | ControllerActions<ControllerName, ControllerState>,\n EventConstraint | ControllerEvents<ControllerName, ControllerState>,\n string,\n string\n >,\n> {\n #internalState: ControllerState;\n\n protected messagingSystem: messenger;\n\n /**\n * The name of the controller.\n *\n * This is used by the ComposableController to construct a composed application state.\n */\n public readonly name: ControllerName;\n\n public readonly metadata: StateMetadata<ControllerState>;\n\n /**\n * 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 this.#internalState = nextState;\n this.messagingSystem.publish(\n `${this.name}:stateChange`,\n nextState,\n patches,\n );\n\n return { nextState, patches, inversePatches };\n }\n\n /**\n * Applies immer patches to the current state. The patches come from the\n * update function itself and can either be normal or inverse patches.\n *\n * @param patches - An array of immer patches that are to be applied to make\n * or undo changes.\n */\n protected applyPatches(patches: Patch[]) {\n const nextState = applyPatches(this.#internalState, patches);\n this.#internalState = nextState;\n this.messagingSystem.publish(\n `${this.name}:stateChange`,\n nextState,\n patches,\n );\n }\n\n /**\n * Prepares the controller for garbage collection. This should be extended\n * by any subclasses to clean up any additional connections or events.\n *\n * The only cleanup performed here is to remove listeners. While technically\n * this is not required to ensure this instance is garbage collected, it at\n * least ensures this instance won't be responsible for preventing the\n * listeners from being garbage collected.\n */\n protected destroy() {\n this.messagingSystem.clearEventSubscriptions(`${this.name}:stateChange`);\n }\n}\n\n/**\n * Returns an anonymized representation of the controller state.\n *\n * By \"anonymized\" we mean that it should not contain any information that could be personally\n * identifiable.\n *\n * @param state - The controller state.\n * @param metadata - The controller state metadata, which describes how to derive the\n * anonymized state.\n * @returns The anonymized controller state.\n */\nexport function getAnonymizedState<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"],"mappings":";;;;;;;AACA,SAAS,eAAe,oBAAoB,cAAc,cAAc;AAaxE,cAAc;AAQP,SAAS,iBACd,YACsC;AACtC,SACE,UAAU,cACV,OAAO,WAAW,SAAS,YAC3B,WAAW,cACX,OAAO,WAAW,UAAU,YAC5B,cAAc,cACd,OAAO,WAAW,aAAa;AAEnC;AAjCA;AA0LO,IAAM,iBAAN,MAYL;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAwBA,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAKG;AAjCH;AAkCE,SAAK,kBAAkB;AACvB,SAAK,OAAO;AAMZ,uBAA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B,kBAAkB,QAAQ,cAAc,SAAS,SAAS;AAE/D;AAUA;AAAA,qBAAgB,SACd,YAG4C;AAE5C,QAAM,iBAAkC,mBAAK;AAC7C,SACE,sBAAK,gDAAL,WAA2B,eAC1B,mBAAmB,QAAQ,eAAe,SAAS,UAAU;AAElE;AAQA;AAAA,0BAAqB,SAAC,MAAiD;AACrE,SAAO,KAAK,WAAW,GAAG,mBAAK,gBAAe,GAAG;AACnD","sourcesContent":["import type {\n ActionConstraint,\n ActionHandler,\n ControllerMessenger,\n EventConstraint,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n NamespacedName,\n NotNamespacedBy,\n SelectorEventHandler,\n SelectorFunction,\n} from './ControllerMessenger';\n\n/**\n * A universal supertype of all `RestrictedControllerMessenger` instances.\n * This type can be assigned to any `RestrictedControllerMessenger` type.\n *\n * @template ControllerName - Name of the controller. Optionally can be used to\n * narrow this type to a constraint for the messenger of a specific controller.\n */\nexport type RestrictedControllerMessengerConstraint<\n ControllerName extends string = string,\n> = RestrictedControllerMessenger<\n ControllerName,\n ActionConstraint,\n EventConstraint,\n string,\n string\n>;\n\n/**\n * A restricted controller messenger.\n *\n * This acts as a wrapper around the controller messenger instance that restricts access to actions\n * and events.\n *\n * @template Namespace - The namespace for this messenger. Typically this is the name of the controller or\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * This must not include internal actions that are in the messenger's namespace.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * This must not include internal events that are in the messenger's namespace.\n */\nexport class RestrictedControllerMessenger<\n Namespace extends string,\n Action extends ActionConstraint,\n Event extends EventConstraint,\n AllowedAction extends string,\n AllowedEvent extends string,\n> {\n readonly #controllerMessenger: ControllerMessenger<\n ActionConstraint,\n EventConstraint\n >;\n\n readonly #controllerName: Namespace;\n\n readonly #allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n\n readonly #allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n\n /**\n * Constructs a restricted controller messenger\n *\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - The controller options.\n * @param options.controllerMessenger - The controller messenger instance that is being wrapped.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted controller messenger returned.\n * @param options.allowedActions - The list of actions that this restricted controller messenger\n * should be alowed to call.\n * @param options.allowedEvents - The list of events that this restricted controller messenger\n * should be allowed to subscribe to.\n */\n constructor({\n controllerMessenger,\n name,\n allowedActions,\n allowedEvents,\n }: {\n controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;\n name: Namespace;\n allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n }) {\n this.#controllerMessenger = controllerMessenger;\n this.#controllerName = name;\n this.#allowedActions = allowedActions;\n this.#allowedEvents = allowedEvents;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action type this handler is registered under *must* be in the current namespace.\n *\n * @param action - The action type. This is a unqiue identifier for this action.\n * @param handler - The action handler. This function gets called when the `call` method is\n * invoked with the given action type.\n * @throws Will throw if an action handler that is not in the current namespace is being registered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n registerActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType, handler: ActionHandler<Action, ActionType>) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${\n this.#controllerName\n }:'`,\n );\n }\n this.#controllerMessenger.registerActionHandler(action, handler);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * The action type being unregistered *must* be in the current namespace.\n *\n * @param action - The action type. This is a unique identifier for this action.\n * @throws Will throw if an action handler that is not in the current namespace is being unregistered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n unregisterActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${\n this.#controllerName\n }:'`,\n );\n }\n this.#controllerMessenger.unregisterActionHandler(action);\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * The action type being called must be on the action allowlist.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @param params - The action parameters. These must match the type of the parameters of the\n * registered action handler.\n * @throws Will throw when no handler has been registered for the given type.\n * @template ActionType - A type union of allowed Action type strings.\n * @returns The action return value.\n */\n call<\n ActionType extends\n | AllowedAction\n | (Action['type'] & NamespacedName<Namespace>),\n >(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n if (!this.#isAllowedAction(actionType)) {\n throw new Error(`Action missing from allow list: ${actionType}`);\n }\n const response = this.#controllerMessenger.call<ActionType>(\n actionType,\n ...params,\n );\n\n return response;\n }\n\n /**\n * Register a function for getting the initial payload for an event.\n *\n * This is used for events that represent a state change, where the payload is the state.\n * Registering a function for getting the payload allows event selectors to have a point of\n * comparison the first time state changes.\n *\n * The event type *must* be in the current namespace\n *\n * @param args - The arguments to this function\n * @param args.eventType - The event type to register a payload for.\n * @param args.getPayload - A function for retrieving the event payload.\n */\n registerInitialEventPayload<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(eventType)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.registerInitialEventPayload({\n eventType,\n getPayload,\n });\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * The event type being published *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @throws Will throw if an event that is not in the current namespace is being published.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n publish<EventType extends Event['type'] & NamespacedName<Namespace>>(\n event: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.publish(event, ...payload);\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this controller messenger.\n * @template EventType - A type union of Event type strings.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(eventType: EventType, handler: ExtractEventHandler<Event, EventType>): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this controller messenger.\n * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n event: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n\n if (selector) {\n return this.#controllerMessenger.subscribe(event, handler, selector);\n }\n return this.#controllerMessenger.subscribe(event, handler);\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * The event type being unsubscribed to must be on the event allowlist.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw if the given event is not an allowed event for this controller messenger.\n * @template EventType - A type union of allowed Event type strings.\n */\n unsubscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(event: EventType, handler: ExtractEventHandler<Event, EventType>) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n this.#controllerMessenger.unsubscribe(event, handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * The event type being cleared *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @throws Will throw if a subscription for an event that is not in the current namespace is being cleared.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n clearEventSubscriptions<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >(event: EventType) {\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed clearing events prefixed by '${this.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.clearEventSubscriptions(event);\n }\n\n /**\n * Determine whether the given event type is allowed. Event types are\n * allowed if they are in the current namespace or on the list of\n * allowed events.\n *\n * @param eventType - The event type to check.\n * @returns Whether the event type is allowed.\n */\n #isAllowedEvent(\n eventType: Event['type'],\n ): eventType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedEvent> {\n // Safely upcast to allow runtime check\n const allowedEvents: string[] | null = this.#allowedEvents;\n return (\n this.#isInCurrentNamespace(eventType) ||\n (allowedEvents !== null && allowedEvents.includes(eventType))\n );\n }\n\n /**\n * Determine whether the given action type is allowed. Action types\n * are allowed if they are in the current namespace or on the list of\n * allowed actions.\n *\n * @param actionType - The action type to check.\n * @returns Whether the action type is allowed.\n */\n #isAllowedAction(\n actionType: Action['type'],\n ): actionType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedAction> {\n // Safely upcast to allow runtime check\n const allowedActions: string[] | null = this.#allowedActions;\n return (\n this.#isInCurrentNamespace(actionType) ||\n (allowedActions !== null && allowedActions.includes(actionType))\n );\n }\n\n /**\n * Determine whether the given name is within the current namespace.\n *\n * @param name - The name to check\n * @returns Whether the name is within the current namespace\n */\n #isInCurrentNamespace(name: string): name is NamespacedName<Namespace> {\n return name.startsWith(`${this.#controllerName}:`);\n }\n}\n"]}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/BaseControllerV1.ts
|
|
2
|
+
function isBaseControllerV1(controller) {
|
|
3
|
+
return "name" in controller && typeof controller.name === "string" && "config" in controller && typeof controller.config === "object" && "defaultConfig" in controller && typeof controller.defaultConfig === "object" && "state" in controller && typeof controller.state === "object" && "defaultState" in controller && typeof controller.defaultState === "object" && "disabled" in controller && typeof controller.disabled === "boolean" && "subscribe" in controller && typeof controller.subscribe === "function";
|
|
4
|
+
}
|
|
2
5
|
var BaseControllerV1 = class {
|
|
3
6
|
/**
|
|
4
7
|
* Creates a BaseControllerV1 instance. Both initial state and initial
|
|
@@ -132,5 +135,6 @@ var BaseControllerV1_default = BaseControllerV1;
|
|
|
132
135
|
|
|
133
136
|
|
|
134
137
|
|
|
135
|
-
|
|
136
|
-
|
|
138
|
+
|
|
139
|
+
exports.isBaseControllerV1 = isBaseControllerV1; exports.BaseControllerV1 = BaseControllerV1; exports.BaseControllerV1_default = BaseControllerV1_default;
|
|
140
|
+
//# sourceMappingURL=chunk-Z5HOIP7I.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/BaseControllerV1.ts"],"names":[],"mappings":";AAUO,SAAS,mBACd,YACwC;AACxC,SACE,UAAU,cACV,OAAO,WAAW,SAAS,YAC3B,YAAY,cACZ,OAAO,WAAW,WAAW,YAC7B,mBAAmB,cACnB,OAAO,WAAW,kBAAkB,YACpC,WAAW,cACX,OAAO,WAAW,UAAU,YAC5B,kBAAkB,cAClB,OAAO,WAAW,iBAAiB,YACnC,cAAc,cACd,OAAO,WAAW,aAAa,aAC/B,eAAe,cACf,OAAO,WAAW,cAAc;AAEpC;AAkEO,IAAM,mBAAN,MAAkE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsCvE,YAAY,SAAqB,CAAC,GAAG,QAAoB,CAAC,GAAG;AAlC7D;AAAA;AAAA;AAAA,yBAAmB,CAAC;AAKpB;AAAA;AAAA;AAAA,wBAAkB,CAAC;AAKnB;AAAA;AAAA;AAAA,oBAAW;AAKX;AAAA;AAAA;AAAA,gBAAO;AAMP,SAAQ,iBAAoB,KAAK;AAEjC,SAAQ,gBAAmB,KAAK;AAEhC,SAAiB,oBAAmC,CAAC;AAUnD,SAAK,eAAe;AACpB,SAAK,gBAAgB;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASU,aAAa;AACrB,SAAK,gBAAgB,KAAK;AAC1B,SAAK,iBAAi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,EAAE;AACtE,YAAQ,MAAM,KAAK,kBAAkB,OAAO,OAAO,CAAC;AACpD,WAAO,QAAQ;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,OAAmB,YAAY,OAAO;AAC3C,SAAK,gBAAgB,YACjB,OAAO,OAAO,CAAC,GAAG,KAAU,IAC5B,OAAO,OAAO,CAAC,GAAG,KAAK,eAAe,KAAK;AAC/C,SAAK,OAAO;AAAA,EACd;AACF;AAEA,IAAO,2BAAQ","sourcesContent":["import type { PublicInterface } from '@metamask/utils';\n\nimport type { ControllerInstance } from './BaseControllerV2';\n\n/**\n * Determines if the given controller is an instance of `BaseControllerV1`\n *\n * @param controller - Controller instance to check\n * @returns True if the controller is an instance of `BaseControllerV1`\n */\nexport function isBaseControllerV1(\n controller: ControllerInstance,\n): controller is BaseControllerV1Instance {\n return (\n 'name' in controller &&\n typeof controller.name === 'string' &&\n 'config' in controller &&\n typeof controller.config === 'object' &&\n 'defaultConfig' in controller &&\n typeof controller.defaultConfig === 'object' &&\n 'state' in controller &&\n typeof controller.state === 'object' &&\n 'defaultState' in controller &&\n typeof controller.defaultState === 'object' &&\n 'disabled' in controller &&\n typeof controller.disabled === 'boolean' &&\n 'subscribe' in controller &&\n typeof controller.subscribe === 'function'\n );\n}\n\n/**\n * State change callbacks\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) => void;\n\n/**\n * @type BaseConfig\n *\n * Base controller configuration\n * @property disabled - Determines if this controller is enabled\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface BaseConfig {\n disabled?: boolean;\n}\n\n/**\n * @type BaseState\n *\n * Base state representation\n * @property name - Unique name for this controller\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface BaseState {\n name?: string;\n}\n\n/**\n * The narrowest supertype for `BaseControllerV1` config objects.\n * This type can be assigned to any `BaseControllerV1` config object.\n */\nexport type ConfigConstraint = BaseConfig & object;\n\n/**\n * The narrowest supertype for `BaseControllerV1` state objects.\n * This type can be assigned to any `BaseControllerV1` state object.\n */\nexport type StateConstraint = BaseState & object;\n\n/**\n * The widest subtype of all controller instances that extend from `BaseControllerV1`.\n * Any `BaseControllerV1` instance can be assigned to this type.\n */\nexport type BaseControllerV1Instance = PublicInterface<\n BaseControllerV1<ConfigConstraint, StateConstraint>\n>;\n\n/**\n * @deprecated This class has been renamed to BaseControllerV1 and is no longer recommended for use for controllers. Please use BaseController (formerly BaseControllerV2) instead.\n *\n * Controller class that provides configuration, state management, and subscriptions.\n *\n * The core purpose of every controller is to maintain an internal data object\n * called \"state\". Each controller is responsible for its own state, and all global wallet state\n * is tracked in a controller as state.\n */\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 class BaseControllerV1<C extends BaseConfig, S extends BaseState> {\n /**\n * Default options used to configure this controller\n */\n defaultConfig: C = {} as never;\n\n /**\n * Default state set on this controller\n */\n defaultState: S = {} as never;\n\n /**\n * Determines if listeners are notified of state changes\n */\n disabled = false;\n\n /**\n * Name of this controller used during composition\n */\n name = 'BaseController';\n\n private readonly initialConfig: Partial<C>;\n\n private readonly initialState: Partial<S>;\n\n private internalConfig: C = this.defaultConfig;\n\n private internalState: S = this.defaultState;\n\n private readonly internalListeners: Listener<S>[] = [];\n\n /**\n * Creates a BaseControllerV1 instance. Both initial state and initial\n * configuration options are merged with defaults upon initialization.\n *\n * @param config - Initial options used to configure this controller.\n * @param state - Initial state to set on this controller.\n */\n constructor(config: Partial<C> = {}, state: Partial<S> = {}) {\n this.initialState = state;\n this.initialConfig = config;\n }\n\n /**\n * Enables the controller. This sets each config option as a member\n * variable on this instance and triggers any defined setters. This\n * also sets initial state and triggers any listeners.\n *\n * @returns This controller instance.\n */\n protected initialize() {\n this.internalState = this.defaultState;\n this.internalConfig = this.defaultConfig;\n this.configure(this.initialConfig);\n this.update(this.initialState);\n return this;\n }\n\n /**\n * Retrieves current controller configuration options.\n *\n * @returns The current configuration.\n */\n get config() {\n return this.internalConfig;\n }\n\n /**\n * Retrieves current controller state.\n *\n * @returns The current state.\n */\n get state() {\n return this.internalState;\n }\n\n /**\n * Updates controller configuration.\n *\n * @param config - New configuration options.\n * @param overwrite - Overwrite config instead of merging.\n * @param fullUpdate - Boolean that defines if the update is partial or not.\n */\n configure(config: Partial<C>, overwrite = false, fullUpdate = true) {\n if (fullUpdate) {\n this.internalConfig = overwrite\n ? (config as C)\n : Object.assign(this.internalConfig, config);\n\n for (const key of Object.keys(this.internalConfig) as (keyof C)[]) {\n const value = this.internalConfig[key];\n if (value !== undefined) {\n (this as unknown as C)[key] = value;\n }\n }\n } else {\n for (const key of Object.keys(config) as (keyof C)[]) {\n /* istanbul ignore else */\n if (this.internalConfig[key] !== undefined) {\n const value = (config as C)[key];\n this.internalConfig[key] = value;\n (this as unknown as C)[key] = value;\n }\n }\n }\n }\n\n /**\n * Notifies all subscribed listeners of current state.\n */\n notify() {\n if (this.disabled) {\n return;\n }\n\n this.internalListeners.forEach((listener) => {\n listener(this.internalState);\n });\n }\n\n /**\n * Adds new listener to be notified of state changes.\n *\n * @param listener - The callback triggered when state changes.\n */\n subscribe(listener: Listener<S>) {\n this.internalListeners.push(listener);\n }\n\n /**\n * Removes existing listener from receiving state changes.\n *\n * @param listener - The callback to remove.\n * @returns `true` if a listener is found and unsubscribed.\n */\n unsubscribe(listener: Listener<S>) {\n const index = this.internalListeners.findIndex((cb) => listener === cb);\n index > -1 && this.internalListeners.splice(index, 1);\n return index > -1;\n }\n\n /**\n * Updates controller state.\n *\n * @param state - The new state.\n * @param overwrite - Overwrite state instead of merging.\n */\n update(state: Partial<S>, overwrite = false) {\n this.internalState = overwrite\n ? Object.assign({}, state as S)\n : Object.assign({}, this.internalState, state);\n this.notify();\n }\n}\n\nexport default BaseControllerV1;\n"]}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// src/BaseControllerV1.ts
|
|
2
|
+
function isBaseControllerV1(controller) {
|
|
3
|
+
return "name" in controller && typeof controller.name === "string" && "config" in controller && typeof controller.config === "object" && "defaultConfig" in controller && typeof controller.defaultConfig === "object" && "state" in controller && typeof controller.state === "object" && "defaultState" in controller && typeof controller.defaultState === "object" && "disabled" in controller && typeof controller.disabled === "boolean" && "subscribe" in controller && typeof controller.subscribe === "function";
|
|
4
|
+
}
|
|
2
5
|
var BaseControllerV1 = class {
|
|
3
6
|
/**
|
|
4
7
|
* Creates a BaseControllerV1 instance. Both initial state and initial
|
|
@@ -130,7 +133,8 @@ var BaseControllerV1 = class {
|
|
|
130
133
|
var BaseControllerV1_default = BaseControllerV1;
|
|
131
134
|
|
|
132
135
|
export {
|
|
136
|
+
isBaseControllerV1,
|
|
133
137
|
BaseControllerV1,
|
|
134
138
|
BaseControllerV1_default
|
|
135
139
|
};
|
|
136
|
-
//# sourceMappingURL=chunk-
|
|
140
|
+
//# sourceMappingURL=chunk-ZQJW5EWL.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/BaseControllerV1.ts"],"sourcesContent":["import type { PublicInterface } from '@metamask/utils';\n\nimport type { ControllerInstance } from './BaseControllerV2';\n\n/**\n * Determines if the given controller is an instance of `BaseControllerV1`\n *\n * @param controller - Controller instance to check\n * @returns True if the controller is an instance of `BaseControllerV1`\n */\nexport function isBaseControllerV1(\n controller: ControllerInstance,\n): controller is BaseControllerV1Instance {\n return (\n 'name' in controller &&\n typeof controller.name === 'string' &&\n 'config' in controller &&\n typeof controller.config === 'object' &&\n 'defaultConfig' in controller &&\n typeof controller.defaultConfig === 'object' &&\n 'state' in controller &&\n typeof controller.state === 'object' &&\n 'defaultState' in controller &&\n typeof controller.defaultState === 'object' &&\n 'disabled' in controller &&\n typeof controller.disabled === 'boolean' &&\n 'subscribe' in controller &&\n typeof controller.subscribe === 'function'\n );\n}\n\n/**\n * State change callbacks\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) => void;\n\n/**\n * @type BaseConfig\n *\n * Base controller configuration\n * @property disabled - Determines if this controller is enabled\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface BaseConfig {\n disabled?: boolean;\n}\n\n/**\n * @type BaseState\n *\n * Base state representation\n * @property name - Unique name for this controller\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface BaseState {\n name?: string;\n}\n\n/**\n * The narrowest supertype for `BaseControllerV1` config objects.\n * This type can be assigned to any `BaseControllerV1` config object.\n */\nexport type ConfigConstraint = BaseConfig & object;\n\n/**\n * The narrowest supertype for `BaseControllerV1` state objects.\n * This type can be assigned to any `BaseControllerV1` state object.\n */\nexport type StateConstraint = BaseState & object;\n\n/**\n * The widest subtype of all controller instances that extend from `BaseControllerV1`.\n * Any `BaseControllerV1` instance can be assigned to this type.\n */\nexport type BaseControllerV1Instance = PublicInterface<\n BaseControllerV1<ConfigConstraint, StateConstraint>\n>;\n\n/**\n * @deprecated This class has been renamed to BaseControllerV1 and is no longer recommended for use for controllers. Please use BaseController (formerly BaseControllerV2) instead.\n *\n * Controller class that provides configuration, state management, and subscriptions.\n *\n * The core purpose of every controller is to maintain an internal data object\n * called \"state\". Each controller is responsible for its own state, and all global wallet state\n * is tracked in a controller as state.\n */\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 class BaseControllerV1<C extends BaseConfig, S extends BaseState> {\n /**\n * Default options used to configure this controller\n */\n defaultConfig: C = {} as never;\n\n /**\n * Default state set on this controller\n */\n defaultState: S = {} as never;\n\n /**\n * Determines if listeners are notified of state changes\n */\n disabled = false;\n\n /**\n * Name of this controller used during composition\n */\n name = 'BaseController';\n\n private readonly initialConfig: Partial<C>;\n\n private readonly initialState: Partial<S>;\n\n private internalConfig: C = this.defaultConfig;\n\n private internalState: S = this.defaultState;\n\n private readonly internalListeners: Listener<S>[] = [];\n\n /**\n * Creates a BaseControllerV1 instance. Both initial state and initial\n * configuration options are merged with defaults upon initialization.\n *\n * @param config - Initial options used to configure this controller.\n * @param state - Initial state to set on this controller.\n */\n constructor(config: Partial<C> = {}, state: Partial<S> = {}) {\n this.initialState = state;\n this.initialConfig = config;\n }\n\n /**\n * Enables the controller. This sets each config option as a member\n * variable on this instance and triggers any defined setters. This\n * also sets initial state and triggers any listeners.\n *\n * @returns This controller instance.\n */\n protected initialize() {\n this.internalState = this.defaultState;\n this.internalConfig = this.defaultConfig;\n this.configure(this.initialConfig);\n this.update(this.initialState);\n return this;\n }\n\n /**\n * Retrieves current controller configuration options.\n *\n * @returns The current configuration.\n */\n get config() {\n return this.internalConfig;\n }\n\n /**\n * Retrieves current controller state.\n *\n * @returns The current state.\n */\n get state() {\n return this.internalState;\n }\n\n /**\n * Updates controller configuration.\n *\n * @param config - New configuration options.\n * @param overwrite - Overwrite config instead of merging.\n * @param fullUpdate - Boolean that defines if the update is partial or not.\n */\n configure(config: Partial<C>, overwrite = false, fullUpdate = true) {\n if (fullUpdate) {\n this.internalConfig = overwrite\n ? (config as C)\n : Object.assign(this.internalConfig, config);\n\n for (const key of Object.keys(this.internalConfig) as (keyof C)[]) {\n const value = this.internalConfig[key];\n if (value !== undefined) {\n (this as unknown as C)[key] = value;\n }\n }\n } else {\n for (const key of Object.keys(config) as (keyof C)[]) {\n /* istanbul ignore else */\n if (this.internalConfig[key] !== undefined) {\n const value = (config as C)[key];\n this.internalConfig[key] = value;\n (this as unknown as C)[key] = value;\n }\n }\n }\n }\n\n /**\n * Notifies all subscribed listeners of current state.\n */\n notify() {\n if (this.disabled) {\n return;\n }\n\n this.internalListeners.forEach((listener) => {\n listener(this.internalState);\n });\n }\n\n /**\n * Adds new listener to be notified of state changes.\n *\n * @param listener - The callback triggered when state changes.\n */\n subscribe(listener: Listener<S>) {\n this.internalListeners.push(listener);\n }\n\n /**\n * Removes existing listener from receiving state changes.\n *\n * @param listener - The callback to remove.\n * @returns `true` if a listener is found and unsubscribed.\n */\n unsubscribe(listener: Listener<S>) {\n const index = this.internalListeners.findIndex((cb) => listener === cb);\n index > -1 && this.internalListeners.splice(index, 1);\n return index > -1;\n }\n\n /**\n * Updates controller state.\n *\n * @param state - The new state.\n * @param overwrite - Overwrite state instead of merging.\n */\n update(state: Partial<S>, overwrite = false) {\n this.internalState = overwrite\n ? Object.assign({}, state as S)\n : Object.assign({}, this.internalState, state);\n this.notify();\n }\n}\n\nexport default BaseControllerV1;\n"],"mappings":";AAUO,SAAS,mBACd,YACwC;AACxC,SACE,UAAU,cACV,OAAO,WAAW,SAAS,YAC3B,YAAY,cACZ,OAAO,WAAW,WAAW,YAC7B,mBAAmB,cACnB,OAAO,WAAW,kBAAkB,YACpC,WAAW,cACX,OAAO,WAAW,UAAU,YAC5B,kBAAkB,cAClB,OAAO,WAAW,iBAAiB,YACnC,cAAc,cACd,OAAO,WAAW,aAAa,aAC/B,eAAe,cACf,OAAO,WAAW,cAAc;AAEpC;AAkEO,IAAM,mBAAN,MAAkE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsCvE,YAAY,SAAqB,CAAC,GAAG,QAAoB,CAAC,GAAG;AAlC7D;AAAA;AAAA;AAAA,yBAAmB,CAAC;AAKpB;AAAA;AAAA;AAAA,wBAAkB,CAAC;AAKnB;AAAA;AAAA;AAAA,oBAAW;AAKX;AAAA;AAAA;AAAA,gBAAO;AAMP,SAAQ,iBAAoB,KAAK;AAEjC,SAAQ,gBAAmB,KAAK;AAEhC,SAAiB,oBAAmC,CAAC;AAUnD,SAAK,eAAe;AACpB,SAAK,gBAAgB;AA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A;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,YAAY,UAAuB;AACjC,UAAM,QAAQ,KAAK,kBAAkB,UAAU,CAAC,OAAO,aAAa,EAAE;AACtE,YAAQ,MAAM,KAAK,kBAAkB,OAAO,OAAO,CAAC;AACpD,WAAO,QAAQ;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,OAAmB,YAAY,OAAO;AAC3C,SAAK,gBAAgB,YACjB,OAAO,OAAO,CAAC,GAAG,KAAU,IAC5B,OAAO,OAAO,CAAC,GAAG,KAAK,eAAe,KAAK;AAC/C,SAAK,OAAO;AAAA,EACd;AACF;AAEA,IAAO,2BAAQ;","names":[]}
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var _chunkFTHCJ6HYjs = require('./chunk-FTHCJ6HY.js');
|
|
4
3
|
|
|
4
|
+
var _chunkZ5HOIP7Ijs = require('./chunk-Z5HOIP7I.js');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
var _chunkIURK6TKDjs = require('./chunk-IURK6TKD.js');
|
|
9
8
|
|
|
10
9
|
|
|
11
|
-
var
|
|
10
|
+
var _chunkC7UY7MQNjs = require('./chunk-C7UY7MQN.js');
|
|
12
11
|
|
|
13
12
|
|
|
14
|
-
var
|
|
13
|
+
var _chunkPQONVO7Ojs = require('./chunk-PQONVO7O.js');
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
var _chunkWJADFPC6js = require('./chunk-WJADFPC6.js');
|
|
15
17
|
require('./chunk-Z4BLTVTB.js');
|
|
16
18
|
|
|
17
19
|
|
|
@@ -20,5 +22,7 @@ require('./chunk-Z4BLTVTB.js');
|
|
|
20
22
|
|
|
21
23
|
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
exports.BaseController = _chunkC7UY7MQNjs.BaseController; exports.BaseControllerV1 = _chunkZ5HOIP7Ijs.BaseControllerV1; exports.ControllerMessenger = _chunkPQONVO7Ojs.ControllerMessenger; exports.RestrictedControllerMessenger = _chunkWJADFPC6js.RestrictedControllerMessenger; exports.getAnonymizedState = _chunkC7UY7MQNjs.getAnonymizedState; exports.getPersistentState = _chunkC7UY7MQNjs.getPersistentState; exports.isBaseController = _chunkC7UY7MQNjs.isBaseController; exports.isBaseControllerV1 = _chunkZ5HOIP7Ijs.isBaseControllerV1;
|
|
24
28
|
//# sourceMappingURL=index.js.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
|
-
BaseControllerV1
|
|
3
|
-
|
|
2
|
+
BaseControllerV1,
|
|
3
|
+
isBaseControllerV1
|
|
4
|
+
} from "./chunk-ZQJW5EWL.mjs";
|
|
4
5
|
import {
|
|
5
6
|
BaseController,
|
|
6
7
|
getAnonymizedState,
|
|
7
|
-
getPersistentState
|
|
8
|
-
|
|
8
|
+
getPersistentState,
|
|
9
|
+
isBaseController
|
|
10
|
+
} from "./chunk-QL4BF6DD.mjs";
|
|
9
11
|
import {
|
|
10
12
|
ControllerMessenger
|
|
11
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-K2UEDATB.mjs";
|
|
12
14
|
import {
|
|
13
15
|
RestrictedControllerMessenger
|
|
14
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-GHGTKX3N.mjs";
|
|
15
17
|
import "./chunk-XUI43LEZ.mjs";
|
|
16
18
|
export {
|
|
17
19
|
BaseController,
|
|
@@ -19,6 +21,8 @@ export {
|
|
|
19
21
|
ControllerMessenger,
|
|
20
22
|
RestrictedControllerMessenger,
|
|
21
23
|
getAnonymizedState,
|
|
22
|
-
getPersistentState
|
|
24
|
+
getPersistentState,
|
|
25
|
+
isBaseController,
|
|
26
|
+
isBaseControllerV1
|
|
23
27
|
};
|
|
24
28
|
//# sourceMappingURL=index.mjs.map
|