@metamask-previews/base-controller 3.2.3-preview.ce06456 → 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.
@@ -1,180 +1,13 @@
1
1
  "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var _ControllerMessenger_actions, _ControllerMessenger_events, _ControllerMessenger_eventPayloadCache;
2
8
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ControllerMessenger = exports.RestrictedControllerMessenger = void 0;
4
- /**
5
- * A restricted controller messenger.
6
- *
7
- * This acts as a wrapper around the controller messenger instance that restricts access to actions
8
- * and events.
9
- *
10
- * @template N - The namespace for this messenger. Typically this is the name of the controller or
11
- * module that this messenger has been created for. The authority to publish events and register
12
- * actions under this namespace is granted to this restricted messenger instance.
13
- * @template Action - A type union of all Action types.
14
- * @template Event - A type union of all Event types.
15
- * @template AllowedAction - A type union of the 'type' string for any allowed actions.
16
- * @template AllowedEvent - A type union of the 'type' string for any allowed events.
17
- */
18
- class RestrictedControllerMessenger {
19
- /**
20
- * Constructs a restricted controller messenger
21
- *
22
- * The provided allowlists grant the ability to call the listed actions and subscribe to the
23
- * listed events. The "name" provided grants ownership of any actions and events under that
24
- * namespace. Ownership allows registering actions and publishing events, as well as
25
- * unregistering actions and clearing event subscriptions.
26
- *
27
- * @param options - The controller options.
28
- * @param options.controllerMessenger - The controller messenger instance that is being wrapped.
29
- * @param options.name - The name of the thing this messenger will be handed to (e.g. the
30
- * controller name). This grants "ownership" of actions and events under this namespace to the
31
- * restricted controller messenger returned.
32
- * @param options.allowedActions - The list of actions that this restricted controller messenger
33
- * should be alowed to call.
34
- * @param options.allowedEvents - The list of events that this restricted controller messenger
35
- * should be allowed to subscribe to.
36
- */
37
- constructor({ controllerMessenger, name, allowedActions, allowedEvents, }) {
38
- this.controllerMessenger = controllerMessenger;
39
- this.controllerName = name;
40
- this.allowedActions = allowedActions || null;
41
- this.allowedEvents = allowedEvents || null;
42
- }
43
- /**
44
- * Register an action handler.
45
- *
46
- * This will make the registered function available to call via the `call` method.
47
- *
48
- * The action type this handler is registered under *must* be in the current namespace.
49
- *
50
- * @param action - The action type. This is a unqiue identifier for this action.
51
- * @param handler - The action handler. This function gets called when the `call` method is
52
- * invoked with the given action type.
53
- * @throws Will throw when a handler has been registered for this action type already.
54
- * @template T - A type union of Action type strings that are namespaced by N.
55
- */
56
- registerActionHandler(action, handler) {
57
- /* istanbul ignore if */ // Branch unreachable with valid types
58
- if (!action.startsWith(`${this.controllerName}:`)) {
59
- throw new Error(`Only allowed registering action handlers prefixed by '${this.controllerName}:'`);
60
- }
61
- this.controllerMessenger.registerActionHandler(action, handler);
62
- }
63
- /**
64
- * Unregister an action handler.
65
- *
66
- * This will prevent this action from being called.
67
- *
68
- * The action type being unregistered *must* be in the current namespace.
69
- *
70
- * @param action - The action type. This is a unqiue identifier for this action.
71
- * @template T - A type union of Action type strings that are namespaced by N.
72
- */
73
- unregisterActionHandler(action) {
74
- /* istanbul ignore if */ // Branch unreachable with valid types
75
- if (!action.startsWith(`${this.controllerName}:`)) {
76
- throw new Error(`Only allowed unregistering action handlers prefixed by '${this.controllerName}:'`);
77
- }
78
- this.controllerMessenger.unregisterActionHandler(action);
79
- }
80
- /**
81
- * Call an action.
82
- *
83
- * This function will call the action handler corresponding to the given action type, passing
84
- * along any parameters given.
85
- *
86
- * The action type being called must be on the action allowlist.
87
- *
88
- * @param action - The action type. This is a unqiue identifier for this action.
89
- * @param params - The action parameters. These must match the type of the parameters of the
90
- * registered action handler.
91
- * @throws Will throw when no handler has been registered for the given type.
92
- * @template T - A type union of allowed Action type strings.
93
- * @returns The action return value.
94
- */
95
- call(action, ...params) {
96
- /* istanbul ignore next */ // Branches unreachable with valid types
97
- if (this.allowedActions === null) {
98
- throw new Error('No actions allowed');
99
- }
100
- else if (!this.allowedActions.includes(action)) {
101
- throw new Error(`Action missing from allow list: ${action}`);
102
- }
103
- return this.controllerMessenger.call(action, ...params);
104
- }
105
- /**
106
- * Publish an event.
107
- *
108
- * Publishes the given payload to all subscribers of the given event type.
109
- *
110
- * The event type being published *must* be in the current namespace.
111
- *
112
- * @param event - The event type. This is a unique identifier for this event.
113
- * @param payload - The event payload. The type of the parameters for each event handler must
114
- * match the type of this payload.
115
- * @template E - A type union of Event type strings that are namespaced by N.
116
- */
117
- publish(event, ...payload) {
118
- /* istanbul ignore if */ // Branch unreachable with valid types
119
- if (!event.startsWith(`${this.controllerName}:`)) {
120
- throw new Error(`Only allowed publishing events prefixed by '${this.controllerName}:'`);
121
- }
122
- this.controllerMessenger.publish(event, ...payload);
123
- }
124
- subscribe(event, handler, selector) {
125
- /* istanbul ignore next */ // Branches unreachable with valid types
126
- if (this.allowedEvents === null) {
127
- throw new Error('No events allowed');
128
- }
129
- else if (!this.allowedEvents.includes(event)) {
130
- throw new Error(`Event missing from allow list: ${event}`);
131
- }
132
- if (selector) {
133
- return this.controllerMessenger.subscribe(event, handler, selector);
134
- }
135
- return this.controllerMessenger.subscribe(event, handler);
136
- }
137
- /**
138
- * Unsubscribe from an event.
139
- *
140
- * Unregisters the given function as an event handler for the given event.
141
- *
142
- * The event type being unsubscribed to must be on the event allowlist.
143
- *
144
- * @param event - The event type. This is a unique identifier for this event.
145
- * @param handler - The event handler to unregister.
146
- * @throws Will throw when the given event handler is not registered for this event.
147
- * @template T - A type union of allowed Event type strings.
148
- */
149
- unsubscribe(event, handler) {
150
- /* istanbul ignore next */ // Branches unreachable with valid types
151
- if (this.allowedEvents === null) {
152
- throw new Error('No events allowed');
153
- }
154
- else if (!this.allowedEvents.includes(event)) {
155
- throw new Error(`Event missing from allow list: ${event}`);
156
- }
157
- this.controllerMessenger.unsubscribe(event, handler);
158
- }
159
- /**
160
- * Clear subscriptions for a specific event.
161
- *
162
- * This will remove all subscribed handlers for this event.
163
- *
164
- * The event type being cleared *must* be in the current namespace.
165
- *
166
- * @param event - The event type. This is a unique identifier for this event.
167
- * @template E - A type union of Event type strings that are namespaced by N.
168
- */
169
- clearEventSubscriptions(event) {
170
- /* istanbul ignore if */ // Branch unreachable with valid types
171
- if (!event.startsWith(`${this.controllerName}:`)) {
172
- throw new Error(`Only allowed clearing events prefixed by '${this.controllerName}:'`);
173
- }
174
- this.controllerMessenger.clearEventSubscriptions(event);
175
- }
176
- }
177
- exports.RestrictedControllerMessenger = RestrictedControllerMessenger;
9
+ exports.ControllerMessenger = void 0;
10
+ const RestrictedControllerMessenger_1 = require("./RestrictedControllerMessenger");
178
11
  /**
179
12
  * A messaging system for controllers.
180
13
  *
@@ -187,12 +20,12 @@ exports.RestrictedControllerMessenger = RestrictedControllerMessenger;
187
20
  */
188
21
  class ControllerMessenger {
189
22
  constructor() {
190
- this.actions = new Map();
191
- this.events = new Map();
23
+ _ControllerMessenger_actions.set(this, new Map());
24
+ _ControllerMessenger_events.set(this, new Map());
192
25
  /**
193
26
  * A cache of selector return values for their respective handlers.
194
27
  */
195
- this.eventPayloadCache = new Map();
28
+ _ControllerMessenger_eventPayloadCache.set(this, new Map());
196
29
  }
197
30
  /**
198
31
  * Register an action handler.
@@ -203,13 +36,13 @@ class ControllerMessenger {
203
36
  * @param handler - The action handler. This function gets called when the `call` method is
204
37
  * invoked with the given action type.
205
38
  * @throws Will throw when a handler has been registered for this action type already.
206
- * @template T - A type union of Action type strings.
39
+ * @template ActionType - A type union of Action type strings.
207
40
  */
208
41
  registerActionHandler(actionType, handler) {
209
- if (this.actions.has(actionType)) {
42
+ if (__classPrivateFieldGet(this, _ControllerMessenger_actions, "f").has(actionType)) {
210
43
  throw new Error(`A handler for ${actionType} has already been registered`);
211
44
  }
212
- this.actions.set(actionType, handler);
45
+ __classPrivateFieldGet(this, _ControllerMessenger_actions, "f").set(actionType, handler);
213
46
  }
214
47
  /**
215
48
  * Unregister an action handler.
@@ -217,10 +50,10 @@ class ControllerMessenger {
217
50
  * This will prevent this action from being called.
218
51
  *
219
52
  * @param actionType - The action type. This is a unqiue identifier for this action.
220
- * @template T - A type union of Action type strings.
53
+ * @template ActionType - A type union of Action type strings.
221
54
  */
222
55
  unregisterActionHandler(actionType) {
223
- this.actions.delete(actionType);
56
+ __classPrivateFieldGet(this, _ControllerMessenger_actions, "f").delete(actionType);
224
57
  }
225
58
  /**
226
59
  * Unregister all action handlers.
@@ -228,7 +61,7 @@ class ControllerMessenger {
228
61
  * This prevents all actions from being called.
229
62
  */
230
63
  clearActions() {
231
- this.actions.clear();
64
+ __classPrivateFieldGet(this, _ControllerMessenger_actions, "f").clear();
232
65
  }
233
66
  /**
234
67
  * Call an action.
@@ -240,11 +73,11 @@ class ControllerMessenger {
240
73
  * @param params - The action parameters. These must match the type of the parameters of the
241
74
  * registered action handler.
242
75
  * @throws Will throw when no handler has been registered for the given type.
243
- * @template T - A type union of Action type strings.
76
+ * @template ActionType - A type union of Action type strings.
244
77
  * @returns The action return value.
245
78
  */
246
79
  call(actionType, ...params) {
247
- const handler = this.actions.get(actionType);
80
+ const handler = __classPrivateFieldGet(this, _ControllerMessenger_actions, "f").get(actionType);
248
81
  if (!handler) {
249
82
  throw new Error(`A handler for ${actionType} has not been registered`);
250
83
  }
@@ -261,18 +94,18 @@ class ControllerMessenger {
261
94
  * @param eventType - The event type. This is a unique identifier for this event.
262
95
  * @param payload - The event payload. The type of the parameters for each event handler must
263
96
  * match the type of this payload.
264
- * @template E - A type union of Event type strings.
97
+ * @template EventType - A type union of Event type strings.
265
98
  */
266
99
  publish(eventType, ...payload) {
267
- const subscribers = this.events.get(eventType);
100
+ const subscribers = __classPrivateFieldGet(this, _ControllerMessenger_events, "f").get(eventType);
268
101
  if (subscribers) {
269
102
  for (const [handler, selector] of subscribers.entries()) {
270
103
  try {
271
104
  if (selector) {
272
- const previousValue = this.eventPayloadCache.get(handler);
105
+ const previousValue = __classPrivateFieldGet(this, _ControllerMessenger_eventPayloadCache, "f").get(handler);
273
106
  const newValue = selector(...payload);
274
107
  if (newValue !== previousValue) {
275
- this.eventPayloadCache.set(handler, newValue);
108
+ __classPrivateFieldGet(this, _ControllerMessenger_eventPayloadCache, "f").set(handler, newValue);
276
109
  handler(newValue, previousValue);
277
110
  }
278
111
  }
@@ -291,10 +124,10 @@ class ControllerMessenger {
291
124
  }
292
125
  }
293
126
  subscribe(eventType, handler, selector) {
294
- let subscribers = this.events.get(eventType);
127
+ let subscribers = __classPrivateFieldGet(this, _ControllerMessenger_events, "f").get(eventType);
295
128
  if (!subscribers) {
296
129
  subscribers = new Map();
297
- this.events.set(eventType, subscribers);
130
+ __classPrivateFieldGet(this, _ControllerMessenger_events, "f").set(eventType, subscribers);
298
131
  }
299
132
  subscribers.set(handler, selector);
300
133
  }
@@ -306,16 +139,16 @@ class ControllerMessenger {
306
139
  * @param eventType - The event type. This is a unique identifier for this event.
307
140
  * @param handler - The event handler to unregister.
308
141
  * @throws Will throw when the given event handler is not registered for this event.
309
- * @template E - A type union of Event type strings.
142
+ * @template EventType - A type union of Event type strings.
310
143
  */
311
144
  unsubscribe(eventType, handler) {
312
- const subscribers = this.events.get(eventType);
145
+ const subscribers = __classPrivateFieldGet(this, _ControllerMessenger_events, "f").get(eventType);
313
146
  if (!subscribers || !subscribers.has(handler)) {
314
147
  throw new Error(`Subscription not found for event: ${eventType}`);
315
148
  }
316
149
  const selector = subscribers.get(handler);
317
150
  if (selector) {
318
- this.eventPayloadCache.delete(handler);
151
+ __classPrivateFieldGet(this, _ControllerMessenger_eventPayloadCache, "f").delete(handler);
319
152
  }
320
153
  subscribers.delete(handler);
321
154
  }
@@ -325,10 +158,10 @@ class ControllerMessenger {
325
158
  * This will remove all subscribed handlers for this event.
326
159
  *
327
160
  * @param eventType - The event type. This is a unique identifier for this event.
328
- * @template E - A type union of Event type strings.
161
+ * @template EventType - A type union of Event type strings.
329
162
  */
330
163
  clearEventSubscriptions(eventType) {
331
- this.events.delete(eventType);
164
+ __classPrivateFieldGet(this, _ControllerMessenger_events, "f").delete(eventType);
332
165
  }
333
166
  /**
334
167
  * Clear all subscriptions.
@@ -336,7 +169,7 @@ class ControllerMessenger {
336
169
  * This will remove all subscribed handlers for all events.
337
170
  */
338
171
  clearSubscriptions() {
339
- this.events.clear();
172
+ __classPrivateFieldGet(this, _ControllerMessenger_events, "f").clear();
340
173
  }
341
174
  /**
342
175
  * Get a restricted controller messenger
@@ -355,7 +188,7 @@ class ControllerMessenger {
355
188
  * should be alowed to call.
356
189
  * @param options.allowedEvents - The list of events that this restricted controller messenger
357
190
  * should be allowed to subscribe to.
358
- * @template N - The namespace for this messenger. Typically this is the name of the controller or
191
+ * @template Namespace - The namespace for this messenger. Typically this is the name of the controller or
359
192
  * module that this messenger has been created for. The authority to publish events and register
360
193
  * actions under this namespace is granted to this restricted messenger instance.
361
194
  * @template AllowedAction - A type union of the 'type' string for any allowed actions.
@@ -363,7 +196,7 @@ class ControllerMessenger {
363
196
  * @returns The restricted controller messenger.
364
197
  */
365
198
  getRestricted({ name, allowedActions, allowedEvents, }) {
366
- return new RestrictedControllerMessenger({
199
+ return new RestrictedControllerMessenger_1.RestrictedControllerMessenger({
367
200
  controllerMessenger: this,
368
201
  name,
369
202
  allowedActions,
@@ -372,4 +205,5 @@ class ControllerMessenger {
372
205
  }
373
206
  }
374
207
  exports.ControllerMessenger = ControllerMessenger;
208
+ _ControllerMessenger_actions = new WeakMap(), _ControllerMessenger_events = new WeakMap(), _ControllerMessenger_eventPayloadCache = new WeakMap();
375
209
  //# sourceMappingURL=ControllerMessenger.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ControllerMessenger.js","sourceRoot":"","sources":["../src/ControllerMessenger.ts"],"names":[],"mappings":";;;AA4EA;;;;;;;;;;;;;GAaG;AACH,MAAa,6BA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Ac;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF;AA7RD,kDA6RC","sourcesContent":["export type ActionHandler<Action, ActionType> = (\n ...args: ExtractActionParameters<Action, ActionType>\n) => ExtractActionResponse<Action, ActionType>;\nexport type ExtractActionParameters<Action, T> = Action extends {\n type: T;\n handler: (...args: infer H) => any;\n}\n ? H\n : never;\nexport type ExtractActionResponse<Action, T> = Action extends {\n type: T;\n handler: (...args: any) => infer H;\n}\n ? H\n : never;\n\nexport type ExtractEventHandler<Event, T> = Event extends {\n type: T;\n payload: infer P;\n}\n ? P extends unknown[]\n ? (...payload: P) => void\n : never\n : never;\nexport type ExtractEventPayload<Event, T> = Event extends {\n type: T;\n payload: infer P;\n}\n ? P\n : never;\n\nexport type GenericEventHandler = (...args: unknown[]) => void;\n\nexport type SelectorFunction<Args extends unknown[], ReturnValue> = (\n ...args: Args\n) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: string;\n handler: (...args: any) => unknown;\n};\nexport type EventConstraint = { type: string; payload: unknown[] };\n\ntype EventSubscriptionMap = Map<\n GenericEventHandler | SelectorEventHandler<unknown>,\n SelectorFunction<any, unknown> | undefined\n>;\n\n/**\n * A namespaced string\n *\n * This type verifies that the string T is prefixed by the string Name followed by a colon.\n *\n * @template Name - The namespace we're checking for.\n * @template T - The full string, including the namespace.\n */\nexport type Namespaced<Name extends string, T> = T extends `${Name}:${string}`\n ? T\n : never;\n\ntype NarrowToNamespace<T, Namespace extends string> = T extends {\n type: `${Namespace}:${string}`;\n}\n ? T\n : never;\n\ntype NarrowToAllowed<T, Allowed extends string> = T extends {\n type: Allowed;\n}\n ? T\n : never;\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 N - 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 N extends string,\n Action extends ActionConstraint,\n Event extends EventConstraint,\n AllowedAction extends string,\n AllowedEvent extends string,\n> {\n private readonly controllerMessenger: ControllerMessenger<\n ActionConstraint,\n EventConstraint\n >;\n\n private readonly controllerName: N;\n\n private readonly allowedActions: AllowedAction[] | null;\n\n private 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: N;\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 when a handler has been registered for this action type already.\n * @template T - A type union of Action type strings that are namespaced by N.\n */\n registerActionHandler<T extends Namespaced<N, Action['type']>>(\n action: T,\n handler: ActionHandler<Action, T>,\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 '${this.controllerName}:'`,\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 T - A type union of Action type strings that are namespaced by N.\n */\n unregisterActionHandler<T extends Namespaced<N, Action['type']>>(action: T) {\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 '${this.controllerName}:'`,\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 T - A type union of allowed Action type strings.\n * @returns The action return value.\n */\n call<T extends AllowedAction & string>(\n action: T,\n ...params: ExtractActionParameters<Action, T>\n ): ExtractActionResponse<Action, T> {\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 E - A type union of Event type strings that are namespaced by N.\n */\n publish<E extends Namespaced<N, Event['type']>>(\n event: E,\n ...payload: ExtractEventPayload<Event, E>\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 E - A type union of Event type strings.\n */\n subscribe<E extends AllowedEvent & string>(\n eventType: E,\n handler: ExtractEventHandler<Event, E>,\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 E - A type union of Event type strings.\n * @template V - The selector return value.\n */\n subscribe<E extends AllowedEvent & string, V>(\n eventType: E,\n handler: SelectorEventHandler<V>,\n selector: SelectorFunction<ExtractEventPayload<Event, E>, V>,\n ): void;\n\n subscribe<E extends AllowedEvent & string, V>(\n event: E,\n handler: ExtractEventHandler<Event, E>,\n selector?: SelectorFunction<ExtractEventPayload<Event, E>, V>,\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 T - A type union of allowed Event type strings.\n */\n unsubscribe<E extends AllowedEvent & string>(\n event: E,\n handler: ExtractEventHandler<Event, E>,\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 E - A type union of Event type strings that are namespaced by N.\n */\n clearEventSubscriptions<E extends Namespaced<N, Event['type']>>(event: E) {\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\n/**\n * A messaging system for controllers.\n *\n * The controller 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 ControllerMessenger<\n Action extends ActionConstraint,\n Event extends EventConstraint,\n> {\n private readonly actions = new Map<Action['type'], unknown>();\n\n private readonly events = new Map<Event['type'], EventSubscriptionMap>();\n\n /**\n * A cache of selector return values for their respective handlers.\n */\n private 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 T - A type union of Action type strings.\n */\n registerActionHandler<T extends Action['type']>(\n actionType: T,\n handler: ActionHandler<Action, T>,\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 T - A type union of Action type strings.\n */\n unregisterActionHandler<T extends Action['type']>(actionType: T) {\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 T - A type union of Action type strings.\n * @returns The action return value.\n */\n call<T extends Action['type']>(\n actionType: T,\n ...params: ExtractActionParameters<Action, T>\n ): ExtractActionResponse<Action, T> {\n const handler = this.actions.get(actionType) as ActionHandler<Action, T>;\n if (!handler) {\n throw new Error(`A handler for ${actionType} has not been registered`);\n }\n return handler(...params);\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 E - A type union of Event type strings.\n */\n publish<E extends Event['type']>(\n eventType: E,\n ...payload: ExtractEventPayload<Event, E>\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 E - A type union of Event type strings.\n */\n subscribe<E extends Event['type']>(\n eventType: E,\n handler: ExtractEventHandler<Event, E>,\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 E - A type union of Event type strings.\n * @template V - The selector return value.\n */\n subscribe<E extends Event['type'], V>(\n eventType: E,\n handler: SelectorEventHandler<V>,\n selector: SelectorFunction<ExtractEventPayload<Event, E>, V>,\n ): void;\n\n subscribe<E extends Event['type'], V>(\n eventType: E,\n handler: ExtractEventHandler<Event, E>,\n selector?: SelectorFunction<ExtractEventPayload<Event, E>, V>,\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\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 E - A type union of Event type strings.\n */\n unsubscribe<E extends Event['type']>(\n eventType: E,\n handler: ExtractEventHandler<Event, E>,\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 E - A type union of Event type strings.\n */\n clearEventSubscriptions<E extends Event['type']>(eventType: E) {\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 controller messenger\n *\n * Returns a wrapper around the controller messenger instance that restricts access to actions\n * and events. The provided allowlists grant the ability to call the listed actions and subscribe\n * to the listed events. The \"name\" provided grants ownership of any actions and events under\n * that 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 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 * @template N - 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 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 * @returns The restricted controller messenger.\n */\n getRestricted<\n N extends string,\n AllowedAction extends string,\n AllowedEvent extends string,\n >({\n name,\n allowedActions,\n allowedEvents,\n }: {\n name: N;\n allowedActions?: Extract<Action['type'], AllowedAction>[];\n allowedEvents?: Extract<Event['type'], AllowedEvent>[];\n }): RestrictedControllerMessenger<\n N,\n NarrowToNamespace<Action, N> | NarrowToAllowed<Action, AllowedAction>,\n NarrowToNamespace<Event, N> | NarrowToAllowed<Event, AllowedEvent>,\n AllowedAction,\n AllowedEvent\n > {\n return new RestrictedControllerMessenger<\n N,\n NarrowToNamespace<Action, N> | NarrowToAllowed<Action, AllowedAction>,\n NarrowToNamespace<Event, N> | NarrowToAllowed<Event, AllowedEvent>,\n AllowedAction,\n AllowedEvent\n >({\n controllerMessenger: this,\n name,\n allowedActions,\n allowedEvents,\n });\n }\n}\n"]}
1
+ {"version":3,"file":"ControllerMessenger.js","sourceRoot":"","sources":["../src/ControllerMessenger.ts"],"names":[],"mappings":";;;;;;;;;AAAA,mFAAgF;AA4GhF;;;;;;;;;GASG;AACH,MAAa,mBAAmB;IAAhC;QAIE,uCAAoB,IAAI,GAAG,EAA2B,EAAC;QAEvD,sCAAmB,IAAI,GAAG,EAA8C,EAAC;QAEzE;;WAEG;QACH,iDAA8B,IAAI,GAAG,EAGlC,EAAC;IA+RN,CAAC;IA7RC;;;;;;;;;;OAUG;IACH,qBAAqB,CACnB,UAAsB,EACtB,OAA0C;QAE1C,IAAI,uBAAA,IAAI,oCAAS,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE;YACjC,MAAM,IAAI,KAAK,CACb,iBAAiB,UAAU,8BAA8B,CAC1D,CAAC;SACH;QACD,uBAAA,IAAI,oCAAS,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;IAED;;;;;;;OAOG;IACH,uBAAuB,CACrB,UAAsB;QAEtB,uBAAA,IAAI,oCAAS,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACH,YAAY;QACV,uBAAA,IAAI,oCAAS,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,IAAI,CACF,UAAsB,EACtB,GAAG,MAAmD;QAEtD,MAAM,OAAO,GAAG,uBAAA,IAAI,oCAAS,CAAC,GAAG,CAAC,UAAU,CAG3C,CAAC;QACF,IAAI,CAAC,OAAO,EAAE;YACZ,MAAM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AAS,EAAE,WAAW,CAAC,CAAC;SAC1C;QAED,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACrC,CAAC;IAED;;;;;;;;;OASG;IACH,WAAW,CACT,SAAoB,EACpB,OAA8C;QAE9C,MAAM,WAAW,GAAG,uBAAA,IAAI,mCAAQ,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,8CAAmB,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,mCAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,kBAAkB;QAChB,uBAAA,IAAI,mCAAQ,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,aAAa,CAIX,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GAKd;QAQC,OAAO,IAAI,6DAA6B,CAQtC;YACA,mBAAmB,EAAE,IAAI;YACzB,IAAI;YACJ,cAAc;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF;AA7SD,kDA6SC","sourcesContent":["import { RestrictedControllerMessenger } from './RestrictedControllerMessenger';\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<Event extends EventConstraint, ReturnValue> = (\n ...args: ExtractEventPayload<Event>\n) => 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<ExtractEventPayload<Event>, 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 Namespaced<\n Namespace extends string,\n Name,\n> = Name extends `${Namespace}:${string}` ? Name : never;\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 messaging system for controllers.\n *\n * The controller 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 ControllerMessenger<\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 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 * 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<\n ExtractEventPayload<Event, EventType>,\n SelectorReturnValue\n >,\n ): void;\n\n subscribe<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<\n ExtractEventPayload<Event, EventType>,\n SelectorReturnValue\n >,\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\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 controller messenger\n *\n * Returns a wrapper around the controller messenger instance that restricts access to actions\n * and events. The provided allowlists grant the ability to call the listed actions and subscribe\n * to the listed events. The \"name\" provided grants ownership of any actions and events under\n * that 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 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 * @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 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 * @returns The restricted controller messenger.\n */\n getRestricted<\n Namespace extends string,\n AllowedAction extends string,\n AllowedEvent extends string,\n >({\n name,\n allowedActions,\n allowedEvents,\n }: {\n name: Namespace;\n allowedActions?: Extract<Action['type'], AllowedAction>[];\n allowedEvents?: Extract<Event['type'], AllowedEvent>[];\n }): RestrictedControllerMessenger<\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 RestrictedControllerMessenger<\n Namespace,\n | NarrowToNamespace<Action, Namespace>\n | NarrowToAllowed<Action, AllowedAction>,\n | NarrowToNamespace<Event, Namespace>\n | NarrowToAllowed<Event, AllowedEvent>,\n AllowedAction,\n AllowedEvent\n >({\n controllerMessenger: this,\n name,\n allowedActions,\n allowedEvents,\n });\n }\n}\n"]}
@@ -0,0 +1,154 @@
1
+ import type { ActionConstraint, ActionHandler, ControllerMessenger, EventConstraint, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, NamespacedName, SelectorEventHandler, SelectorFunction } from './ControllerMessenger';
2
+ /**
3
+ * A restricted controller messenger.
4
+ *
5
+ * This acts as a wrapper around the controller messenger instance that restricts access to actions
6
+ * and events.
7
+ *
8
+ * @template Namespace - The namespace for this messenger. Typically this is the name of the controller or
9
+ * module that this messenger has been created for. The authority to publish events and register
10
+ * actions under this namespace is granted to this restricted messenger instance.
11
+ * @template Action - A type union of all Action types.
12
+ * @template Event - A type union of all Event types.
13
+ * @template AllowedAction - A type union of the 'type' string for any allowed actions.
14
+ * @template AllowedEvent - A type union of the 'type' string for any allowed events.
15
+ */
16
+ export declare class RestrictedControllerMessenger<Namespace extends string, Action extends ActionConstraint, Event extends EventConstraint, AllowedAction extends string, AllowedEvent extends string> {
17
+ #private;
18
+ /**
19
+ * Constructs a restricted controller messenger
20
+ *
21
+ * The provided allowlists grant the ability to call the listed actions and subscribe to the
22
+ * listed events. The "name" provided grants ownership of any actions and events under that
23
+ * namespace. Ownership allows registering actions and publishing events, as well as
24
+ * unregistering actions and clearing event subscriptions.
25
+ *
26
+ * @param options - The controller options.
27
+ * @param options.controllerMessenger - The controller messenger instance that is being wrapped.
28
+ * @param options.name - The name of the thing this messenger will be handed to (e.g. the
29
+ * controller name). This grants "ownership" of actions and events under this namespace to the
30
+ * restricted controller messenger returned.
31
+ * @param options.allowedActions - The list of actions that this restricted controller messenger
32
+ * should be alowed to call.
33
+ * @param options.allowedEvents - The list of events that this restricted controller messenger
34
+ * should be allowed to subscribe to.
35
+ */
36
+ constructor({ controllerMessenger, name, allowedActions, allowedEvents, }: {
37
+ controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;
38
+ name: Namespace;
39
+ allowedActions?: AllowedAction[];
40
+ allowedEvents?: AllowedEvent[];
41
+ });
42
+ /**
43
+ * Register an action handler.
44
+ *
45
+ * This will make the registered function available to call via the `call` method.
46
+ *
47
+ * The action type this handler is registered under *must* be in the current namespace.
48
+ *
49
+ * @param action - The action type. This is a unqiue identifier for this action.
50
+ * @param handler - The action handler. This function gets called when the `call` method is
51
+ * invoked with the given action type.
52
+ * @throws Will throw if an action handler that is not in the current namespace is being registered.
53
+ * @template ActionType - A type union of Action type strings that are namespaced by Namespace.
54
+ */
55
+ registerActionHandler<ActionType extends Action['type']>(action: ActionType, handler: ActionHandler<Action, ActionType>): void;
56
+ /**
57
+ * Unregister an action handler.
58
+ *
59
+ * This will prevent this action from being called.
60
+ *
61
+ * The action type being unregistered *must* be in the current namespace.
62
+ *
63
+ * @param action - The action type. This is a unqiue identifier for this action.
64
+ * @template ActionType - A type union of Action type strings that are namespaced by Namespace.
65
+ */
66
+ unregisterActionHandler<ActionType extends NamespacedName<Namespace>>(action: ActionType): void;
67
+ /**
68
+ * Call an action.
69
+ *
70
+ * This function will call the action handler corresponding to the given action type, passing
71
+ * along any parameters given.
72
+ *
73
+ * The action type being called must be on the action allowlist.
74
+ *
75
+ * @param action - The action type. This is a unqiue identifier for this action.
76
+ * @param params - The action parameters. These must match the type of the parameters of the
77
+ * registered action handler.
78
+ * @throws Will throw when no handler has been registered for the given type.
79
+ * @template ActionType - A type union of allowed Action type strings.
80
+ * @returns The action return value.
81
+ */
82
+ call<ActionType extends AllowedAction & NamespacedName>(action: ActionType, ...params: ExtractActionParameters<Action, ActionType>): ExtractActionResponse<Action, ActionType>;
83
+ /**
84
+ * Publish an event.
85
+ *
86
+ * Publishes the given payload to all subscribers of the given event type.
87
+ *
88
+ * The event type being published *must* be in the current namespace.
89
+ *
90
+ * @param event - The event type. This is a unique identifier for this event.
91
+ * @param payload - The event payload. The type of the parameters for each event handler must
92
+ * match the type of this payload.
93
+ * @template EventType - A type union of Event type strings that are namespaced by Namespace.
94
+ */
95
+ publish<EventType extends NamespacedName<Namespace>>(event: EventType, ...payload: ExtractEventPayload<Event, EventType>): void;
96
+ /**
97
+ * Subscribe to an event.
98
+ *
99
+ * Registers the given function as an event handler for the given event type.
100
+ *
101
+ * The event type being subscribed to must be on the event allowlist.
102
+ *
103
+ * @param eventType - The event type. This is a unique identifier for this event.
104
+ * @param handler - The event handler. The type of the parameters for this event handler must
105
+ * match the type of the payload for this event type.
106
+ * @template EventType - A type union of Event type strings.
107
+ */
108
+ subscribe<EventType extends AllowedEvent & NamespacedName>(eventType: EventType, handler: ExtractEventHandler<Event, EventType>): void;
109
+ /**
110
+ * Subscribe to an event, with a selector.
111
+ *
112
+ * Registers the given handler function as an event handler for the given
113
+ * event type. When an event is published, its payload is first passed to the
114
+ * selector. The event handler is only called if the selector's return value
115
+ * differs from its last known return value.
116
+ *
117
+ * The event type being subscribed to must be on the event allowlist.
118
+ *
119
+ * @param eventType - The event type. This is a unique identifier for this event.
120
+ * @param handler - The event handler. The type of the parameters for this event
121
+ * handler must match the return type of the selector.
122
+ * @param selector - The selector function used to select relevant data from
123
+ * the event payload. The type of the parameters for this selector must match
124
+ * the type of the payload for this event type.
125
+ * @template EventType - A type union of Event type strings.
126
+ * @template SelectorReturnValue - The selector return value.
127
+ */
128
+ subscribe<EventType extends AllowedEvent & NamespacedName, SelectorReturnValue>(eventType: EventType, handler: SelectorEventHandler<SelectorReturnValue>, selector: SelectorFunction<ExtractEventPayload<Event, EventType>, SelectorReturnValue>): void;
129
+ /**
130
+ * Unsubscribe from an event.
131
+ *
132
+ * Unregisters the given function as an event handler for the given event.
133
+ *
134
+ * The event type being unsubscribed to must be on the event allowlist.
135
+ *
136
+ * @param event - The event type. This is a unique identifier for this event.
137
+ * @param handler - The event handler to unregister.
138
+ * @throws Will throw when the given event handler is not registered for this event.
139
+ * @template EventType - A type union of allowed Event type strings.
140
+ */
141
+ unsubscribe<EventType extends AllowedEvent & NamespacedName>(event: EventType, handler: ExtractEventHandler<Event, EventType>): void;
142
+ /**
143
+ * Clear subscriptions for a specific event.
144
+ *
145
+ * This will remove all subscribed handlers for this event.
146
+ *
147
+ * The event type being cleared *must* be in the current namespace.
148
+ *
149
+ * @param event - The event type. This is a unique identifier for this event.
150
+ * @template EventType - A type union of Event type strings that are namespaced by Namespace.
151
+ */
152
+ clearEventSubscriptions<EventType extends NamespacedName<Namespace>>(event: EventType): void;
153
+ }
154
+ //# sourceMappingURL=RestrictedControllerMessenger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RestrictedControllerMessenger.d.ts","sourceRoot":"","sources":["../src/RestrictedControllerMessenger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,EACd,oBAAoB,EACpB,gBAAgB,EACjB,MAAM,uBAAuB,CAAC;AAE/B;;;;;;;;;;;;;GAaG;AACH,qBAAa,6BAA6B,CACxC,SAAS,SAAS,MAAM,EACxB,MAAM,SAAS,gBAAgB,EAC/B,KAAK,SAAS,eAAe,EAC7B,aAAa,SAAS,MAAM,EAC5B,YAAY,SAAS,MAAM;;IAa3B;;;;;;;;;;;;;;;;;OAiBG;gBACS,EACV,mBAAmB,EACnB,IAAI,EACJ,cAAc,EACd,aAAa,GACd,EAAE;QACD,mBAAmB,EAAE,mBAAmB,CAAC,gBAAgB,EAAE,eAAe,CAAC,CAAC;QAC5E,IAAI,EAAE,SAAS,CAAC;QAChB,cAAc,CAAC,EAAE,aAAa,EAAE,CAAC;QACjC,aAAa,CAAC,EAAE,YAAY,EAAE,CAAC;KAChC;IAOD;;;;;;;;;;;;OAYG;IACH,qBAAqB,CAAC,UAAU,SAAS,MAAM,CAAC,MAAM,CAAC,EACrD,MAAM,EAAE,UAAU,EAClB,OAAO,EAAE,aAAa,CAAC,MAAM,EAAE,UAAU,CAAC;IAa5C;;;;;;;;;OASG;IACH,uBAAuB,CAAC,UAAU,SAAS,cAAc,CAAC,SAAS,CAAC,EAClE,MAAM,EAAE,UAAU;IAapB;;;;;;;;;;;;;;OAcG;IACH,IAAI,CAAC,UAAU,SAAS,aAAa,GAAG,cAAc,EACpD,MAAM,EAAE,UAAU,EAClB,GAAG,MAAM,E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