@metamask-previews/base-controller 7.1.1-preview-224dff25 → 7.1.1-preview-99939da3
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 +0 -14
- package/dist/BaseControllerV1.cjs +174 -0
- package/dist/BaseControllerV1.cjs.map +1 -0
- package/dist/BaseControllerV1.d.cts +140 -0
- package/dist/BaseControllerV1.d.cts.map +1 -0
- package/dist/BaseControllerV1.d.mts +140 -0
- package/dist/BaseControllerV1.d.mts.map +1 -0
- package/dist/BaseControllerV1.mjs +169 -0
- package/dist/BaseControllerV1.mjs.map +1 -0
- package/dist/BaseControllerV2.cjs +1 -3
- package/dist/BaseControllerV2.cjs.map +1 -1
- package/dist/BaseControllerV2.d.cts +11 -1
- package/dist/BaseControllerV2.d.cts.map +1 -1
- package/dist/BaseControllerV2.d.mts +11 -1
- package/dist/BaseControllerV2.d.mts.map +1 -1
- package/dist/BaseControllerV2.mjs +1 -3
- package/dist/BaseControllerV2.mjs.map +1 -1
- package/dist/Messenger.cjs +2 -1
- package/dist/Messenger.cjs.map +1 -1
- package/dist/Messenger.d.cts +1 -1
- package/dist/Messenger.d.cts.map +1 -1
- package/dist/Messenger.d.mts +1 -1
- package/dist/Messenger.d.mts.map +1 -1
- package/dist/Messenger.mjs +1 -0
- package/dist/Messenger.mjs.map +1 -1
- package/dist/RestrictedMessenger.cjs +10 -4
- package/dist/RestrictedMessenger.cjs.map +1 -1
- package/dist/RestrictedMessenger.d.cts +13 -1
- package/dist/RestrictedMessenger.d.cts.map +1 -1
- package/dist/RestrictedMessenger.d.mts +13 -1
- package/dist/RestrictedMessenger.d.mts.map +1 -1
- package/dist/RestrictedMessenger.mjs +9 -3
- package/dist/RestrictedMessenger.mjs.map +1 -1
- package/dist/index.cjs +6 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +6 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,20 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
-
### Changed
|
|
11
|
-
|
|
12
|
-
- **BREAKING:** Remove deprecated messenger-related exports and simplify `RestrictedMessenger` constructor ([#5260](https://github.com/MetaMask/core/pull/5260))
|
|
13
|
-
- Remove `ControllerMessenger` export which was an alias for `Messenger`. Consumers should import `Messenger` directly
|
|
14
|
-
- Remove `RestrictedControllerMessenger` export which was an alias for `RestrictedMessenger`. Consumers should import `RestrictedMessenger` directly
|
|
15
|
-
- Remove `RestrictedControllerMessengerConstraint` type export which was an alias for `RestrictedMessengerConstraint`. Consumers should use `RestrictedMessengerConstraint` type directly
|
|
16
|
-
- Simplify `RestrictedMessenger` constructor by removing deprecated `controllerMessenger` parameter. The messenger instance should now be passed using only the `messenger` parameter instead of supporting both options
|
|
17
|
-
- Widen input parameter for type guard `isBaseController` from `ControllerInstance` to `unknown` ([#5018](https://github.com/MetaMask/core/pull/5018/))
|
|
18
|
-
|
|
19
|
-
### Removed
|
|
20
|
-
|
|
21
|
-
- **BREAKING:** Remove class `BaseControllerV1` and type guard `isBaseControllerV1` ([#5018](https://github.com/MetaMask/core/pull/5018/))
|
|
22
|
-
- **BREAKING:** Remove types `BaseConfig`, `BaseControllerV1Instance`, `BaseState`, `ConfigConstraintV1`, `Listener`, `StateConstraintV1`, `LegacyControllerStateConstraint`, `ControllerInstance` ([#5018](https://github.com/MetaMask/core/pull/5018/))
|
|
23
|
-
|
|
24
10
|
## [7.1.1]
|
|
25
11
|
|
|
26
12
|
### Changed
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseControllerV1 = exports.isBaseControllerV1 = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Determines if the given controller is an instance of `BaseControllerV1`
|
|
6
|
+
*
|
|
7
|
+
* @param controller - Controller instance to check
|
|
8
|
+
* @returns True if the controller is an instance of `BaseControllerV1`
|
|
9
|
+
*/
|
|
10
|
+
function isBaseControllerV1(controller) {
|
|
11
|
+
return ('name' in controller &&
|
|
12
|
+
typeof controller.name === 'string' &&
|
|
13
|
+
'config' in controller &&
|
|
14
|
+
typeof controller.config === 'object' &&
|
|
15
|
+
'defaultConfig' in controller &&
|
|
16
|
+
typeof controller.defaultConfig === 'object' &&
|
|
17
|
+
'state' in controller &&
|
|
18
|
+
typeof controller.state === 'object' &&
|
|
19
|
+
'defaultState' in controller &&
|
|
20
|
+
typeof controller.defaultState === 'object' &&
|
|
21
|
+
'disabled' in controller &&
|
|
22
|
+
typeof controller.disabled === 'boolean' &&
|
|
23
|
+
'subscribe' in controller &&
|
|
24
|
+
typeof controller.subscribe === 'function');
|
|
25
|
+
}
|
|
26
|
+
exports.isBaseControllerV1 = isBaseControllerV1;
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated This class has been renamed to BaseControllerV1 and is no longer recommended for use for controllers. Please use BaseController (formerly BaseControllerV2) instead.
|
|
29
|
+
*
|
|
30
|
+
* Controller class that provides configuration, state management, and subscriptions.
|
|
31
|
+
*
|
|
32
|
+
* The core purpose of every controller is to maintain an internal data object
|
|
33
|
+
* called "state". Each controller is responsible for its own state, and all global wallet state
|
|
34
|
+
* is tracked in a controller as state.
|
|
35
|
+
*/
|
|
36
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
38
|
+
class BaseControllerV1 {
|
|
39
|
+
/**
|
|
40
|
+
* Creates a BaseControllerV1 instance. Both initial state and initial
|
|
41
|
+
* configuration options are merged with defaults upon initialization.
|
|
42
|
+
*
|
|
43
|
+
* @param config - Initial options used to configure this controller.
|
|
44
|
+
* @param state - Initial state to set on this controller.
|
|
45
|
+
*/
|
|
46
|
+
constructor(config = {}, state = {}) {
|
|
47
|
+
/**
|
|
48
|
+
* Default options used to configure this controller
|
|
49
|
+
*/
|
|
50
|
+
this.defaultConfig = {};
|
|
51
|
+
/**
|
|
52
|
+
* Default state set on this controller
|
|
53
|
+
*/
|
|
54
|
+
this.defaultState = {};
|
|
55
|
+
/**
|
|
56
|
+
* Determines if listeners are notified of state changes
|
|
57
|
+
*/
|
|
58
|
+
this.disabled = false;
|
|
59
|
+
/**
|
|
60
|
+
* Name of this controller used during composition
|
|
61
|
+
*/
|
|
62
|
+
this.name = 'BaseController';
|
|
63
|
+
this.internalConfig = this.defaultConfig;
|
|
64
|
+
this.internalState = this.defaultState;
|
|
65
|
+
this.internalListeners = [];
|
|
66
|
+
this.initialState = state;
|
|
67
|
+
this.initialConfig = config;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Enables the controller. This sets each config option as a member
|
|
71
|
+
* variable on this instance and triggers any defined setters. This
|
|
72
|
+
* also sets initial state and triggers any listeners.
|
|
73
|
+
*
|
|
74
|
+
* @returns This controller instance.
|
|
75
|
+
*/
|
|
76
|
+
initialize() {
|
|
77
|
+
this.internalState = this.defaultState;
|
|
78
|
+
this.internalConfig = this.defaultConfig;
|
|
79
|
+
this.configure(this.initialConfig);
|
|
80
|
+
this.update(this.initialState);
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Retrieves current controller configuration options.
|
|
85
|
+
*
|
|
86
|
+
* @returns The current configuration.
|
|
87
|
+
*/
|
|
88
|
+
get config() {
|
|
89
|
+
return this.internalConfig;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Retrieves current controller state.
|
|
93
|
+
*
|
|
94
|
+
* @returns The current state.
|
|
95
|
+
*/
|
|
96
|
+
get state() {
|
|
97
|
+
return this.internalState;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Updates controller configuration.
|
|
101
|
+
*
|
|
102
|
+
* @param config - New configuration options.
|
|
103
|
+
* @param overwrite - Overwrite config instead of merging.
|
|
104
|
+
* @param fullUpdate - Boolean that defines if the update is partial or not.
|
|
105
|
+
*/
|
|
106
|
+
configure(config, overwrite = false, fullUpdate = true) {
|
|
107
|
+
if (fullUpdate) {
|
|
108
|
+
this.internalConfig = overwrite
|
|
109
|
+
? config
|
|
110
|
+
: Object.assign(this.internalConfig, config);
|
|
111
|
+
for (const key of Object.keys(this.internalConfig)) {
|
|
112
|
+
const value = this.internalConfig[key];
|
|
113
|
+
if (value !== undefined) {
|
|
114
|
+
this[key] = value;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
for (const key of Object.keys(config)) {
|
|
120
|
+
/* istanbul ignore else */
|
|
121
|
+
if (this.internalConfig[key] !== undefined) {
|
|
122
|
+
const value = config[key];
|
|
123
|
+
this.internalConfig[key] = value;
|
|
124
|
+
this[key] = value;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Notifies all subscribed listeners of current state.
|
|
131
|
+
*/
|
|
132
|
+
notify() {
|
|
133
|
+
if (this.disabled) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
this.internalListeners.forEach((listener) => {
|
|
137
|
+
listener(this.internalState);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Adds new listener to be notified of state changes.
|
|
142
|
+
*
|
|
143
|
+
* @param listener - The callback triggered when state changes.
|
|
144
|
+
*/
|
|
145
|
+
subscribe(listener) {
|
|
146
|
+
this.internalListeners.push(listener);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Removes existing listener from receiving state changes.
|
|
150
|
+
*
|
|
151
|
+
* @param listener - The callback to remove.
|
|
152
|
+
* @returns `true` if a listener is found and unsubscribed.
|
|
153
|
+
*/
|
|
154
|
+
unsubscribe(listener) {
|
|
155
|
+
const index = this.internalListeners.findIndex((cb) => listener === cb);
|
|
156
|
+
index > -1 && this.internalListeners.splice(index, 1);
|
|
157
|
+
return index > -1;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Updates controller state.
|
|
161
|
+
*
|
|
162
|
+
* @param state - The new state.
|
|
163
|
+
* @param overwrite - Overwrite state instead of merging.
|
|
164
|
+
*/
|
|
165
|
+
update(state, overwrite = false) {
|
|
166
|
+
this.internalState = overwrite
|
|
167
|
+
? Object.assign({}, state)
|
|
168
|
+
: Object.assign({}, this.internalState, state);
|
|
169
|
+
this.notify();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
exports.BaseControllerV1 = BaseControllerV1;
|
|
173
|
+
exports.default = BaseControllerV1;
|
|
174
|
+
//# sourceMappingURL=BaseControllerV1.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseControllerV1.cjs","sourceRoot":"","sources":["../src/BaseControllerV1.ts"],"names":[],"mappings":";;;AAIA;;;;;GAKG;AACH,SAAgB,kBAAkB,CAChC,UAA8B;IAE9B,OAAO,CACL,MAAM,IAAI,UAAU;QACpB,OAAO,UAAU,CAAC,IAAI,KAAK,QAAQ;QACnC,QAAQ,IAAI,UAAU;QACtB,OAAO,UAAU,CAAC,MAAM,KAAK,QAAQ;QACrC,eAAe,IAAI,UAAU;QAC7B,OAAO,UAAU,CAAC,aAAa,KAAK,QAAQ;QAC5C,OAAO,IAAI,UAAU;QACrB,OAAO,UAAU,CAAC,KAAK,KAAK,QAAQ;QACpC,cAAc,IAAI,UAAU;QAC5B,OAAO,UAAU,CAAC,YAAY,KAAK,QAAQ;QAC3C,UAAU,IAAI,UAAU;QACxB,OAAO,UAAU,CAAC,QAAQ,KAAK,SAAS;QACxC,WAAW,IAAI,UAAU;QACzB,OAAO,UAAU,CAAC,SAAS,KAAK,UAAU,CAC3C,CAAC;AACJ,CAAC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AC,KAAK,EAAE,CAAC,CAAC,CAAC;QACtD,OAAO,KAAK,GAAG,CAAC,CAAC,CAAC;IACpB,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,KAAiB,EAAE,SAAS,GAAG,KAAK;QACzC,IAAI,CAAC,aAAa,GAAG,SAAS;YAC5B,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,KAAU,CAAC;YAC/B,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM,EAAE,CAAC;IAChB,CAAC;CACF;AAzJD,4CAyJC;AAED,kBAAe,gBAAgB,CAAC","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"]}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { PublicInterface } from "@metamask/utils";
|
|
2
|
+
import type { ControllerInstance } from "./BaseControllerV2.cjs";
|
|
3
|
+
/**
|
|
4
|
+
* Determines if the given controller is an instance of `BaseControllerV1`
|
|
5
|
+
*
|
|
6
|
+
* @param controller - Controller instance to check
|
|
7
|
+
* @returns True if the controller is an instance of `BaseControllerV1`
|
|
8
|
+
*/
|
|
9
|
+
export declare function isBaseControllerV1(controller: ControllerInstance): controller is BaseControllerV1Instance;
|
|
10
|
+
/**
|
|
11
|
+
* State change callbacks
|
|
12
|
+
*/
|
|
13
|
+
export type Listener<T> = (state: T) => void;
|
|
14
|
+
/**
|
|
15
|
+
* @type BaseConfig
|
|
16
|
+
*
|
|
17
|
+
* Base controller configuration
|
|
18
|
+
* @property disabled - Determines if this controller is enabled
|
|
19
|
+
*/
|
|
20
|
+
export interface BaseConfig {
|
|
21
|
+
disabled?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @type BaseState
|
|
25
|
+
*
|
|
26
|
+
* Base state representation
|
|
27
|
+
* @property name - Unique name for this controller
|
|
28
|
+
*/
|
|
29
|
+
export interface BaseState {
|
|
30
|
+
name?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The narrowest supertype for `BaseControllerV1` config objects.
|
|
34
|
+
* This type can be assigned to any `BaseControllerV1` config object.
|
|
35
|
+
*/
|
|
36
|
+
export type ConfigConstraint = BaseConfig & object;
|
|
37
|
+
/**
|
|
38
|
+
* The narrowest supertype for `BaseControllerV1` state objects.
|
|
39
|
+
* This type can be assigned to any `BaseControllerV1` state object.
|
|
40
|
+
*/
|
|
41
|
+
export type StateConstraint = BaseState & object;
|
|
42
|
+
/**
|
|
43
|
+
* The widest subtype of all controller instances that extend from `BaseControllerV1`.
|
|
44
|
+
* Any `BaseControllerV1` instance can be assigned to this type.
|
|
45
|
+
*/
|
|
46
|
+
export type BaseControllerV1Instance = PublicInterface<BaseControllerV1<ConfigConstraint, StateConstraint>>;
|
|
47
|
+
/**
|
|
48
|
+
* @deprecated This class has been renamed to BaseControllerV1 and is no longer recommended for use for controllers. Please use BaseController (formerly BaseControllerV2) instead.
|
|
49
|
+
*
|
|
50
|
+
* Controller class that provides configuration, state management, and subscriptions.
|
|
51
|
+
*
|
|
52
|
+
* The core purpose of every controller is to maintain an internal data object
|
|
53
|
+
* called "state". Each controller is responsible for its own state, and all global wallet state
|
|
54
|
+
* is tracked in a controller as state.
|
|
55
|
+
*/
|
|
56
|
+
export declare class BaseControllerV1<C extends BaseConfig, S extends BaseState> {
|
|
57
|
+
/**
|
|
58
|
+
* Default options used to configure this controller
|
|
59
|
+
*/
|
|
60
|
+
defaultConfig: C;
|
|
61
|
+
/**
|
|
62
|
+
* Default state set on this controller
|
|
63
|
+
*/
|
|
64
|
+
defaultState: S;
|
|
65
|
+
/**
|
|
66
|
+
* Determines if listeners are notified of state changes
|
|
67
|
+
*/
|
|
68
|
+
disabled: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Name of this controller used during composition
|
|
71
|
+
*/
|
|
72
|
+
name: string;
|
|
73
|
+
private readonly initialConfig;
|
|
74
|
+
private readonly initialState;
|
|
75
|
+
private internalConfig;
|
|
76
|
+
private internalState;
|
|
77
|
+
private readonly internalListeners;
|
|
78
|
+
/**
|
|
79
|
+
* Creates a BaseControllerV1 instance. Both initial state and initial
|
|
80
|
+
* configuration options are merged with defaults upon initialization.
|
|
81
|
+
*
|
|
82
|
+
* @param config - Initial options used to configure this controller.
|
|
83
|
+
* @param state - Initial state to set on this controller.
|
|
84
|
+
*/
|
|
85
|
+
constructor(config?: Partial<C>, state?: Partial<S>);
|
|
86
|
+
/**
|
|
87
|
+
* Enables the controller. This sets each config option as a member
|
|
88
|
+
* variable on this instance and triggers any defined setters. This
|
|
89
|
+
* also sets initial state and triggers any listeners.
|
|
90
|
+
*
|
|
91
|
+
* @returns This controller instance.
|
|
92
|
+
*/
|
|
93
|
+
protected initialize(): this;
|
|
94
|
+
/**
|
|
95
|
+
* Retrieves current controller configuration options.
|
|
96
|
+
*
|
|
97
|
+
* @returns The current configuration.
|
|
98
|
+
*/
|
|
99
|
+
get config(): C;
|
|
100
|
+
/**
|
|
101
|
+
* Retrieves current controller state.
|
|
102
|
+
*
|
|
103
|
+
* @returns The current state.
|
|
104
|
+
*/
|
|
105
|
+
get state(): S;
|
|
106
|
+
/**
|
|
107
|
+
* Updates controller configuration.
|
|
108
|
+
*
|
|
109
|
+
* @param config - New configuration options.
|
|
110
|
+
* @param overwrite - Overwrite config instead of merging.
|
|
111
|
+
* @param fullUpdate - Boolean that defines if the update is partial or not.
|
|
112
|
+
*/
|
|
113
|
+
configure(config: Partial<C>, overwrite?: boolean, fullUpdate?: boolean): void;
|
|
114
|
+
/**
|
|
115
|
+
* Notifies all subscribed listeners of current state.
|
|
116
|
+
*/
|
|
117
|
+
notify(): void;
|
|
118
|
+
/**
|
|
119
|
+
* Adds new listener to be notified of state changes.
|
|
120
|
+
*
|
|
121
|
+
* @param listener - The callback triggered when state changes.
|
|
122
|
+
*/
|
|
123
|
+
subscribe(listener: Listener<S>): void;
|
|
124
|
+
/**
|
|
125
|
+
* Removes existing listener from receiving state changes.
|
|
126
|
+
*
|
|
127
|
+
* @param listener - The callback to remove.
|
|
128
|
+
* @returns `true` if a listener is found and unsubscribed.
|
|
129
|
+
*/
|
|
130
|
+
unsubscribe(listener: Listener<S>): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Updates controller state.
|
|
133
|
+
*
|
|
134
|
+
* @param state - The new state.
|
|
135
|
+
* @param overwrite - Overwrite state instead of merging.
|
|
136
|
+
*/
|
|
137
|
+
update(state: Partial<S>, overwrite?: boolean): void;
|
|
138
|
+
}
|
|
139
|
+
export default BaseControllerV1;
|
|
140
|
+
//# sourceMappingURL=BaseControllerV1.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseControllerV1.d.cts","sourceRoot":"","sources":["../src/BaseControllerV1.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,wBAAwB;AAEvD,OAAO,KAAK,EAAE,kBAAkB,EAAE,+BAA2B;AAE7D;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,kBAAkB,GAC7B,UAAU,IAAI,wBAAwB,CAiBxC;AAED;;GAEG;AAGH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;AAE7C;;;;;GAKG;AAIH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;;GAKG;AAIH,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,UAAU,GAAG,MAAM,CAAC;AAEnD;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,SAAS,GAAG,MAAM,CAAC;AAEjD;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG,eAAe,CACpD,gBAAgB,CAAC,gBAAgB,EAAE,eAAe,CAAC,CACpD,CAAC;AAEF;;;;;;;;GAQG;AAGH,qBAAa,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,SAAS,SAAS;IACrE;;OAEG;IACH,aAAa,EAAE,CAAC,CAAe;IAE/B;;OAEG;IACH,YAAY,EAAE,CAAC,CAAe;IAE9B;;OAEG;IACH,QAAQ,UAAS;IAEjB;;OAEG;IACH,IAAI,SAAoB;IAExB,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAa;IAE3C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAa;IAE1C,OAAO,CAAC,cAAc,CAAyB;IAE/C,OAAO,CAAC,aAAa,CAAwB;IAE7C,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAqB;IAEvD;;;;;;OAMG;gBACS,MAAM,GAAE,OAAO,CAAC,CAAC,CAAM,EAAE,KAAK,GAAE,OAAO,CAAC,CAAC,CAAM;IAK3D;;;;;;OAMG;IACH,SAAS,CAAC,UAAU;IAQpB;;;;OAIG;IACH,IAAI,MAAM,MAET;IAED;;;;OAIG;IACH,IAAI,KAAK,MAER;IAED;;;;;;OAMG;IACH,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,SAAS,UAAQ,EAAE,UAAU,UAAO;IAwBlE;;OAEG;IACH,MAAM;IAUN;;;;OAIG;IACH,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC;IAI/B;;;;;OAKG;IACH,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC;IAMjC;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,SAAS,UAAQ;CAM5C;AAED,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { PublicInterface } from "@metamask/utils";
|
|
2
|
+
import type { ControllerInstance } from "./BaseControllerV2.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Determines if the given controller is an instance of `BaseControllerV1`
|
|
5
|
+
*
|
|
6
|
+
* @param controller - Controller instance to check
|
|
7
|
+
* @returns True if the controller is an instance of `BaseControllerV1`
|
|
8
|
+
*/
|
|
9
|
+
export declare function isBaseControllerV1(controller: ControllerInstance): controller is BaseControllerV1Instance;
|
|
10
|
+
/**
|
|
11
|
+
* State change callbacks
|
|
12
|
+
*/
|
|
13
|
+
export type Listener<T> = (state: T) => void;
|
|
14
|
+
/**
|
|
15
|
+
* @type BaseConfig
|
|
16
|
+
*
|
|
17
|
+
* Base controller configuration
|
|
18
|
+
* @property disabled - Determines if this controller is enabled
|
|
19
|
+
*/
|
|
20
|
+
export interface BaseConfig {
|
|
21
|
+
disabled?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @type BaseState
|
|
25
|
+
*
|
|
26
|
+
* Base state representation
|
|
27
|
+
* @property name - Unique name for this controller
|
|
28
|
+
*/
|
|
29
|
+
export interface BaseState {
|
|
30
|
+
name?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The narrowest supertype for `BaseControllerV1` config objects.
|
|
34
|
+
* This type can be assigned to any `BaseControllerV1` config object.
|
|
35
|
+
*/
|
|
36
|
+
export type ConfigConstraint = BaseConfig & object;
|
|
37
|
+
/**
|
|
38
|
+
* The narrowest supertype for `BaseControllerV1` state objects.
|
|
39
|
+
* This type can be assigned to any `BaseControllerV1` state object.
|
|
40
|
+
*/
|
|
41
|
+
export type StateConstraint = BaseState & object;
|
|
42
|
+
/**
|
|
43
|
+
* The widest subtype of all controller instances that extend from `BaseControllerV1`.
|
|
44
|
+
* Any `BaseControllerV1` instance can be assigned to this type.
|
|
45
|
+
*/
|
|
46
|
+
export type BaseControllerV1Instance = PublicInterface<BaseControllerV1<ConfigConstraint, StateConstraint>>;
|
|
47
|
+
/**
|
|
48
|
+
* @deprecated This class has been renamed to BaseControllerV1 and is no longer recommended for use for controllers. Please use BaseController (formerly BaseControllerV2) instead.
|
|
49
|
+
*
|
|
50
|
+
* Controller class that provides configuration, state management, and subscriptions.
|
|
51
|
+
*
|
|
52
|
+
* The core purpose of every controller is to maintain an internal data object
|
|
53
|
+
* called "state". Each controller is responsible for its own state, and all global wallet state
|
|
54
|
+
* is tracked in a controller as state.
|
|
55
|
+
*/
|
|
56
|
+
export declare class BaseControllerV1<C extends BaseConfig, S extends BaseState> {
|
|
57
|
+
/**
|
|
58
|
+
* Default options used to configure this controller
|
|
59
|
+
*/
|
|
60
|
+
defaultConfig: C;
|
|
61
|
+
/**
|
|
62
|
+
* Default state set on this controller
|
|
63
|
+
*/
|
|
64
|
+
defaultState: S;
|
|
65
|
+
/**
|
|
66
|
+
* Determines if listeners are notified of state changes
|
|
67
|
+
*/
|
|
68
|
+
disabled: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Name of this controller used during composition
|
|
71
|
+
*/
|
|
72
|
+
name: string;
|
|
73
|
+
private readonly initialConfig;
|
|
74
|
+
private readonly initialState;
|
|
75
|
+
private internalConfig;
|
|
76
|
+
private internalState;
|
|
77
|
+
private readonly internalListeners;
|
|
78
|
+
/**
|
|
79
|
+
* Creates a BaseControllerV1 instance. Both initial state and initial
|
|
80
|
+
* configuration options are merged with defaults upon initialization.
|
|
81
|
+
*
|
|
82
|
+
* @param config - Initial options used to configure this controller.
|
|
83
|
+
* @param state - Initial state to set on this controller.
|
|
84
|
+
*/
|
|
85
|
+
constructor(config?: Partial<C>, state?: Partial<S>);
|
|
86
|
+
/**
|
|
87
|
+
* Enables the controller. This sets each config option as a member
|
|
88
|
+
* variable on this instance and triggers any defined setters. This
|
|
89
|
+
* also sets initial state and triggers any listeners.
|
|
90
|
+
*
|
|
91
|
+
* @returns This controller instance.
|
|
92
|
+
*/
|
|
93
|
+
protected initialize(): this;
|
|
94
|
+
/**
|
|
95
|
+
* Retrieves current controller configuration options.
|
|
96
|
+
*
|
|
97
|
+
* @returns The current configuration.
|
|
98
|
+
*/
|
|
99
|
+
get config(): C;
|
|
100
|
+
/**
|
|
101
|
+
* Retrieves current controller state.
|
|
102
|
+
*
|
|
103
|
+
* @returns The current state.
|
|
104
|
+
*/
|
|
105
|
+
get state(): S;
|
|
106
|
+
/**
|
|
107
|
+
* Updates controller configuration.
|
|
108
|
+
*
|
|
109
|
+
* @param config - New configuration options.
|
|
110
|
+
* @param overwrite - Overwrite config instead of merging.
|
|
111
|
+
* @param fullUpdate - Boolean that defines if the update is partial or not.
|
|
112
|
+
*/
|
|
113
|
+
configure(config: Partial<C>, overwrite?: boolean, fullUpdate?: boolean): void;
|
|
114
|
+
/**
|
|
115
|
+
* Notifies all subscribed listeners of current state.
|
|
116
|
+
*/
|
|
117
|
+
notify(): void;
|
|
118
|
+
/**
|
|
119
|
+
* Adds new listener to be notified of state changes.
|
|
120
|
+
*
|
|
121
|
+
* @param listener - The callback triggered when state changes.
|
|
122
|
+
*/
|
|
123
|
+
subscribe(listener: Listener<S>): void;
|
|
124
|
+
/**
|
|
125
|
+
* Removes existing listener from receiving state changes.
|
|
126
|
+
*
|
|
127
|
+
* @param listener - The callback to remove.
|
|
128
|
+
* @returns `true` if a listener is found and unsubscribed.
|
|
129
|
+
*/
|
|
130
|
+
unsubscribe(listener: Listener<S>): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Updates controller state.
|
|
133
|
+
*
|
|
134
|
+
* @param state - The new state.
|
|
135
|
+
* @param overwrite - Overwrite state instead of merging.
|
|
136
|
+
*/
|
|
137
|
+
update(state: Partial<S>, overwrite?: boolean): void;
|
|
138
|
+
}
|
|
139
|
+
export default BaseControllerV1;
|
|
140
|
+
//# sourceMappingURL=BaseControllerV1.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseControllerV1.d.mts","sourceRoot":"","sources":["../src/BaseControllerV1.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,wBAAwB;AAEvD,OAAO,KAAK,EAAE,kBAAkB,EAAE,+BAA2B;AAE7D;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,kBAAkB,GAC7B,UAAU,IAAI,wBAAwB,CAiBxC;AAED;;GAEG;AAGH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;AAE7C;;;;;GAKG;AAIH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;;GAKG;AAIH,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC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ACH,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,SAAS,UAAQ;CAM5C;AAED,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines if the given controller is an instance of `BaseControllerV1`
|
|
3
|
+
*
|
|
4
|
+
* @param controller - Controller instance to check
|
|
5
|
+
* @returns True if the controller is an instance of `BaseControllerV1`
|
|
6
|
+
*/
|
|
7
|
+
export function isBaseControllerV1(controller) {
|
|
8
|
+
return ('name' in controller &&
|
|
9
|
+
typeof controller.name === 'string' &&
|
|
10
|
+
'config' in controller &&
|
|
11
|
+
typeof controller.config === 'object' &&
|
|
12
|
+
'defaultConfig' in controller &&
|
|
13
|
+
typeof controller.defaultConfig === 'object' &&
|
|
14
|
+
'state' in controller &&
|
|
15
|
+
typeof controller.state === 'object' &&
|
|
16
|
+
'defaultState' in controller &&
|
|
17
|
+
typeof controller.defaultState === 'object' &&
|
|
18
|
+
'disabled' in controller &&
|
|
19
|
+
typeof controller.disabled === 'boolean' &&
|
|
20
|
+
'subscribe' in controller &&
|
|
21
|
+
typeof controller.subscribe === 'function');
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated This class has been renamed to BaseControllerV1 and is no longer recommended for use for controllers. Please use BaseController (formerly BaseControllerV2) instead.
|
|
25
|
+
*
|
|
26
|
+
* Controller class that provides configuration, state management, and subscriptions.
|
|
27
|
+
*
|
|
28
|
+
* The core purpose of every controller is to maintain an internal data object
|
|
29
|
+
* called "state". Each controller is responsible for its own state, and all global wallet state
|
|
30
|
+
* is tracked in a controller as state.
|
|
31
|
+
*/
|
|
32
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
34
|
+
export class BaseControllerV1 {
|
|
35
|
+
/**
|
|
36
|
+
* Creates a BaseControllerV1 instance. Both initial state and initial
|
|
37
|
+
* configuration options are merged with defaults upon initialization.
|
|
38
|
+
*
|
|
39
|
+
* @param config - Initial options used to configure this controller.
|
|
40
|
+
* @param state - Initial state to set on this controller.
|
|
41
|
+
*/
|
|
42
|
+
constructor(config = {}, state = {}) {
|
|
43
|
+
/**
|
|
44
|
+
* Default options used to configure this controller
|
|
45
|
+
*/
|
|
46
|
+
this.defaultConfig = {};
|
|
47
|
+
/**
|
|
48
|
+
* Default state set on this controller
|
|
49
|
+
*/
|
|
50
|
+
this.defaultState = {};
|
|
51
|
+
/**
|
|
52
|
+
* Determines if listeners are notified of state changes
|
|
53
|
+
*/
|
|
54
|
+
this.disabled = false;
|
|
55
|
+
/**
|
|
56
|
+
* Name of this controller used during composition
|
|
57
|
+
*/
|
|
58
|
+
this.name = 'BaseController';
|
|
59
|
+
this.internalConfig = this.defaultConfig;
|
|
60
|
+
this.internalState = this.defaultState;
|
|
61
|
+
this.internalListeners = [];
|
|
62
|
+
this.initialState = state;
|
|
63
|
+
this.initialConfig = config;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Enables the controller. This sets each config option as a member
|
|
67
|
+
* variable on this instance and triggers any defined setters. This
|
|
68
|
+
* also sets initial state and triggers any listeners.
|
|
69
|
+
*
|
|
70
|
+
* @returns This controller instance.
|
|
71
|
+
*/
|
|
72
|
+
initialize() {
|
|
73
|
+
this.internalState = this.defaultState;
|
|
74
|
+
this.internalConfig = this.defaultConfig;
|
|
75
|
+
this.configure(this.initialConfig);
|
|
76
|
+
this.update(this.initialState);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Retrieves current controller configuration options.
|
|
81
|
+
*
|
|
82
|
+
* @returns The current configuration.
|
|
83
|
+
*/
|
|
84
|
+
get config() {
|
|
85
|
+
return this.internalConfig;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Retrieves current controller state.
|
|
89
|
+
*
|
|
90
|
+
* @returns The current state.
|
|
91
|
+
*/
|
|
92
|
+
get state() {
|
|
93
|
+
return this.internalState;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Updates controller configuration.
|
|
97
|
+
*
|
|
98
|
+
* @param config - New configuration options.
|
|
99
|
+
* @param overwrite - Overwrite config instead of merging.
|
|
100
|
+
* @param fullUpdate - Boolean that defines if the update is partial or not.
|
|
101
|
+
*/
|
|
102
|
+
configure(config, overwrite = false, fullUpdate = true) {
|
|
103
|
+
if (fullUpdate) {
|
|
104
|
+
this.internalConfig = overwrite
|
|
105
|
+
? config
|
|
106
|
+
: Object.assign(this.internalConfig, config);
|
|
107
|
+
for (const key of Object.keys(this.internalConfig)) {
|
|
108
|
+
const value = this.internalConfig[key];
|
|
109
|
+
if (value !== undefined) {
|
|
110
|
+
this[key] = value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
for (const key of Object.keys(config)) {
|
|
116
|
+
/* istanbul ignore else */
|
|
117
|
+
if (this.internalConfig[key] !== undefined) {
|
|
118
|
+
const value = config[key];
|
|
119
|
+
this.internalConfig[key] = value;
|
|
120
|
+
this[key] = value;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Notifies all subscribed listeners of current state.
|
|
127
|
+
*/
|
|
128
|
+
notify() {
|
|
129
|
+
if (this.disabled) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.internalListeners.forEach((listener) => {
|
|
133
|
+
listener(this.internalState);
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Adds new listener to be notified of state changes.
|
|
138
|
+
*
|
|
139
|
+
* @param listener - The callback triggered when state changes.
|
|
140
|
+
*/
|
|
141
|
+
subscribe(listener) {
|
|
142
|
+
this.internalListeners.push(listener);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Removes existing listener from receiving state changes.
|
|
146
|
+
*
|
|
147
|
+
* @param listener - The callback to remove.
|
|
148
|
+
* @returns `true` if a listener is found and unsubscribed.
|
|
149
|
+
*/
|
|
150
|
+
unsubscribe(listener) {
|
|
151
|
+
const index = this.internalListeners.findIndex((cb) => listener === cb);
|
|
152
|
+
index > -1 && this.internalListeners.splice(index, 1);
|
|
153
|
+
return index > -1;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Updates controller state.
|
|
157
|
+
*
|
|
158
|
+
* @param state - The new state.
|
|
159
|
+
* @param overwrite - Overwrite state instead of merging.
|
|
160
|
+
*/
|
|
161
|
+
update(state, overwrite = false) {
|
|
162
|
+
this.internalState = overwrite
|
|
163
|
+
? Object.assign({}, state)
|
|
164
|
+
: Object.assign({}, this.internalState, state);
|
|
165
|
+
this.notify();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
export default BaseControllerV1;
|
|
169
|
+
//# sourceMappingURL=BaseControllerV1.mjs.map
|