@metamask-previews/messenger 0.0.0-preview-3713f9f

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 ADDED
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+
12
+ - Migrate `Messenger` class from `@metamask/base-controller` package ([#6127](https://github.com/MetaMask/core/pull/6127))
13
+
14
+ [Unreleased]: https://github.com/MetaMask/core/
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MetaMask
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # `@metamask/messenger`
2
+
3
+ A type-safe message bus library.
4
+
5
+ The `Messenger` class allows registering functions as 'actions' that can be called elsewhere, and it allows publishing and subscribing to events. Both actions and events are identified by namespaced strings.
6
+
7
+ ## Installation
8
+
9
+ `yarn add @metamask/messenger`
10
+
11
+ or
12
+
13
+ `npm install @metamask/messenger`
14
+
15
+ ## Contributing
16
+
17
+ This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
@@ -0,0 +1,254 @@
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 _Messenger_actions, _Messenger_events, _Messenger_initialEventPayloadGetters, _Messenger_eventPayloadCache;
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.Messenger = void 0;
10
+ const RestrictedMessenger_1 = require("./RestrictedMessenger.cjs");
11
+ /**
12
+ * A message broker for "actions" and "events".
13
+ *
14
+ * The messenger allows registering functions as 'actions' that can be called elsewhere,
15
+ * and it allows publishing and subscribing to events. Both actions and events are identified by
16
+ * unique strings.
17
+ *
18
+ * @template Action - A type union of all Action types.
19
+ * @template Event - A type union of all Event types.
20
+ */
21
+ class Messenger {
22
+ constructor() {
23
+ _Messenger_actions.set(this, new Map());
24
+ _Messenger_events.set(this, new Map());
25
+ /**
26
+ * A map of functions for getting the initial event payload.
27
+ *
28
+ * Used only for events that represent state changes.
29
+ */
30
+ _Messenger_initialEventPayloadGetters.set(this, new Map());
31
+ /**
32
+ * A cache of selector return values for their respective handlers.
33
+ */
34
+ _Messenger_eventPayloadCache.set(this, new Map());
35
+ }
36
+ /**
37
+ * Register an action handler.
38
+ *
39
+ * This will make the registered function available to call via the `call` method.
40
+ *
41
+ * @param actionType - The action type. This is a unique identifier for this action.
42
+ * @param handler - The action handler. This function gets called when the `call` method is
43
+ * invoked with the given action type.
44
+ * @throws Will throw when a handler has been registered for this action type already.
45
+ * @template ActionType - A type union of Action type strings.
46
+ */
47
+ registerActionHandler(actionType, handler) {
48
+ if (__classPrivateFieldGet(this, _Messenger_actions, "f").has(actionType)) {
49
+ throw new Error(`A handler for ${actionType} has already been registered`);
50
+ }
51
+ __classPrivateFieldGet(this, _Messenger_actions, "f").set(actionType, handler);
52
+ }
53
+ /**
54
+ * Registers action handlers for a list of methods on a messenger client
55
+ *
56
+ * @param messengerClient - The object that is expected to make use of the messenger.
57
+ * @param methodNames - The names of the methods on the messenger client to register as action
58
+ * handlers
59
+ */
60
+ registerMethodActionHandlers(messengerClient, methodNames) {
61
+ for (const methodName of methodNames) {
62
+ const method = messengerClient[methodName];
63
+ if (typeof method === 'function') {
64
+ const actionType = `${messengerClient.name}:${methodName}`;
65
+ this.registerActionHandler(actionType, method.bind(messengerClient));
66
+ }
67
+ }
68
+ }
69
+ /**
70
+ * Unregister an action handler.
71
+ *
72
+ * This will prevent this action from being called.
73
+ *
74
+ * @param actionType - The action type. This is a unique identifier for this action.
75
+ * @template ActionType - A type union of Action type strings.
76
+ */
77
+ unregisterActionHandler(actionType) {
78
+ __classPrivateFieldGet(this, _Messenger_actions, "f").delete(actionType);
79
+ }
80
+ /**
81
+ * Unregister all action handlers.
82
+ *
83
+ * This prevents all actions from being called.
84
+ */
85
+ clearActions() {
86
+ __classPrivateFieldGet(this, _Messenger_actions, "f").clear();
87
+ }
88
+ /**
89
+ * Call an action.
90
+ *
91
+ * This function will call the action handler corresponding to the given action type, passing
92
+ * along any parameters given.
93
+ *
94
+ * @param actionType - The action type. This is a unique identifier for this action.
95
+ * @param params - The action parameters. These must match the type of the parameters of the
96
+ * registered action handler.
97
+ * @throws Will throw when no handler has been registered for the given type.
98
+ * @template ActionType - A type union of Action type strings.
99
+ * @returns The action return value.
100
+ */
101
+ call(actionType, ...params) {
102
+ const handler = __classPrivateFieldGet(this, _Messenger_actions, "f").get(actionType);
103
+ if (!handler) {
104
+ throw new Error(`A handler for ${actionType} has not been registered`);
105
+ }
106
+ return handler(...params);
107
+ }
108
+ /**
109
+ * Register a function for getting the initial payload for an event.
110
+ *
111
+ * This is used for events that represent a state change, where the payload is the state.
112
+ * Registering a function for getting the payload allows event selectors to have a point of
113
+ * comparison the first time state changes.
114
+ *
115
+ * @param args - The arguments to this function
116
+ * @param args.eventType - The event type to register a payload for.
117
+ * @param args.getPayload - A function for retrieving the event payload.
118
+ */
119
+ registerInitialEventPayload({ eventType, getPayload, }) {
120
+ __classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").set(eventType, getPayload);
121
+ }
122
+ /**
123
+ * Publish an event.
124
+ *
125
+ * Publishes the given payload to all subscribers of the given event type.
126
+ *
127
+ * Note that this method should never throw directly. Any errors from
128
+ * subscribers are captured and re-thrown in a timeout handler.
129
+ *
130
+ * @param eventType - The event type. This is a unique identifier for this event.
131
+ * @param payload - The event payload. The type of the parameters for each event handler must
132
+ * match the type of this payload.
133
+ * @template EventType - A type union of Event type strings.
134
+ */
135
+ publish(eventType, ...payload) {
136
+ const subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
137
+ if (subscribers) {
138
+ for (const [handler, selector] of subscribers.entries()) {
139
+ try {
140
+ if (selector) {
141
+ const previousValue = __classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").get(handler);
142
+ const newValue = selector(...payload);
143
+ if (newValue !== previousValue) {
144
+ __classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").set(handler, newValue);
145
+ handler(newValue, previousValue);
146
+ }
147
+ }
148
+ else {
149
+ handler(...payload);
150
+ }
151
+ }
152
+ catch (error) {
153
+ // Throw error after timeout so that it is capured as a console error
154
+ // (and by Sentry) without interrupting the event publishing.
155
+ setTimeout(() => {
156
+ throw error;
157
+ });
158
+ }
159
+ }
160
+ }
161
+ }
162
+ subscribe(eventType, handler, selector) {
163
+ let subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
164
+ if (!subscribers) {
165
+ subscribers = new Map();
166
+ __classPrivateFieldGet(this, _Messenger_events, "f").set(eventType, subscribers);
167
+ }
168
+ subscribers.set(handler, selector);
169
+ if (selector) {
170
+ const getPayload = __classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").get(eventType);
171
+ if (getPayload) {
172
+ const initialValue = selector(...getPayload());
173
+ __classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").set(handler, initialValue);
174
+ }
175
+ }
176
+ }
177
+ /**
178
+ * Unsubscribe from an event.
179
+ *
180
+ * Unregisters the given function as an event handler for the given event.
181
+ *
182
+ * @param eventType - The event type. This is a unique identifier for this event.
183
+ * @param handler - The event handler to unregister.
184
+ * @throws Will throw when the given event handler is not registered for this event.
185
+ * @template EventType - A type union of Event type strings.
186
+ */
187
+ unsubscribe(eventType, handler) {
188
+ const subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
189
+ if (!subscribers || !subscribers.has(handler)) {
190
+ throw new Error(`Subscription not found for event: ${eventType}`);
191
+ }
192
+ const selector = subscribers.get(handler);
193
+ if (selector) {
194
+ __classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").delete(handler);
195
+ }
196
+ subscribers.delete(handler);
197
+ }
198
+ /**
199
+ * Clear subscriptions for a specific event.
200
+ *
201
+ * This will remove all subscribed handlers for this event.
202
+ *
203
+ * @param eventType - The event type. This is a unique identifier for this event.
204
+ * @template EventType - A type union of Event type strings.
205
+ */
206
+ clearEventSubscriptions(eventType) {
207
+ __classPrivateFieldGet(this, _Messenger_events, "f").delete(eventType);
208
+ }
209
+ /**
210
+ * Clear all subscriptions.
211
+ *
212
+ * This will remove all subscribed handlers for all events.
213
+ */
214
+ clearSubscriptions() {
215
+ __classPrivateFieldGet(this, _Messenger_events, "f").clear();
216
+ }
217
+ /**
218
+ * Get a restricted messenger
219
+ *
220
+ * Returns a wrapper around the messenger instance that restricts access to actions and events.
221
+ * The provided allowlists grant the ability to call the listed actions and subscribe to the
222
+ * listed events. The "name" provided grants ownership of any actions and events under that
223
+ * namespace. Ownership allows registering actions and publishing events, as well as
224
+ * unregistering actions and clearing event subscriptions.
225
+ *
226
+ * @param options - Messenger options.
227
+ * @param options.name - The name of the thing this messenger will be handed to (e.g. the
228
+ * controller name). This grants "ownership" of actions and events under this namespace to the
229
+ * restricted messenger returned.
230
+ * @param options.allowedActions - The list of actions that this restricted messenger should be
231
+ * allowed to call.
232
+ * @param options.allowedEvents - The list of events that this restricted messenger should be
233
+ * allowed to subscribe to.
234
+ * @template Namespace - The namespace for this messenger. Typically this is the name of the
235
+ * module that this messenger has been created for. The authority to publish events and register
236
+ * actions under this namespace is granted to this restricted messenger instance.
237
+ * @template AllowedAction - A type union of the 'type' string for any allowed actions.
238
+ * This must not include internal actions that are in the messenger's namespace.
239
+ * @template AllowedEvent - A type union of the 'type' string for any allowed events.
240
+ * This must not include internal events that are in the messenger's namespace.
241
+ * @returns The restricted messenger.
242
+ */
243
+ getRestricted({ name, allowedActions, allowedEvents, }) {
244
+ return new RestrictedMessenger_1.RestrictedMessenger({
245
+ messenger: this,
246
+ name,
247
+ allowedActions,
248
+ allowedEvents,
249
+ });
250
+ }
251
+ }
252
+ exports.Messenger = Messenger;
253
+ _Messenger_actions = new WeakMap(), _Messenger_events = new WeakMap(), _Messenger_initialEventPayloadGetters = new WeakMap(), _Messenger_eventPayloadCache = new WeakMap();
254
+ //# sourceMappingURL=Messenger.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Messenger.cjs","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":";;;;;;;;;AAAA,mEAA4D;AAmH5D;;;;;;;;;GASG;AACH,MAAa,SAAS;IAAtB;QAIW,6BAAW,IAAI,GAAG,EAA2B,EAAC;QAE9C,4BAAU,IAAI,GAAG,EAA8C,EAAC;QAEzE;;;;WAIG;QACM,g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;;;;;;;;;;;;;;;;;OAyBG;IACH,aAAa,CAIX,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GAWd;QAQC,OAAO,IAAI,yCAAmB,CAAC;YAC7B,SAAS,EAAE,IAAI;YACf,IAAI;YACJ,cAAc;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF;AAlWD,8BAkWC","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 unique 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 * Registers action handlers for a list of methods on a messenger client\n *\n * @param messengerClient - The object that is expected to make use of the messenger.\n * @param methodNames - The names of the methods on the messenger client to register as action\n * handlers\n */\n registerMethodActionHandlers<\n MessengerClient extends { name: string },\n MethodNames extends keyof MessengerClient & string,\n >(messengerClient: MessengerClient, methodNames: readonly MethodNames[]) {\n for (const methodName of methodNames) {\n const method = messengerClient[methodName];\n if (typeof method === 'function') {\n const actionType = `${messengerClient.name}:${methodName}` as const;\n this.registerActionHandler(actionType, method.bind(messengerClient));\n }\n }\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 unique 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 unique 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 - 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"]}
@@ -0,0 +1,227 @@
1
+ import { RestrictedMessenger } from "./RestrictedMessenger.cjs";
2
+ export type ActionHandler<Action extends ActionConstraint, ActionType = Action['type']> = (...args: ExtractActionParameters<Action, ActionType>) => ExtractActionResponse<Action, ActionType>;
3
+ export type ExtractActionParameters<Action extends ActionConstraint, ActionType = Action['type']> = Action extends {
4
+ type: ActionType;
5
+ handler: (...args: infer HandlerArgs) => unknown;
6
+ } ? HandlerArgs : never;
7
+ export type ExtractActionResponse<Action extends ActionConstraint, ActionType = Action['type']> = Action extends {
8
+ type: ActionType;
9
+ handler: (...args: infer _) => infer HandlerReturnValue;
10
+ } ? HandlerReturnValue : never;
11
+ export type ExtractEventHandler<Event extends EventConstraint, EventType = Event['type']> = Event extends {
12
+ type: EventType;
13
+ payload: infer Payload;
14
+ } ? Payload extends unknown[] ? (...payload: Payload) => void : never : never;
15
+ export type ExtractEventPayload<Event extends EventConstraint, EventType = Event['type']> = Event extends {
16
+ type: EventType;
17
+ payload: infer Payload;
18
+ } ? Payload extends unknown[] ? Payload : never : never;
19
+ export type GenericEventHandler = (...args: unknown[]) => void;
20
+ export type SelectorFunction<Event extends EventConstraint, EventType extends Event['type'], ReturnValue> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;
21
+ export type SelectorEventHandler<SelectorReturnValue> = (newValue: SelectorReturnValue, previousValue: SelectorReturnValue | undefined) => void;
22
+ export type ActionConstraint = {
23
+ type: string;
24
+ handler: ((...args: never) => unknown) | ((...args: never[]) => unknown);
25
+ };
26
+ export type EventConstraint = {
27
+ type: string;
28
+ payload: unknown[];
29
+ };
30
+ /**
31
+ * A namespaced string
32
+ *
33
+ * This type verifies that the string Name is prefixed by the string Name followed by a colon.
34
+ *
35
+ * @template Namespace - The namespace we're checking for.
36
+ * @template Name - The full string, including the namespace.
37
+ */
38
+ export type NamespacedBy<Namespace extends string, Name extends string> = Name extends `${Namespace}:${string}` ? Name : never;
39
+ export type NotNamespacedBy<Namespace extends string, Name extends string> = Name extends `${Namespace}:${string}` ? never : Name;
40
+ export type NamespacedName<Namespace extends string = string> = `${Namespace}:${string}`;
41
+ type NarrowToNamespace<Name, Namespace extends string> = Name extends {
42
+ type: `${Namespace}:${string}`;
43
+ } ? Name : never;
44
+ type NarrowToAllowed<Name, Allowed extends string> = Name extends {
45
+ type: Allowed;
46
+ } ? Name : never;
47
+ /**
48
+ * A message broker for "actions" and "events".
49
+ *
50
+ * The messenger allows registering functions as 'actions' that can be called elsewhere,
51
+ * and it allows publishing and subscribing to events. Both actions and events are identified by
52
+ * unique strings.
53
+ *
54
+ * @template Action - A type union of all Action types.
55
+ * @template Event - A type union of all Event types.
56
+ */
57
+ export declare class Messenger<Action extends ActionConstraint, Event extends EventConstraint> {
58
+ #private;
59
+ /**
60
+ * Register an action handler.
61
+ *
62
+ * This will make the registered function available to call via the `call` method.
63
+ *
64
+ * @param actionType - The action type. This is a unique identifier for this action.
65
+ * @param handler - The action handler. This function gets called when the `call` method is
66
+ * invoked with the given action type.
67
+ * @throws Will throw when a handler has been registered for this action type already.
68
+ * @template ActionType - A type union of Action type strings.
69
+ */
70
+ registerActionHandler<ActionType extends Action['type']>(actionType: ActionType, handler: ActionHandler<Action, ActionType>): void;
71
+ /**
72
+ * Registers action handlers for a list of methods on a messenger client
73
+ *
74
+ * @param messengerClient - The object that is expected to make use of the messenger.
75
+ * @param methodNames - The names of the methods on the messenger client to register as action
76
+ * handlers
77
+ */
78
+ registerMethodActionHandlers<MessengerClient extends {
79
+ name: string;
80
+ }, MethodNames extends keyof MessengerClient & string>(messengerClient: MessengerClient, methodNames: readonly MethodNames[]): void;
81
+ /**
82
+ * Unregister an action handler.
83
+ *
84
+ * This will prevent this action from being called.
85
+ *
86
+ * @param actionType - The action type. This is a unique identifier for this action.
87
+ * @template ActionType - A type union of Action type strings.
88
+ */
89
+ unregisterActionHandler<ActionType extends Action['type']>(actionType: ActionType): void;
90
+ /**
91
+ * Unregister all action handlers.
92
+ *
93
+ * This prevents all actions from being called.
94
+ */
95
+ clearActions(): void;
96
+ /**
97
+ * Call an action.
98
+ *
99
+ * This function will call the action handler corresponding to the given action type, passing
100
+ * along any parameters given.
101
+ *
102
+ * @param actionType - The action type. This is a unique identifier for this action.
103
+ * @param params - The action parameters. These must match the type of the parameters of the
104
+ * registered action handler.
105
+ * @throws Will throw when no handler has been registered for the given type.
106
+ * @template ActionType - A type union of Action type strings.
107
+ * @returns The action return value.
108
+ */
109
+ call<ActionType extends Action['type']>(actionType: ActionType, ...params: ExtractActionParameters<Action, ActionType>): ExtractActionResponse<Action, ActionType>;
110
+ /**
111
+ * Register a function for getting the initial payload for an event.
112
+ *
113
+ * This is used for events that represent a state change, where the payload is the state.
114
+ * Registering a function for getting the payload allows event selectors to have a point of
115
+ * comparison the first time state changes.
116
+ *
117
+ * @param args - The arguments to this function
118
+ * @param args.eventType - The event type to register a payload for.
119
+ * @param args.getPayload - A function for retrieving the event payload.
120
+ */
121
+ registerInitialEventPayload<EventType extends Event['type']>({ eventType, getPayload, }: {
122
+ eventType: EventType;
123
+ getPayload: () => ExtractEventPayload<Event, EventType>;
124
+ }): void;
125
+ /**
126
+ * Publish an event.
127
+ *
128
+ * Publishes the given payload to all subscribers of the given event type.
129
+ *
130
+ * Note that this method should never throw directly. Any errors from
131
+ * subscribers are captured and re-thrown in a timeout handler.
132
+ *
133
+ * @param eventType - The event type. This is a unique identifier for this event.
134
+ * @param payload - The event payload. The type of the parameters for each event handler must
135
+ * match the type of this payload.
136
+ * @template EventType - A type union of Event type strings.
137
+ */
138
+ publish<EventType extends Event['type']>(eventType: EventType, ...payload: ExtractEventPayload<Event, EventType>): void;
139
+ /**
140
+ * Subscribe to an event.
141
+ *
142
+ * Registers the given function as an event handler for the given event type.
143
+ *
144
+ * @param eventType - The event type. This is a unique identifier for this event.
145
+ * @param handler - The event handler. The type of the parameters for this event handler must
146
+ * match the type of the payload for this event type.
147
+ * @template EventType - A type union of Event type strings.
148
+ */
149
+ subscribe<EventType extends Event['type']>(eventType: EventType, handler: ExtractEventHandler<Event, EventType>): void;
150
+ /**
151
+ * Subscribe to an event, with a selector.
152
+ *
153
+ * Registers the given handler function as an event handler for the given
154
+ * event type. When an event is published, its payload is first passed to the
155
+ * selector. The event handler is only called if the selector's return value
156
+ * differs from its last known return value.
157
+ *
158
+ * @param eventType - The event type. This is a unique identifier for this event.
159
+ * @param handler - The event handler. The type of the parameters for this event
160
+ * handler must match the return type of the selector.
161
+ * @param selector - The selector function used to select relevant data from
162
+ * the event payload. The type of the parameters for this selector must match
163
+ * the type of the payload for this event type.
164
+ * @template EventType - A type union of Event type strings.
165
+ * @template SelectorReturnValue - The selector return value.
166
+ */
167
+ subscribe<EventType extends Event['type'], SelectorReturnValue>(eventType: EventType, handler: SelectorEventHandler<SelectorReturnValue>, selector: SelectorFunction<Event, EventType, SelectorReturnValue>): void;
168
+ /**
169
+ * Unsubscribe from an event.
170
+ *
171
+ * Unregisters the given function as an event handler for the given event.
172
+ *
173
+ * @param eventType - The event type. This is a unique identifier for this event.
174
+ * @param handler - The event handler to unregister.
175
+ * @throws Will throw when the given event handler is not registered for this event.
176
+ * @template EventType - A type union of Event type strings.
177
+ */
178
+ unsubscribe<EventType extends Event['type']>(eventType: EventType, handler: ExtractEventHandler<Event, EventType>): void;
179
+ /**
180
+ * Clear subscriptions for a specific event.
181
+ *
182
+ * This will remove all subscribed handlers for this event.
183
+ *
184
+ * @param eventType - The event type. This is a unique identifier for this event.
185
+ * @template EventType - A type union of Event type strings.
186
+ */
187
+ clearEventSubscriptions<EventType extends Event['type']>(eventType: EventType): void;
188
+ /**
189
+ * Clear all subscriptions.
190
+ *
191
+ * This will remove all subscribed handlers for all events.
192
+ */
193
+ clearSubscriptions(): void;
194
+ /**
195
+ * Get a restricted messenger
196
+ *
197
+ * Returns a wrapper around the messenger instance that restricts access to actions and events.
198
+ * The provided allowlists grant the ability to call the listed actions and subscribe to the
199
+ * listed events. The "name" provided grants ownership of any actions and events under that
200
+ * namespace. Ownership allows registering actions and publishing events, as well as
201
+ * unregistering actions and clearing event subscriptions.
202
+ *
203
+ * @param options - Messenger options.
204
+ * @param options.name - The name of the thing this messenger will be handed to (e.g. the
205
+ * controller name). This grants "ownership" of actions and events under this namespace to the
206
+ * restricted messenger returned.
207
+ * @param options.allowedActions - The list of actions that this restricted messenger should be
208
+ * allowed to call.
209
+ * @param options.allowedEvents - The list of events that this restricted messenger should be
210
+ * allowed to subscribe to.
211
+ * @template Namespace - The namespace for this messenger. Typically this is the name of the
212
+ * module that this messenger has been created for. The authority to publish events and register
213
+ * actions under this namespace is granted to this restricted messenger instance.
214
+ * @template AllowedAction - A type union of the 'type' string for any allowed actions.
215
+ * This must not include internal actions that are in the messenger's namespace.
216
+ * @template AllowedEvent - A type union of the 'type' string for any allowed events.
217
+ * This must not include internal events that are in the messenger's namespace.
218
+ * @returns The restricted messenger.
219
+ */
220
+ getRestricted<Namespace extends string, AllowedAction extends NotNamespacedBy<Namespace, Action['type']> = never, AllowedEvent extends NotNamespacedBy<Namespace, Event['type']> = never>({ name, allowedActions, allowedEvents, }: {
221
+ name: Namespace;
222
+ allowedActions: NotNamespacedBy<Namespace, Extract<Action['type'], AllowedAction>>[];
223
+ allowedEvents: NotNamespacedBy<Namespace, Extract<Event['type'], AllowedEvent>>[];
224
+ }): RestrictedMessenger<Namespace, NarrowToNamespace<Action, Namespace> | NarrowToAllowed<Action, AllowedAction>, NarrowToNamespace<Event, Namespace> | NarrowToAllowed<Event, AllowedEvent>, AllowedAction, AllowedEvent>;
225
+ }
226
+ export {};
227
+ //# sourceMappingURL=Messenger.d.cts.map
@@ -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,qBA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