@metamask-previews/base-controller 7.0.2-preview-e58ce4ab → 7.0.2-preview-1d772cc
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 +9 -0
- package/dist/BaseControllerV2.cjs.map +1 -1
- package/dist/BaseControllerV2.d.cts +2 -2
- package/dist/BaseControllerV2.d.cts.map +1 -1
- package/dist/BaseControllerV2.d.mts +2 -2
- package/dist/BaseControllerV2.d.mts.map +1 -1
- package/dist/BaseControllerV2.mjs.map +1 -1
- package/dist/{ControllerMessenger.mjs → Messenger.cjs} +47 -42
- package/dist/Messenger.cjs.map +1 -0
- package/dist/{ControllerMessenger.d.mts → Messenger.d.cts} +19 -19
- package/dist/Messenger.d.cts.map +1 -0
- package/dist/{ControllerMessenger.d.cts → Messenger.d.mts} +19 -19
- package/dist/Messenger.d.mts.map +1 -0
- package/dist/{ControllerMessenger.cjs → Messenger.mjs} +43 -46
- package/dist/Messenger.mjs.map +1 -0
- package/dist/{RestrictedControllerMessenger.mjs → RestrictedMessenger.cjs} +68 -54
- package/dist/RestrictedMessenger.cjs.map +1 -0
- package/dist/{RestrictedControllerMessenger.d.cts → RestrictedMessenger.d.cts} +35 -23
- package/dist/RestrictedMessenger.d.cts.map +1 -0
- package/dist/{RestrictedControllerMessenger.d.mts → RestrictedMessenger.d.mts} +35 -23
- package/dist/RestrictedMessenger.d.mts.map +1 -0
- package/dist/{RestrictedControllerMessenger.cjs → RestrictedMessenger.mjs} +64 -58
- package/dist/RestrictedMessenger.mjs.map +1 -0
- package/dist/index.cjs +7 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/ControllerMessenger.cjs.map +0 -1
- package/dist/ControllerMessenger.d.cts.map +0 -1
- package/dist/ControllerMessenger.d.mts.map +0 -1
- package/dist/ControllerMessenger.mjs.map +0 -1
- package/dist/RestrictedControllerMessenger.cjs.map +0 -1
- package/dist/RestrictedControllerMessenger.d.cts.map +0 -1
- package/dist/RestrictedControllerMessenger.d.mts.map +0 -1
- package/dist/RestrictedControllerMessenger.mjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Messenger.d.cts","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAE5D,MAAM,MAAM,aAAa,CACvB,MAAM,SAAS,gBAAgB,EAC/B,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IACzB,CACF,GAAG,IAAI,EAAE,uBAAuB,CAAC,MAAM,EAAE,UAAU,CAAC,KACjD,qBAAqB,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAE/C,MAAM,MAAM,uBAAuB,CACjC,MAAM,SAAS,gBAAgB,EAC/B,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IACzB,MAAM,SAAS;IACjB,IAAI,EAAE,UAAU,CAAC;IACjB,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,WAAW,KAAK,OAAO,CAAC;CAClD,GACG,WAAW,GACX,KAAK,CAAC;AAEV,MAAM,MAAM,qBAAqB,CAC/B,MAAM,SAAS,gBAAgB,EAC/B,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IACzB,MAAM,SAAS;IACjB,IAAI,EAAE,UAAU,CAAC;IACjB,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,CAAC,KAAK,MAAM,kBAAkB,CAAC;CACzD,GACG,kBAAkB,GAClB,KAAK,CAAC;AAEV,MAAM,MAAM,mBAAmB,CAC7B,KAAK,SAAS,eAAe,EAC7B,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC,IACvB,KAAK,SAAS;IAChB,IAAI,EAAE,SAAS,CAAC;IAChB,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AAC,GAAG,KAAK,EACxE,YAAY,SAAS,eAAe,CAAC,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,EACtE,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GACd,EAAE;QACD,IAAI,EAAE,SAAS,CAAC;QAChB,cAAc,EAAE,eAAe,CAC7B,SAAS,EACT,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,aAAa,CAAC,CACvC,EAAE,CAAC;QACJ,aAAa,EAAE,eAAe,CAC5B,SAAS,EACT,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC,CACrC,EAAE,CAAC;KACL,GAAG,mBAAmB,CACrB,SAAS,EACP,iBAAiB,CAAC,MAAM,EAAE,SAAS,CAAC,GACpC,eAAe,CAAC,MAAM,EAAE,aAAa,CAAC,EACxC,iBAAiB,CAAC,KAAK,EAAE,SAAS,CAAC,GAAG,eAAe,CAAC,KAAK,EAAE,YAAY,CAAC,EAC1E,aAAa,EACb,YAAY,CACb;CAQF;AAED,OAAO,EAAE,SAAS,IAAI,mBAAmB,EAAE,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RestrictedMessenger } from "./RestrictedMessenger.mjs";
|
|
2
2
|
export type ActionHandler<Action extends ActionConstraint, ActionType = Action['type']> = (...args: ExtractActionParameters<Action, ActionType>) => ExtractActionResponse<Action, ActionType>;
|
|
3
3
|
export type ExtractActionParameters<Action extends ActionConstraint, ActionType = Action['type']> = Action extends {
|
|
4
4
|
type: ActionType;
|
|
@@ -45,16 +45,16 @@ type NarrowToAllowed<Name, Allowed extends string> = Name extends {
|
|
|
45
45
|
type: Allowed;
|
|
46
46
|
} ? Name : never;
|
|
47
47
|
/**
|
|
48
|
-
* A
|
|
48
|
+
* A message broker for "actions" and "events".
|
|
49
49
|
*
|
|
50
|
-
* The
|
|
50
|
+
* The messenger allows registering functions as 'actions' that can be called elsewhere,
|
|
51
51
|
* and it allows publishing and subscribing to events. Both actions and events are identified by
|
|
52
52
|
* unique strings.
|
|
53
53
|
*
|
|
54
54
|
* @template Action - A type union of all Action types.
|
|
55
55
|
* @template Event - A type union of all Event types.
|
|
56
56
|
*/
|
|
57
|
-
export declare class
|
|
57
|
+
export declare class Messenger<Action extends ActionConstraint, Event extends EventConstraint> {
|
|
58
58
|
#private;
|
|
59
59
|
/**
|
|
60
60
|
* Register an action handler.
|
|
@@ -182,36 +182,36 @@ export declare class ControllerMessenger<Action extends ActionConstraint, Event
|
|
|
182
182
|
*/
|
|
183
183
|
clearSubscriptions(): void;
|
|
184
184
|
/**
|
|
185
|
-
* Get a restricted
|
|
185
|
+
* Get a restricted messenger
|
|
186
186
|
*
|
|
187
|
-
* Returns a wrapper around the
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
187
|
+
* Returns a wrapper around the messenger instance that restricts access to actions and events.
|
|
188
|
+
* The provided allowlists grant the ability to call the listed actions and subscribe to the
|
|
189
|
+
* listed events. The "name" provided grants ownership of any actions and events under that
|
|
190
|
+
* namespace. Ownership allows registering actions and publishing events, as well as
|
|
191
191
|
* unregistering actions and clearing event subscriptions.
|
|
192
192
|
*
|
|
193
193
|
* @param options - Controller messenger options.
|
|
194
194
|
* @param options.name - The name of the thing this messenger will be handed to (e.g. the
|
|
195
195
|
* controller name). This grants "ownership" of actions and events under this namespace to the
|
|
196
|
-
* restricted
|
|
197
|
-
* @param options.allowedActions - The list of actions that this restricted
|
|
198
|
-
*
|
|
199
|
-
* @param options.allowedEvents - The list of events that this restricted
|
|
200
|
-
*
|
|
201
|
-
* @template Namespace - The namespace for this messenger. Typically this is the name of the
|
|
196
|
+
* restricted messenger returned.
|
|
197
|
+
* @param options.allowedActions - The list of actions that this restricted messenger should be
|
|
198
|
+
* allowed to call.
|
|
199
|
+
* @param options.allowedEvents - The list of events that this restricted messenger should be
|
|
200
|
+
* allowed to subscribe to.
|
|
201
|
+
* @template Namespace - The namespace for this messenger. Typically this is the name of the
|
|
202
202
|
* module that this messenger has been created for. The authority to publish events and register
|
|
203
203
|
* actions under this namespace is granted to this restricted messenger instance.
|
|
204
204
|
* @template AllowedAction - A type union of the 'type' string for any allowed actions.
|
|
205
205
|
* This must not include internal actions that are in the messenger's namespace.
|
|
206
206
|
* @template AllowedEvent - A type union of the 'type' string for any allowed events.
|
|
207
207
|
* This must not include internal events that are in the messenger's namespace.
|
|
208
|
-
* @returns The restricted
|
|
208
|
+
* @returns The restricted messenger.
|
|
209
209
|
*/
|
|
210
210
|
getRestricted<Namespace extends string, AllowedAction extends NotNamespacedBy<Namespace, Action['type']> = never, AllowedEvent extends NotNamespacedBy<Namespace, Event['type']> = never>({ name, allowedActions, allowedEvents, }: {
|
|
211
211
|
name: Namespace;
|
|
212
212
|
allowedActions: NotNamespacedBy<Namespace, Extract<Action['type'], AllowedAction>>[];
|
|
213
213
|
allowedEvents: NotNamespacedBy<Namespace, Extract<Event['type'], AllowedEvent>>[];
|
|
214
|
-
}):
|
|
214
|
+
}): RestrictedMessenger<Namespace, NarrowToNamespace<Action, Namespace> | NarrowToAllowed<Action, AllowedAction>, NarrowToNamespace<Event, Namespace> | NarrowToAllowed<Event, AllowedEvent>, AllowedAction, AllowedEvent>;
|
|
215
215
|
}
|
|
216
|
-
export {};
|
|
217
|
-
//# sourceMappingURL=
|
|
216
|
+
export { Messenger as ControllerMessenger };
|
|
217
|
+
//# sourceMappingURL=Messenger.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Messenger.d.mts","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAE5D,MAAM,MAAM,aAAa,CACvB,MAAM,SAAS,gBAAgB,EAC/B,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IACzB,CACF,GAAG,IAAI,EAAE,uBAAuB,CAAC,MAAM,EAAE,UAAU,CAAC,KACjD,qBAAqB,CAAC,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B,CAAC,KAAK,EAAE,SAAS,CAAC,GAAG,eAAe,CAAC,KAAK,EAAE,YAAY,CAAC,EAC1E,aAAa,EACb,YAAY,CACb;CAQF;AAED,OAAO,EAAE,SAAS,IAAI,mBAAmB,EAAE,CAAC"}
|
|
@@ -1,37 +1,34 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
2
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
3
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
4
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
5
|
};
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
exports.ControllerMessenger = void 0;
|
|
10
|
-
const RestrictedControllerMessenger_1 = require("./RestrictedControllerMessenger.cjs");
|
|
6
|
+
var _Messenger_actions, _Messenger_events, _Messenger_initialEventPayloadGetters, _Messenger_eventPayloadCache;
|
|
7
|
+
import { RestrictedMessenger } from "./RestrictedMessenger.mjs";
|
|
11
8
|
/**
|
|
12
|
-
* A
|
|
9
|
+
* A message broker for "actions" and "events".
|
|
13
10
|
*
|
|
14
|
-
* The
|
|
11
|
+
* The messenger allows registering functions as 'actions' that can be called elsewhere,
|
|
15
12
|
* and it allows publishing and subscribing to events. Both actions and events are identified by
|
|
16
13
|
* unique strings.
|
|
17
14
|
*
|
|
18
15
|
* @template Action - A type union of all Action types.
|
|
19
16
|
* @template Event - A type union of all Event types.
|
|
20
17
|
*/
|
|
21
|
-
class
|
|
18
|
+
export class Messenger {
|
|
22
19
|
constructor() {
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
_Messenger_actions.set(this, new Map());
|
|
21
|
+
_Messenger_events.set(this, new Map());
|
|
25
22
|
/**
|
|
26
23
|
* A map of functions for getting the initial event payload.
|
|
27
24
|
*
|
|
28
25
|
* Used only for events that represent state changes.
|
|
29
26
|
*/
|
|
30
|
-
|
|
27
|
+
_Messenger_initialEventPayloadGetters.set(this, new Map());
|
|
31
28
|
/**
|
|
32
29
|
* A cache of selector return values for their respective handlers.
|
|
33
30
|
*/
|
|
34
|
-
|
|
31
|
+
_Messenger_eventPayloadCache.set(this, new Map());
|
|
35
32
|
}
|
|
36
33
|
/**
|
|
37
34
|
* Register an action handler.
|
|
@@ -45,10 +42,10 @@ class ControllerMessenger {
|
|
|
45
42
|
* @template ActionType - A type union of Action type strings.
|
|
46
43
|
*/
|
|
47
44
|
registerActionHandler(actionType, handler) {
|
|
48
|
-
if (__classPrivateFieldGet(this,
|
|
45
|
+
if (__classPrivateFieldGet(this, _Messenger_actions, "f").has(actionType)) {
|
|
49
46
|
throw new Error(`A handler for ${actionType} has already been registered`);
|
|
50
47
|
}
|
|
51
|
-
__classPrivateFieldGet(this,
|
|
48
|
+
__classPrivateFieldGet(this, _Messenger_actions, "f").set(actionType, handler);
|
|
52
49
|
}
|
|
53
50
|
/**
|
|
54
51
|
* Unregister an action handler.
|
|
@@ -59,7 +56,7 @@ class ControllerMessenger {
|
|
|
59
56
|
* @template ActionType - A type union of Action type strings.
|
|
60
57
|
*/
|
|
61
58
|
unregisterActionHandler(actionType) {
|
|
62
|
-
__classPrivateFieldGet(this,
|
|
59
|
+
__classPrivateFieldGet(this, _Messenger_actions, "f").delete(actionType);
|
|
63
60
|
}
|
|
64
61
|
/**
|
|
65
62
|
* Unregister all action handlers.
|
|
@@ -67,7 +64,7 @@ class ControllerMessenger {
|
|
|
67
64
|
* This prevents all actions from being called.
|
|
68
65
|
*/
|
|
69
66
|
clearActions() {
|
|
70
|
-
__classPrivateFieldGet(this,
|
|
67
|
+
__classPrivateFieldGet(this, _Messenger_actions, "f").clear();
|
|
71
68
|
}
|
|
72
69
|
/**
|
|
73
70
|
* Call an action.
|
|
@@ -83,7 +80,7 @@ class ControllerMessenger {
|
|
|
83
80
|
* @returns The action return value.
|
|
84
81
|
*/
|
|
85
82
|
call(actionType, ...params) {
|
|
86
|
-
const handler = __classPrivateFieldGet(this,
|
|
83
|
+
const handler = __classPrivateFieldGet(this, _Messenger_actions, "f").get(actionType);
|
|
87
84
|
if (!handler) {
|
|
88
85
|
throw new Error(`A handler for ${actionType} has not been registered`);
|
|
89
86
|
}
|
|
@@ -101,7 +98,7 @@ class ControllerMessenger {
|
|
|
101
98
|
* @param args.getPayload - A function for retrieving the event payload.
|
|
102
99
|
*/
|
|
103
100
|
registerInitialEventPayload({ eventType, getPayload, }) {
|
|
104
|
-
__classPrivateFieldGet(this,
|
|
101
|
+
__classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").set(eventType, getPayload);
|
|
105
102
|
}
|
|
106
103
|
/**
|
|
107
104
|
* Publish an event.
|
|
@@ -117,15 +114,15 @@ class ControllerMessenger {
|
|
|
117
114
|
* @template EventType - A type union of Event type strings.
|
|
118
115
|
*/
|
|
119
116
|
publish(eventType, ...payload) {
|
|
120
|
-
const subscribers = __classPrivateFieldGet(this,
|
|
117
|
+
const subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
121
118
|
if (subscribers) {
|
|
122
119
|
for (const [handler, selector] of subscribers.entries()) {
|
|
123
120
|
try {
|
|
124
121
|
if (selector) {
|
|
125
|
-
const previousValue = __classPrivateFieldGet(this,
|
|
122
|
+
const previousValue = __classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").get(handler);
|
|
126
123
|
const newValue = selector(...payload);
|
|
127
124
|
if (newValue !== previousValue) {
|
|
128
|
-
__classPrivateFieldGet(this,
|
|
125
|
+
__classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").set(handler, newValue);
|
|
129
126
|
handler(newValue, previousValue);
|
|
130
127
|
}
|
|
131
128
|
}
|
|
@@ -144,17 +141,17 @@ class ControllerMessenger {
|
|
|
144
141
|
}
|
|
145
142
|
}
|
|
146
143
|
subscribe(eventType, handler, selector) {
|
|
147
|
-
let subscribers = __classPrivateFieldGet(this,
|
|
144
|
+
let subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
148
145
|
if (!subscribers) {
|
|
149
146
|
subscribers = new Map();
|
|
150
|
-
__classPrivateFieldGet(this,
|
|
147
|
+
__classPrivateFieldGet(this, _Messenger_events, "f").set(eventType, subscribers);
|
|
151
148
|
}
|
|
152
149
|
subscribers.set(handler, selector);
|
|
153
150
|
if (selector) {
|
|
154
|
-
const getPayload = __classPrivateFieldGet(this,
|
|
151
|
+
const getPayload = __classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").get(eventType);
|
|
155
152
|
if (getPayload) {
|
|
156
153
|
const initialValue = selector(...getPayload());
|
|
157
|
-
__classPrivateFieldGet(this,
|
|
154
|
+
__classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").set(handler, initialValue);
|
|
158
155
|
}
|
|
159
156
|
}
|
|
160
157
|
}
|
|
@@ -169,13 +166,13 @@ class ControllerMessenger {
|
|
|
169
166
|
* @template EventType - A type union of Event type strings.
|
|
170
167
|
*/
|
|
171
168
|
unsubscribe(eventType, handler) {
|
|
172
|
-
const subscribers = __classPrivateFieldGet(this,
|
|
169
|
+
const subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
173
170
|
if (!subscribers || !subscribers.has(handler)) {
|
|
174
171
|
throw new Error(`Subscription not found for event: ${eventType}`);
|
|
175
172
|
}
|
|
176
173
|
const selector = subscribers.get(handler);
|
|
177
174
|
if (selector) {
|
|
178
|
-
__classPrivateFieldGet(this,
|
|
175
|
+
__classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").delete(handler);
|
|
179
176
|
}
|
|
180
177
|
subscribers.delete(handler);
|
|
181
178
|
}
|
|
@@ -188,7 +185,7 @@ class ControllerMessenger {
|
|
|
188
185
|
* @template EventType - A type union of Event type strings.
|
|
189
186
|
*/
|
|
190
187
|
clearEventSubscriptions(eventType) {
|
|
191
|
-
__classPrivateFieldGet(this,
|
|
188
|
+
__classPrivateFieldGet(this, _Messenger_events, "f").delete(eventType);
|
|
192
189
|
}
|
|
193
190
|
/**
|
|
194
191
|
* Clear all subscriptions.
|
|
@@ -196,43 +193,43 @@ class ControllerMessenger {
|
|
|
196
193
|
* This will remove all subscribed handlers for all events.
|
|
197
194
|
*/
|
|
198
195
|
clearSubscriptions() {
|
|
199
|
-
__classPrivateFieldGet(this,
|
|
196
|
+
__classPrivateFieldGet(this, _Messenger_events, "f").clear();
|
|
200
197
|
}
|
|
201
198
|
/**
|
|
202
|
-
* Get a restricted
|
|
199
|
+
* Get a restricted messenger
|
|
203
200
|
*
|
|
204
|
-
* Returns a wrapper around the
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
201
|
+
* Returns a wrapper around the messenger instance that restricts access to actions and events.
|
|
202
|
+
* The provided allowlists grant the ability to call the listed actions and subscribe to the
|
|
203
|
+
* listed events. The "name" provided grants ownership of any actions and events under that
|
|
204
|
+
* namespace. Ownership allows registering actions and publishing events, as well as
|
|
208
205
|
* unregistering actions and clearing event subscriptions.
|
|
209
206
|
*
|
|
210
207
|
* @param options - Controller messenger options.
|
|
211
208
|
* @param options.name - The name of the thing this messenger will be handed to (e.g. the
|
|
212
209
|
* controller name). This grants "ownership" of actions and events under this namespace to the
|
|
213
|
-
* restricted
|
|
214
|
-
* @param options.allowedActions - The list of actions that this restricted
|
|
215
|
-
*
|
|
216
|
-
* @param options.allowedEvents - The list of events that this restricted
|
|
217
|
-
*
|
|
218
|
-
* @template Namespace - The namespace for this messenger. Typically this is the name of the
|
|
210
|
+
* restricted messenger returned.
|
|
211
|
+
* @param options.allowedActions - The list of actions that this restricted messenger should be
|
|
212
|
+
* allowed to call.
|
|
213
|
+
* @param options.allowedEvents - The list of events that this restricted messenger should be
|
|
214
|
+
* allowed to subscribe to.
|
|
215
|
+
* @template Namespace - The namespace for this messenger. Typically this is the name of the
|
|
219
216
|
* module that this messenger has been created for. The authority to publish events and register
|
|
220
217
|
* actions under this namespace is granted to this restricted messenger instance.
|
|
221
218
|
* @template AllowedAction - A type union of the 'type' string for any allowed actions.
|
|
222
219
|
* This must not include internal actions that are in the messenger's namespace.
|
|
223
220
|
* @template AllowedEvent - A type union of the 'type' string for any allowed events.
|
|
224
221
|
* This must not include internal events that are in the messenger's namespace.
|
|
225
|
-
* @returns The restricted
|
|
222
|
+
* @returns The restricted messenger.
|
|
226
223
|
*/
|
|
227
224
|
getRestricted({ name, allowedActions, allowedEvents, }) {
|
|
228
|
-
return new
|
|
229
|
-
|
|
225
|
+
return new RestrictedMessenger({
|
|
226
|
+
messenger: this,
|
|
230
227
|
name,
|
|
231
228
|
allowedActions,
|
|
232
229
|
allowedEvents,
|
|
233
230
|
});
|
|
234
231
|
}
|
|
235
232
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
//# sourceMappingURL=
|
|
233
|
+
_Messenger_actions = new WeakMap(), _Messenger_events = new WeakMap(), _Messenger_initialEventPayloadGetters = new WeakMap(), _Messenger_eventPayloadCache = new WeakMap();
|
|
234
|
+
export { Messenger as ControllerMessenger };
|
|
235
|
+
//# sourceMappingURL=Messenger.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Messenger.mjs","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAmH5D;;;;;;;;;GASG;AACH,MAAM,OAAO,SAAS;IAAtB;QAIW,6BAAW,IAAI,GAAG,EAA2B,EAAC;QAE9C,4BAAU,IAAI,GAAG,EAA8C,EAAC;QAEzE;;;;WAIG;QACM,gDAA8B,IAAI,GAAG,EAG3C,EAAC;QAEJ;;WAEG;QACM,uCAAqB,IAAI,GAAG,EAGlC,EAAC;IAsTN,CAAC;IApTC;;;;;;;;;;OAUG;IACH,qBAAqB,CACnB,UAAsB,EACtB,OAA0C;QAE1C,IAAI,uBAAA,IAAI,0BAAS,CAAC,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,yBAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAEhD,IAAI,CAAC,WAAW,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;YAC7C,MAAM,IAAI,KAAK,CAAC,qCAAqC,SAAS,EAAE,CAAC,CAAC;SACnE;QAED,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC1C,IAAI,QAAQ,EAAE;YACZ,uBAAA,IAAI,oCAAmB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;SACzC;QAED,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;IAED;;;;;;;OAOG;IACH,uBAAuB,CACrB,SAAoB;QAEpB,uBAAA,IAAI,yBAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,kBAAkB;QAChB,uBAAA,IAAI,yBAAQ,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,aAAa,CAIX,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GAWd;QAQC,OAAO,IAAI,mBAAmB,CAAC;YAC7B,SAAS,EAAE,IAAI;YACf,IAAI;YACJ,cAAc;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF;;AAED,OAAO,EAAE,SAAS,IAAI,mBAAmB,EAAE,CAAC","sourcesContent":["import { RestrictedMessenger } from './RestrictedMessenger';\n\nexport type ActionHandler<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = (\n ...args: ExtractActionParameters<Action, ActionType>\n) => ExtractActionResponse<Action, ActionType>;\n\nexport type ExtractActionParameters<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = Action extends {\n type: ActionType;\n handler: (...args: infer HandlerArgs) => unknown;\n}\n ? HandlerArgs\n : never;\n\nexport type ExtractActionResponse<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = Action extends {\n type: ActionType;\n handler: (...args: infer _) => infer HandlerReturnValue;\n}\n ? HandlerReturnValue\n : never;\n\nexport type ExtractEventHandler<\n Event extends EventConstraint,\n EventType = Event['type'],\n> = Event extends {\n type: EventType;\n payload: infer Payload;\n}\n ? Payload extends unknown[]\n ? (...payload: Payload) => void\n : never\n : never;\n\nexport type ExtractEventPayload<\n Event extends EventConstraint,\n EventType = Event['type'],\n> = Event extends {\n type: EventType;\n payload: infer Payload;\n}\n ? Payload extends unknown[]\n ? Payload\n : never\n : never;\n\nexport type GenericEventHandler = (...args: unknown[]) => void;\n\nexport type SelectorFunction<\n Event extends EventConstraint,\n EventType extends Event['type'],\n ReturnValue,\n> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: string;\n handler: ((...args: never) => unknown) | ((...args: never[]) => unknown);\n};\nexport type EventConstraint = {\n type: string;\n payload: unknown[];\n};\n\ntype EventSubscriptionMap<\n Event extends EventConstraint,\n ReturnValue = unknown,\n> = Map<\n GenericEventHandler | SelectorEventHandler<ReturnValue>,\n SelectorFunction<Event, Event['type'], ReturnValue> | undefined\n>;\n\n/**\n * A namespaced string\n *\n * This type verifies that the string Name is prefixed by the string Name followed by a colon.\n *\n * @template Namespace - The namespace we're checking for.\n * @template Name - The full string, including the namespace.\n */\nexport type NamespacedBy<\n Namespace extends string,\n Name extends string,\n> = Name extends `${Namespace}:${string}` ? Name : never;\n\nexport type NotNamespacedBy<\n Namespace extends string,\n Name extends string,\n> = Name extends `${Namespace}:${string}` ? never : Name;\n\nexport type NamespacedName<Namespace extends string = string> =\n `${Namespace}:${string}`;\n\ntype NarrowToNamespace<Name, Namespace extends string> = Name extends {\n type: `${Namespace}:${string}`;\n}\n ? Name\n : never;\n\ntype NarrowToAllowed<Name, Allowed extends string> = Name extends {\n type: Allowed;\n}\n ? Name\n : never;\n\n/**\n * A message broker for \"actions\" and \"events\".\n *\n * The messenger allows registering functions as 'actions' that can be called elsewhere,\n * and it allows publishing and subscribing to events. Both actions and events are identified by\n * unique strings.\n *\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n */\nexport class Messenger<\n Action extends ActionConstraint,\n Event extends EventConstraint,\n> {\n readonly #actions = new Map<Action['type'], unknown>();\n\n readonly #events = new Map<Event['type'], EventSubscriptionMap<Event>>();\n\n /**\n * A map of functions for getting the initial event payload.\n *\n * Used only for events that represent state changes.\n */\n readonly #initialEventPayloadGetters = new Map<\n Event['type'],\n () => ExtractEventPayload<Event, Event['type']>\n >();\n\n /**\n * A cache of selector return values for their respective handlers.\n */\n readonly #eventPayloadCache = new Map<\n GenericEventHandler,\n unknown | undefined\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 * @param actionType - 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 when a handler has been registered for this action type already.\n * @template ActionType - A type union of Action type strings.\n */\n registerActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n handler: ActionHandler<Action, ActionType>,\n ) {\n if (this.#actions.has(actionType)) {\n throw new Error(\n `A handler for ${actionType} has already been registered`,\n );\n }\n this.#actions.set(actionType, handler);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @template ActionType - A type union of Action type strings.\n */\n unregisterActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#actions.delete(actionType);\n }\n\n /**\n * Unregister all action handlers.\n *\n * This prevents all actions from being called.\n */\n clearActions() {\n this.#actions.clear();\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 * @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 Action type strings.\n * @returns The action return value.\n */\n call<ActionType extends Action['type']>(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n const handler = this.#actions.get(actionType) as ActionHandler<\n Action,\n ActionType\n >;\n if (!handler) {\n throw new Error(`A handler for ${actionType} has not been registered`);\n }\n return handler(...params);\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 * @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<EventType extends Event['type']>({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n this.#initialEventPayloadGetters.set(eventType, getPayload);\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * Note that this method should never throw directly. Any errors from\n * subscribers are captured and re-thrown in a timeout handler.\n *\n * @param eventType - 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.\n */\n publish<EventType extends Event['type']>(\n eventType: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n const subscribers = this.#events.get(eventType);\n\n if (subscribers) {\n for (const [handler, selector] of subscribers.entries()) {\n try {\n if (selector) {\n const previousValue = this.#eventPayloadCache.get(handler);\n const newValue = selector(...payload);\n\n if (newValue !== previousValue) {\n this.#eventPayloadCache.set(handler, newValue);\n handler(newValue, previousValue);\n }\n } else {\n (handler as GenericEventHandler)(...payload);\n }\n } catch (error) {\n // Throw error after timeout so that it is capured as a console error\n // (and by Sentry) without interrupting the event publishing.\n setTimeout(() => {\n throw error;\n });\n }\n }\n }\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 * @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 Event['type']>(\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 * @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<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void {\n let subscribers = this.#events.get(eventType);\n if (!subscribers) {\n subscribers = new Map();\n this.#events.set(eventType, subscribers);\n }\n\n subscribers.set(handler, selector);\n\n if (selector) {\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n const initialValue = selector(...getPayload());\n this.#eventPayloadCache.set(handler, initialValue);\n }\n }\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * @param eventType - 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 Event type strings.\n */\n unsubscribe<EventType extends Event['type']>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ) {\n const subscribers = this.#events.get(eventType);\n\n if (!subscribers || !subscribers.has(handler)) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n\n const selector = subscribers.get(handler);\n if (selector) {\n this.#eventPayloadCache.delete(handler);\n }\n\n subscribers.delete(handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @template EventType - A type union of Event type strings.\n */\n clearEventSubscriptions<EventType extends Event['type']>(\n eventType: EventType,\n ) {\n this.#events.delete(eventType);\n }\n\n /**\n * Clear all subscriptions.\n *\n * This will remove all subscribed handlers for all events.\n */\n clearSubscriptions() {\n this.#events.clear();\n }\n\n /**\n * Get a restricted messenger\n *\n * Returns a wrapper around the messenger instance that restricts access to actions and events.\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 - Controller messenger options.\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 messenger returned.\n * @param options.allowedActions - The list of actions that this restricted messenger should be\n * allowed to call.\n * @param options.allowedEvents - The list of events that this restricted messenger should be\n * allowed to subscribe to.\n * @template Namespace - The namespace for this messenger. Typically this is the name of the\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 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 * @returns The restricted messenger.\n */\n getRestricted<\n Namespace extends string,\n AllowedAction extends NotNamespacedBy<Namespace, Action['type']> = never,\n AllowedEvent extends NotNamespacedBy<Namespace, Event['type']> = never,\n >({\n name,\n allowedActions,\n allowedEvents,\n }: {\n name: Namespace;\n allowedActions: NotNamespacedBy<\n Namespace,\n Extract<Action['type'], AllowedAction>\n >[];\n allowedEvents: NotNamespacedBy<\n Namespace,\n Extract<Event['type'], AllowedEvent>\n >[];\n }): RestrictedMessenger<\n Namespace,\n | NarrowToNamespace<Action, Namespace>\n | NarrowToAllowed<Action, AllowedAction>,\n NarrowToNamespace<Event, Namespace> | NarrowToAllowed<Event, AllowedEvent>,\n AllowedAction,\n AllowedEvent\n > {\n return new RestrictedMessenger({\n messenger: this,\n name,\n allowedActions,\n allowedEvents,\n });\n }\n}\n\nexport { Messenger as ControllerMessenger };\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
3
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
4
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
@@ -9,11 +10,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
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");
|
|
10
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
12
|
};
|
|
12
|
-
var
|
|
13
|
+
var _RestrictedMessenger_instances, _RestrictedMessenger_messenger, _RestrictedMessenger_namespace, _RestrictedMessenger_allowedActions, _RestrictedMessenger_allowedEvents, _RestrictedMessenger_isAllowedEvent, _RestrictedMessenger_isAllowedAction, _RestrictedMessenger_isInCurrentNamespace;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.RestrictedControllerMessenger = exports.RestrictedMessenger = void 0;
|
|
13
16
|
/**
|
|
14
|
-
* A restricted
|
|
17
|
+
* A restricted messenger.
|
|
15
18
|
*
|
|
16
|
-
* This acts as a wrapper around the
|
|
19
|
+
* This acts as a wrapper around the messenger instance that restricts access to actions
|
|
17
20
|
* and events.
|
|
18
21
|
*
|
|
19
22
|
* @template Namespace - The namespace for this messenger. Typically this is the name of the controller or
|
|
@@ -26,35 +29,44 @@ var _RestrictedControllerMessenger_instances, _RestrictedControllerMessenger_con
|
|
|
26
29
|
* @template AllowedEvent - A type union of the 'type' string for any allowed events.
|
|
27
30
|
* This must not include internal events that are in the messenger's namespace.
|
|
28
31
|
*/
|
|
29
|
-
|
|
32
|
+
class RestrictedMessenger {
|
|
30
33
|
/**
|
|
31
|
-
* Constructs a restricted
|
|
34
|
+
* Constructs a restricted messenger
|
|
32
35
|
*
|
|
33
36
|
* The provided allowlists grant the ability to call the listed actions and subscribe to the
|
|
34
37
|
* listed events. The "name" provided grants ownership of any actions and events under that
|
|
35
38
|
* namespace. Ownership allows registering actions and publishing events, as well as
|
|
36
39
|
* unregistering actions and clearing event subscriptions.
|
|
37
40
|
*
|
|
38
|
-
* @param options -
|
|
39
|
-
* @param options.controllerMessenger - The
|
|
41
|
+
* @param options - Options.
|
|
42
|
+
* @param options.controllerMessenger - The messenger instance that is being wrapped. (deprecated)
|
|
43
|
+
* @param options.messenger - The messenger instance that is being wrapped.
|
|
40
44
|
* @param options.name - The name of the thing this messenger will be handed to (e.g. the
|
|
41
45
|
* controller name). This grants "ownership" of actions and events under this namespace to the
|
|
42
|
-
* restricted
|
|
43
|
-
* @param options.allowedActions - The list of actions that this restricted
|
|
44
|
-
*
|
|
45
|
-
* @param options.allowedEvents - The list of events that this restricted
|
|
46
|
-
*
|
|
46
|
+
* restricted messenger returned.
|
|
47
|
+
* @param options.allowedActions - The list of actions that this restricted messenger should be
|
|
48
|
+
* allowed to call.
|
|
49
|
+
* @param options.allowedEvents - The list of events that this restricted messenger should be
|
|
50
|
+
* allowed to subscribe to.
|
|
47
51
|
*/
|
|
48
|
-
constructor({ controllerMessenger, name, allowedActions, allowedEvents, }) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
constructor({ controllerMessenger, messenger, name, allowedActions, allowedEvents, }) {
|
|
53
|
+
_RestrictedMessenger_instances.add(this);
|
|
54
|
+
_RestrictedMessenger_messenger.set(this, void 0);
|
|
55
|
+
_RestrictedMessenger_namespace.set(this, void 0);
|
|
56
|
+
_RestrictedMessenger_allowedActions.set(this, void 0);
|
|
57
|
+
_RestrictedMessenger_allowedEvents.set(this, void 0);
|
|
58
|
+
if (messenger && controllerMessenger) {
|
|
59
|
+
throw new Error(`Both messenger properties provided. Provide message using only 'messenger' option, 'controllerMessenger' is deprecated`);
|
|
60
|
+
}
|
|
61
|
+
else if (!messenger && !controllerMessenger) {
|
|
62
|
+
throw new Error('Messenger not provided');
|
|
63
|
+
}
|
|
64
|
+
// The above condition guarantees that one of these options is defined.
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
66
|
+
__classPrivateFieldSet(this, _RestrictedMessenger_messenger, (messenger ?? controllerMessenger), "f");
|
|
67
|
+
__classPrivateFieldSet(this, _RestrictedMessenger_namespace, name, "f");
|
|
68
|
+
__classPrivateFieldSet(this, _RestrictedMessenger_allowedActions, allowedActions, "f");
|
|
69
|
+
__classPrivateFieldSet(this, _RestrictedMessenger_allowedEvents, allowedEvents, "f");
|
|
58
70
|
}
|
|
59
71
|
/**
|
|
60
72
|
* Register an action handler.
|
|
@@ -71,10 +83,10 @@ export class RestrictedControllerMessenger {
|
|
|
71
83
|
*/
|
|
72
84
|
registerActionHandler(action, handler) {
|
|
73
85
|
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
74
|
-
if (!__classPrivateFieldGet(this,
|
|
75
|
-
throw new Error(`Only allowed registering action handlers prefixed by '${__classPrivateFieldGet(this,
|
|
86
|
+
if (!__classPrivateFieldGet(this, _RestrictedMessenger_instances, "m", _RestrictedMessenger_isInCurrentNamespace).call(this, action)) {
|
|
87
|
+
throw new Error(`Only allowed registering action handlers prefixed by '${__classPrivateFieldGet(this, _RestrictedMessenger_namespace, "f")}:'`);
|
|
76
88
|
}
|
|
77
|
-
__classPrivateFieldGet(this,
|
|
89
|
+
__classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").registerActionHandler(action, handler);
|
|
78
90
|
}
|
|
79
91
|
/**
|
|
80
92
|
* Unregister an action handler.
|
|
@@ -89,10 +101,10 @@ export class RestrictedControllerMessenger {
|
|
|
89
101
|
*/
|
|
90
102
|
unregisterActionHandler(action) {
|
|
91
103
|
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
92
|
-
if (!__classPrivateFieldGet(this,
|
|
93
|
-
throw new Error(`Only allowed unregistering action handlers prefixed by '${__classPrivateFieldGet(this,
|
|
104
|
+
if (!__classPrivateFieldGet(this, _RestrictedMessenger_instances, "m", _RestrictedMessenger_isInCurrentNamespace).call(this, action)) {
|
|
105
|
+
throw new Error(`Only allowed unregistering action handlers prefixed by '${__classPrivateFieldGet(this, _RestrictedMessenger_namespace, "f")}:'`);
|
|
94
106
|
}
|
|
95
|
-
__classPrivateFieldGet(this,
|
|
107
|
+
__classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").unregisterActionHandler(action);
|
|
96
108
|
}
|
|
97
109
|
/**
|
|
98
110
|
* Call an action.
|
|
@@ -110,10 +122,10 @@ export class RestrictedControllerMessenger {
|
|
|
110
122
|
* @returns The action return value.
|
|
111
123
|
*/
|
|
112
124
|
call(actionType, ...params) {
|
|
113
|
-
if (!__classPrivateFieldGet(this,
|
|
125
|
+
if (!__classPrivateFieldGet(this, _RestrictedMessenger_instances, "m", _RestrictedMessenger_isAllowedAction).call(this, actionType)) {
|
|
114
126
|
throw new Error(`Action missing from allow list: ${actionType}`);
|
|
115
127
|
}
|
|
116
|
-
const response = __classPrivateFieldGet(this,
|
|
128
|
+
const response = __classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").call(actionType, ...params);
|
|
117
129
|
return response;
|
|
118
130
|
}
|
|
119
131
|
/**
|
|
@@ -131,10 +143,10 @@ export class RestrictedControllerMessenger {
|
|
|
131
143
|
*/
|
|
132
144
|
registerInitialEventPayload({ eventType, getPayload, }) {
|
|
133
145
|
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
134
|
-
if (!__classPrivateFieldGet(this,
|
|
135
|
-
throw new Error(`Only allowed publishing events prefixed by '${__classPrivateFieldGet(this,
|
|
146
|
+
if (!__classPrivateFieldGet(this, _RestrictedMessenger_instances, "m", _RestrictedMessenger_isInCurrentNamespace).call(this, eventType)) {
|
|
147
|
+
throw new Error(`Only allowed publishing events prefixed by '${__classPrivateFieldGet(this, _RestrictedMessenger_namespace, "f")}:'`);
|
|
136
148
|
}
|
|
137
|
-
__classPrivateFieldGet(this,
|
|
149
|
+
__classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").registerInitialEventPayload({
|
|
138
150
|
eventType,
|
|
139
151
|
getPayload,
|
|
140
152
|
});
|
|
@@ -154,19 +166,19 @@ export class RestrictedControllerMessenger {
|
|
|
154
166
|
*/
|
|
155
167
|
publish(event, ...payload) {
|
|
156
168
|
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
157
|
-
if (!__classPrivateFieldGet(this,
|
|
158
|
-
throw new Error(`Only allowed publishing events prefixed by '${__classPrivateFieldGet(this,
|
|
169
|
+
if (!__classPrivateFieldGet(this, _RestrictedMessenger_instances, "m", _RestrictedMessenger_isInCurrentNamespace).call(this, event)) {
|
|
170
|
+
throw new Error(`Only allowed publishing events prefixed by '${__classPrivateFieldGet(this, _RestrictedMessenger_namespace, "f")}:'`);
|
|
159
171
|
}
|
|
160
|
-
__classPrivateFieldGet(this,
|
|
172
|
+
__classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").publish(event, ...payload);
|
|
161
173
|
}
|
|
162
174
|
subscribe(event, handler, selector) {
|
|
163
|
-
if (!__classPrivateFieldGet(this,
|
|
175
|
+
if (!__classPrivateFieldGet(this, _RestrictedMessenger_instances, "m", _RestrictedMessenger_isAllowedEvent).call(this, event)) {
|
|
164
176
|
throw new Error(`Event missing from allow list: ${event}`);
|
|
165
177
|
}
|
|
166
178
|
if (selector) {
|
|
167
|
-
return __classPrivateFieldGet(this,
|
|
179
|
+
return __classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").subscribe(event, handler, selector);
|
|
168
180
|
}
|
|
169
|
-
return __classPrivateFieldGet(this,
|
|
181
|
+
return __classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").subscribe(event, handler);
|
|
170
182
|
}
|
|
171
183
|
/**
|
|
172
184
|
* Unsubscribe from an event.
|
|
@@ -177,14 +189,14 @@ export class RestrictedControllerMessenger {
|
|
|
177
189
|
*
|
|
178
190
|
* @param event - The event type. This is a unique identifier for this event.
|
|
179
191
|
* @param handler - The event handler to unregister.
|
|
180
|
-
* @throws Will throw if the given event is not an allowed event for this
|
|
192
|
+
* @throws Will throw if the given event is not an allowed event for this messenger.
|
|
181
193
|
* @template EventType - A type union of allowed Event type strings.
|
|
182
194
|
*/
|
|
183
195
|
unsubscribe(event, handler) {
|
|
184
|
-
if (!__classPrivateFieldGet(this,
|
|
196
|
+
if (!__classPrivateFieldGet(this, _RestrictedMessenger_instances, "m", _RestrictedMessenger_isAllowedEvent).call(this, event)) {
|
|
185
197
|
throw new Error(`Event missing from allow list: ${event}`);
|
|
186
198
|
}
|
|
187
|
-
__classPrivateFieldGet(this,
|
|
199
|
+
__classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").unsubscribe(event, handler);
|
|
188
200
|
}
|
|
189
201
|
/**
|
|
190
202
|
* Clear subscriptions for a specific event.
|
|
@@ -198,23 +210,25 @@ export class RestrictedControllerMessenger {
|
|
|
198
210
|
* @template EventType - A type union of Event type strings that are namespaced by Namespace.
|
|
199
211
|
*/
|
|
200
212
|
clearEventSubscriptions(event) {
|
|
201
|
-
if (!__classPrivateFieldGet(this,
|
|
202
|
-
throw new Error(`Only allowed clearing events prefixed by '${__classPrivateFieldGet(this,
|
|
213
|
+
if (!__classPrivateFieldGet(this, _RestrictedMessenger_instances, "m", _RestrictedMessenger_isInCurrentNamespace).call(this, event)) {
|
|
214
|
+
throw new Error(`Only allowed clearing events prefixed by '${__classPrivateFieldGet(this, _RestrictedMessenger_namespace, "f")}:'`);
|
|
203
215
|
}
|
|
204
|
-
__classPrivateFieldGet(this,
|
|
216
|
+
__classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").clearEventSubscriptions(event);
|
|
205
217
|
}
|
|
206
218
|
}
|
|
207
|
-
|
|
219
|
+
exports.RestrictedMessenger = RestrictedMessenger;
|
|
220
|
+
exports.RestrictedControllerMessenger = RestrictedMessenger;
|
|
221
|
+
_RestrictedMessenger_messenger = new WeakMap(), _RestrictedMessenger_namespace = new WeakMap(), _RestrictedMessenger_allowedActions = new WeakMap(), _RestrictedMessenger_allowedEvents = new WeakMap(), _RestrictedMessenger_instances = new WeakSet(), _RestrictedMessenger_isAllowedEvent = function _RestrictedMessenger_isAllowedEvent(eventType) {
|
|
208
222
|
// Safely upcast to allow runtime check
|
|
209
|
-
const allowedEvents = __classPrivateFieldGet(this,
|
|
210
|
-
return (__classPrivateFieldGet(this,
|
|
223
|
+
const allowedEvents = __classPrivateFieldGet(this, _RestrictedMessenger_allowedEvents, "f");
|
|
224
|
+
return (__classPrivateFieldGet(this, _RestrictedMessenger_instances, "m", _RestrictedMessenger_isInCurrentNamespace).call(this, eventType) ||
|
|
211
225
|
(allowedEvents !== null && allowedEvents.includes(eventType)));
|
|
212
|
-
},
|
|
226
|
+
}, _RestrictedMessenger_isAllowedAction = function _RestrictedMessenger_isAllowedAction(actionType) {
|
|
213
227
|
// Safely upcast to allow runtime check
|
|
214
|
-
const allowedActions = __classPrivateFieldGet(this,
|
|
215
|
-
return (__classPrivateFieldGet(this,
|
|
228
|
+
const allowedActions = __classPrivateFieldGet(this, _RestrictedMessenger_allowedActions, "f");
|
|
229
|
+
return (__classPrivateFieldGet(this, _RestrictedMessenger_instances, "m", _RestrictedMessenger_isInCurrentNamespace).call(this, actionType) ||
|
|
216
230
|
(allowedActions !== null && allowedActions.includes(actionType)));
|
|
217
|
-
},
|
|
218
|
-
return name.startsWith(`${__classPrivateFieldGet(this,
|
|
231
|
+
}, _RestrictedMessenger_isInCurrentNamespace = function _RestrictedMessenger_isInCurrentNamespace(name) {
|
|
232
|
+
return name.startsWith(`${__classPrivateFieldGet(this, _RestrictedMessenger_namespace, "f")}:`);
|
|
219
233
|
};
|
|
220
|
-
//# sourceMappingURL=
|
|
234
|
+
//# sourceMappingURL=RestrictedMessenger.cjs.map
|