@metamask-previews/base-controller 3.2.3-preview.e3b05ea → 3.2.3-preview.eb58a59
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/BaseController.d.ts.map +1 -1
- package/dist/BaseController.js.map +1 -1
- package/dist/BaseControllerV2.d.ts +26 -15
- package/dist/BaseControllerV2.d.ts.map +1 -1
- package/dist/BaseControllerV2.js +22 -8
- package/dist/BaseControllerV2.js.map +1 -1
- package/dist/ControllerMessenger.d.ts +51 -209
- package/dist/ControllerMessenger.d.ts.map +1 -1
- package/dist/ControllerMessenger.js +34 -200
- package/dist/ControllerMessenger.js.map +1 -1
- package/dist/RestrictedControllerMessenger.d.ts +154 -0
- package/dist/RestrictedControllerMessenger.d.ts.map +1 -0
- package/dist/RestrictedControllerMessenger.js +195 -0
- package/dist/RestrictedControllerMessenger.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _RestrictedControllerMessenger_controllerMessenger, _RestrictedControllerMessenger_controllerName, _RestrictedControllerMessenger_allowedActions, _RestrictedControllerMessenger_allowedEvents;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.RestrictedControllerMessenger = void 0;
|
|
16
|
+
/**
|
|
17
|
+
* A restricted controller messenger.
|
|
18
|
+
*
|
|
19
|
+
* This acts as a wrapper around the controller messenger instance that restricts access to actions
|
|
20
|
+
* and events.
|
|
21
|
+
*
|
|
22
|
+
* @template Namespace - The namespace for this messenger. Typically this is the name of the controller or
|
|
23
|
+
* module that this messenger has been created for. The authority to publish events and register
|
|
24
|
+
* actions under this namespace is granted to this restricted messenger instance.
|
|
25
|
+
* @template Action - A type union of all Action types.
|
|
26
|
+
* @template Event - A type union of all Event types.
|
|
27
|
+
* @template AllowedAction - A type union of the 'type' string for any allowed actions.
|
|
28
|
+
* @template AllowedEvent - A type union of the 'type' string for any allowed events.
|
|
29
|
+
*/
|
|
30
|
+
class RestrictedControllerMessenger {
|
|
31
|
+
/**
|
|
32
|
+
* Constructs a restricted controller messenger
|
|
33
|
+
*
|
|
34
|
+
* The provided allowlists grant the ability to call the listed actions and subscribe to the
|
|
35
|
+
* listed events. The "name" provided grants ownership of any actions and events under that
|
|
36
|
+
* namespace. Ownership allows registering actions and publishing events, as well as
|
|
37
|
+
* unregistering actions and clearing event subscriptions.
|
|
38
|
+
*
|
|
39
|
+
* @param options - The controller options.
|
|
40
|
+
* @param options.controllerMessenger - The controller messenger instance that is being wrapped.
|
|
41
|
+
* @param options.name - The name of the thing this messenger will be handed to (e.g. the
|
|
42
|
+
* controller name). This grants "ownership" of actions and events under this namespace to the
|
|
43
|
+
* restricted controller messenger returned.
|
|
44
|
+
* @param options.allowedActions - The list of actions that this restricted controller messenger
|
|
45
|
+
* should be alowed to call.
|
|
46
|
+
* @param options.allowedEvents - The list of events that this restricted controller messenger
|
|
47
|
+
* should be allowed to subscribe to.
|
|
48
|
+
*/
|
|
49
|
+
constructor({ controllerMessenger, name, allowedActions, allowedEvents, }) {
|
|
50
|
+
_RestrictedControllerMessenger_controllerMessenger.set(this, void 0);
|
|
51
|
+
_RestrictedControllerMessenger_controllerName.set(this, void 0);
|
|
52
|
+
_RestrictedControllerMessenger_allowedActions.set(this, void 0);
|
|
53
|
+
_RestrictedControllerMessenger_allowedEvents.set(this, void 0);
|
|
54
|
+
__classPrivateFieldSet(this, _RestrictedControllerMessenger_controllerMessenger, controllerMessenger, "f");
|
|
55
|
+
__classPrivateFieldSet(this, _RestrictedControllerMessenger_controllerName, name, "f");
|
|
56
|
+
__classPrivateFieldSet(this, _RestrictedControllerMessenger_allowedActions, allowedActions || null, "f");
|
|
57
|
+
__classPrivateFieldSet(this, _RestrictedControllerMessenger_allowedEvents, allowedEvents || null, "f");
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Register an action handler.
|
|
61
|
+
*
|
|
62
|
+
* This will make the registered function available to call via the `call` method.
|
|
63
|
+
*
|
|
64
|
+
* The action type this handler is registered under *must* be in the current namespace.
|
|
65
|
+
*
|
|
66
|
+
* @param action - The action type. This is a unqiue identifier for this action.
|
|
67
|
+
* @param handler - The action handler. This function gets called when the `call` method is
|
|
68
|
+
* invoked with the given action type.
|
|
69
|
+
* @throws Will throw if an action handler that is not in the current namespace is being registered.
|
|
70
|
+
* @template ActionType - A type union of Action type strings that are namespaced by Namespace.
|
|
71
|
+
*/
|
|
72
|
+
registerActionHandler(action, handler) {
|
|
73
|
+
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
74
|
+
if (!action.startsWith(`${__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerName, "f")}:`)) {
|
|
75
|
+
throw new Error(`Only allowed registering action handlers prefixed by '${__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerName, "f")}:'`);
|
|
76
|
+
}
|
|
77
|
+
__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerMessenger, "f").registerActionHandler(action, handler);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Unregister an action handler.
|
|
81
|
+
*
|
|
82
|
+
* This will prevent this action from being called.
|
|
83
|
+
*
|
|
84
|
+
* The action type being unregistered *must* be in the current namespace.
|
|
85
|
+
*
|
|
86
|
+
* @param action - The action type. This is a unqiue identifier for this action.
|
|
87
|
+
* @template ActionType - A type union of Action type strings that are namespaced by Namespace.
|
|
88
|
+
*/
|
|
89
|
+
unregisterActionHandler(action) {
|
|
90
|
+
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
91
|
+
if (!action.startsWith(`${__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerName, "f")}:`)) {
|
|
92
|
+
throw new Error(`Only allowed unregistering action handlers prefixed by '${__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerName, "f")}:'`);
|
|
93
|
+
}
|
|
94
|
+
__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerMessenger, "f").unregisterActionHandler(action);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Call an action.
|
|
98
|
+
*
|
|
99
|
+
* This function will call the action handler corresponding to the given action type, passing
|
|
100
|
+
* along any parameters given.
|
|
101
|
+
*
|
|
102
|
+
* The action type being called must be on the action allowlist.
|
|
103
|
+
*
|
|
104
|
+
* @param action - The action type. This is a unqiue identifier for this action.
|
|
105
|
+
* @param params - The action parameters. These must match the type of the parameters of the
|
|
106
|
+
* registered action handler.
|
|
107
|
+
* @throws Will throw when no handler has been registered for the given type.
|
|
108
|
+
* @template ActionType - A type union of allowed Action type strings.
|
|
109
|
+
* @returns The action return value.
|
|
110
|
+
*/
|
|
111
|
+
call(action, ...params) {
|
|
112
|
+
/* istanbul ignore next */ // Branches unreachable with valid types
|
|
113
|
+
if (__classPrivateFieldGet(this, _RestrictedControllerMessenger_allowedActions, "f") === null) {
|
|
114
|
+
throw new Error('No actions allowed');
|
|
115
|
+
}
|
|
116
|
+
else if (!__classPrivateFieldGet(this, _RestrictedControllerMessenger_allowedActions, "f").includes(action)) {
|
|
117
|
+
throw new Error(`Action missing from allow list: ${action}`);
|
|
118
|
+
}
|
|
119
|
+
return __classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerMessenger, "f").call(action, ...params);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Publish an event.
|
|
123
|
+
*
|
|
124
|
+
* Publishes the given payload to all subscribers of the given event type.
|
|
125
|
+
*
|
|
126
|
+
* The event type being published *must* be in the current namespace.
|
|
127
|
+
*
|
|
128
|
+
* @param event - The event type. This is a unique identifier for this event.
|
|
129
|
+
* @param payload - The event payload. The type of the parameters for each event handler must
|
|
130
|
+
* match the type of this payload.
|
|
131
|
+
* @template EventType - A type union of Event type strings that are namespaced by Namespace.
|
|
132
|
+
*/
|
|
133
|
+
publish(event, ...payload) {
|
|
134
|
+
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
135
|
+
if (!event.startsWith(`${__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerName, "f")}:`)) {
|
|
136
|
+
throw new Error(`Only allowed publishing events prefixed by '${__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerName, "f")}:'`);
|
|
137
|
+
}
|
|
138
|
+
__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerMessenger, "f").publish(event, ...payload);
|
|
139
|
+
}
|
|
140
|
+
subscribe(event, handler, selector) {
|
|
141
|
+
/* istanbul ignore next */ // Branches unreachable with valid types
|
|
142
|
+
if (__classPrivateFieldGet(this, _RestrictedControllerMessenger_allowedEvents, "f") === null) {
|
|
143
|
+
throw new Error('No events allowed');
|
|
144
|
+
}
|
|
145
|
+
else if (!__classPrivateFieldGet(this, _RestrictedControllerMessenger_allowedEvents, "f").includes(event)) {
|
|
146
|
+
throw new Error(`Event missing from allow list: ${event}`);
|
|
147
|
+
}
|
|
148
|
+
if (selector) {
|
|
149
|
+
return __classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerMessenger, "f").subscribe(event, handler, selector);
|
|
150
|
+
}
|
|
151
|
+
return __classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerMessenger, "f").subscribe(event, handler);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Unsubscribe from an event.
|
|
155
|
+
*
|
|
156
|
+
* Unregisters the given function as an event handler for the given event.
|
|
157
|
+
*
|
|
158
|
+
* The event type being unsubscribed to must be on the event allowlist.
|
|
159
|
+
*
|
|
160
|
+
* @param event - The event type. This is a unique identifier for this event.
|
|
161
|
+
* @param handler - The event handler to unregister.
|
|
162
|
+
* @throws Will throw when the given event handler is not registered for this event.
|
|
163
|
+
* @template EventType - A type union of allowed Event type strings.
|
|
164
|
+
*/
|
|
165
|
+
unsubscribe(event, handler) {
|
|
166
|
+
/* istanbul ignore next */ // Branches unreachable with valid types
|
|
167
|
+
if (__classPrivateFieldGet(this, _RestrictedControllerMessenger_allowedEvents, "f") === null) {
|
|
168
|
+
throw new Error('No events allowed');
|
|
169
|
+
}
|
|
170
|
+
else if (!__classPrivateFieldGet(this, _RestrictedControllerMessenger_allowedEvents, "f").includes(event)) {
|
|
171
|
+
throw new Error(`Event missing from allow list: ${event}`);
|
|
172
|
+
}
|
|
173
|
+
__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerMessenger, "f").unsubscribe(event, handler);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Clear subscriptions for a specific event.
|
|
177
|
+
*
|
|
178
|
+
* This will remove all subscribed handlers for this event.
|
|
179
|
+
*
|
|
180
|
+
* The event type being cleared *must* be in the current namespace.
|
|
181
|
+
*
|
|
182
|
+
* @param event - The event type. This is a unique identifier for this event.
|
|
183
|
+
* @template EventType - A type union of Event type strings that are namespaced by Namespace.
|
|
184
|
+
*/
|
|
185
|
+
clearEventSubscriptions(event) {
|
|
186
|
+
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
187
|
+
if (!event.startsWith(`${__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerName, "f")}:`)) {
|
|
188
|
+
throw new Error(`Only allowed clearing events prefixed by '${__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerName, "f")}:'`);
|
|
189
|
+
}
|
|
190
|
+
__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerMessenger, "f").clearEventSubscriptions(event);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.RestrictedControllerMessenger = RestrictedControllerMessenger;
|
|
194
|
+
_RestrictedControllerMessenger_controllerMessenger = new WeakMap(), _RestrictedControllerMessenger_controllerName = new WeakMap(), _RestrictedControllerMessenger_allowedActions = new WeakMap(), _RestrictedControllerMessenger_allowedEvents = new WeakMap();
|
|
195
|
+
//# sourceMappingURL=RestrictedControllerMessenger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RestrictedControllerMessenger.js","sourceRoot":"","sources":["../src/RestrictedControllerMessenger.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAcA;;;;;;;;;;;;;GAaG;AACH,MAAa,6BAA6B;IAkBxC;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,EACV,mBAAmB,EACnB,IAAI,EACJ,cAAc,EACd,aAAa,GAMd;QAvCD,qEAGE;QAEF,gEAAoC;QAEpC,gEAAiD;QAEjD,+DAA+C;QA+B7C,uBAAA,IAAI,sDAAwB,mBAAmB,MAAA,CAAC;QAChD,uBAAA,IAAI,iDAAmB,IAAI,MAAA,CAAC;QAC5B,uBAAA,IAAI,iDAAmB,cAAc,IAAI,IAAI,MAAA,CAAC;QAC9C,uBAAA,IAAI,gDAAkB,aAAa,IAAI,IAAI,MAAA,CAAC;IAC9C,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,qBAAqB,CACnB,MAAkB,EAClB,OAA0C;QAE1C,wBAAwB,CAAC,sCAAsC;QAC/D,IAAI,CAAC,MAAM,CAAC,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AA,IAAI,qDAAgB,GAAG,CAAC,EAAE;YACjD,MAAM,IAAI,KAAK,CACb,6CAA6C,uBAAA,IAAI,qDAAgB,IAAI,CACtE,CAAC;SACH;QACD,uBAAA,IAAI,0DAAqB,CAAC,uBAAuB,CAAC,KAAK,CAAC,CAAC;IAC3D,CAAC;CACF;AApRD,sEAoRC","sourcesContent":["import type {\n ActionConstraint,\n ActionHandler,\n ControllerMessenger,\n EventConstraint,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n NamespacedName,\n SelectorEventHandler,\n SelectorFunction,\n} from './ControllerMessenger';\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 * @template AllowedEvent - A type union of the 'type' string for any allowed events.\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: AllowedAction[] | null;\n\n readonly #allowedEvents: AllowedEvent[] | null;\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?: AllowedAction[];\n allowedEvents?: AllowedEvent[];\n }) {\n this.#controllerMessenger = controllerMessenger;\n this.#controllerName = name;\n this.#allowedActions = allowedActions || null;\n this.#allowedEvents = allowedEvents || null;\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<ActionType extends Action['type']>(\n action: ActionType,\n handler: ActionHandler<Action, ActionType>,\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!action.startsWith(`${this.#controllerName}:`)) {\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 unqiue identifier for this action.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n unregisterActionHandler<ActionType extends NamespacedName<Namespace>>(\n action: ActionType,\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!action.startsWith(`${this.#controllerName}:`)) {\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 action - 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<ActionType extends AllowedAction & NamespacedName>(\n action: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n /* istanbul ignore next */ // Branches unreachable with valid types\n if (this.#allowedActions === null) {\n throw new Error('No actions allowed');\n } else if (!this.#allowedActions.includes(action)) {\n throw new Error(`Action missing from allow list: ${action}`);\n }\n return this.#controllerMessenger.call(action, ...params);\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 * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n publish<EventType extends NamespacedName<Namespace>>(\n event: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!event.startsWith(`${this.#controllerName}:`)) {\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 * @template EventType - A type union of Event type strings.\n */\n subscribe<EventType extends AllowedEvent & NamespacedName>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ): 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 * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<\n EventType extends AllowedEvent & NamespacedName,\n SelectorReturnValue,\n >(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<\n ExtractEventPayload<Event, EventType>,\n SelectorReturnValue\n >,\n ): void;\n\n subscribe<\n EventType extends AllowedEvent & NamespacedName,\n SelectorReturnValue,\n >(\n event: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<\n ExtractEventPayload<Event, EventType>,\n SelectorReturnValue\n >,\n ) {\n /* istanbul ignore next */ // Branches unreachable with valid types\n if (this.#allowedEvents === null) {\n throw new Error('No events allowed');\n } else if (!this.#allowedEvents.includes(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 when the given event handler is not registered for this event.\n * @template EventType - A type union of allowed Event type strings.\n */\n unsubscribe<EventType extends AllowedEvent & NamespacedName>(\n event: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ) {\n /* istanbul ignore next */ // Branches unreachable with valid types\n if (this.#allowedEvents === null) {\n throw new Error('No events allowed');\n } else if (!this.#allowedEvents.includes(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 * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n clearEventSubscriptions<EventType extends NamespacedName<Namespace>>(\n event: EventType,\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!event.startsWith(`${this.#controllerName}:`)) {\n throw new Error(\n `Only allowed clearing events prefixed by '${this.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.clearEventSubscriptions(event);\n }\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type { BaseConfig, BaseState, Listener } from './BaseController';
|
|
2
2
|
export { BaseController } from './BaseController';
|
|
3
3
|
export type { Listener as ListenerV2, StateDeriver, StateMetadata, StatePropertyMetadata, } from './BaseControllerV2';
|
|
4
|
-
export { BaseController as BaseControllerV2, getAnonymizedState, getPersistentState, } from './BaseControllerV2';
|
|
4
|
+
export { BaseController as BaseControllerV2, getAnonymizedState, getPersistentState, type ControllerGetStateAction, type ControllerStateChangeEvent, } from './BaseControllerV2';
|
|
5
5
|
export * from './ControllerMessenger';
|
|
6
|
+
export * from './RestrictedControllerMessenger';
|
|
6
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,YAAY,EACV,QAAQ,IAAI,UAAU,EACtB,YAAY,EACZ,aAAa,EACb,qBAAqB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,cAAc,IAAI,gBAAgB,EAClC,kBAAkB,EAClB,kBAAkB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,YAAY,EACV,QAAQ,IAAI,UAAU,EACtB,YAAY,EACZ,aAAa,EACb,qBAAqB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,cAAc,IAAI,gBAAgB,EAClC,kBAAkB,EAClB,kBAAkB,EAClB,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,GAChC,MAAM,oBAAoB,CAAC;AAC5B,cAAc,uBAAuB,CAAC;AACtC,cAAc,iCAAiC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -22,4 +22,5 @@ Object.defineProperty(exports, "BaseControllerV2", { enumerable: true, get: func
|
|
|
22
22
|
Object.defineProperty(exports, "getAnonymizedState", { enumerable: true, get: function () { return BaseControllerV2_1.getAnonymizedState; } });
|
|
23
23
|
Object.defineProperty(exports, "getPersistentState", { enumerable: true, get: function () { return BaseControllerV2_1.getPersistentState; } });
|
|
24
24
|
__exportStar(require("./ControllerMessenger"), exports);
|
|
25
|
+
__exportStar(require("./RestrictedControllerMessenger"), exports);
|
|
25
26
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,mDAAkD;AAAzC,gHAAA,cAAc,OAAA;AAOvB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,mDAAkD;AAAzC,gHAAA,cAAc,OAAA;AAOvB,uDAM4B;AAL1B,oHAAA,cAAc,OAAoB;AAClC,sHAAA,kBAAkB,OAAA;AAClB,sHAAA,kBAAkB,OAAA;AAIpB,wDAAsC;AACtC,kEAAgD","sourcesContent":["export type { BaseConfig, BaseState, Listener } from './BaseController';\nexport { BaseController } from './BaseController';\nexport type {\n Listener as ListenerV2,\n StateDeriver,\n StateMetadata,\n StatePropertyMetadata,\n} from './BaseControllerV2';\nexport {\n BaseController as BaseControllerV2,\n getAnonymizedState,\n getPersistentState,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from './BaseControllerV2';\nexport * from './ControllerMessenger';\nexport * from './RestrictedControllerMessenger';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/base-controller",
|
|
3
|
-
"version": "3.2.3-preview.
|
|
3
|
+
"version": "3.2.3-preview.eb58a59",
|
|
4
4
|
"description": "Provides scaffolding for controllers as well a communication system for all controllers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -24,16 +24,17 @@
|
|
|
24
24
|
"build:docs": "typedoc",
|
|
25
25
|
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/base-controller",
|
|
26
26
|
"publish:preview": "yarn npm publish --tag preview",
|
|
27
|
-
"test": "jest",
|
|
27
|
+
"test": "jest --reporters=jest-silent-reporter",
|
|
28
28
|
"test:clean": "jest --clearCache",
|
|
29
|
+
"test:verbose": "jest --verbose",
|
|
29
30
|
"test:watch": "jest --watch"
|
|
30
31
|
},
|
|
31
32
|
"dependencies": {
|
|
32
|
-
"@metamask/utils": "^8.
|
|
33
|
+
"@metamask/utils": "^8.2.0",
|
|
33
34
|
"immer": "^9.0.6"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
|
-
"@metamask/auto-changelog": "^3.4.
|
|
37
|
+
"@metamask/auto-changelog": "^3.4.3",
|
|
37
38
|
"@types/jest": "^27.4.1",
|
|
38
39
|
"@types/sinon": "^9.0.10",
|
|
39
40
|
"deepmerge": "^4.2.2",
|