@metamask-previews/messenger 2.0.0-preview-4f4c98e → 2.0.0-preview-eb263b0ee
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -4
- package/dist/Messenger.cjs +702 -0
- package/dist/Messenger.cjs.map +1 -0
- package/dist/{Messenger.d.ts → Messenger.d.cts} +1 -1
- package/dist/Messenger.d.cts.map +1 -0
- package/dist/Messenger.d.mts +587 -0
- package/dist/Messenger.d.mts.map +1 -0
- package/dist/{Messenger.js → Messenger.mjs} +1 -1
- package/dist/Messenger.mjs.map +1 -0
- package/dist/index.cjs +7 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{index.d.ts → index.d.cts} +3 -3
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +17 -13
- package/dist/Messenger.d.ts.map +0 -1
- package/dist/Messenger.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _Messenger_instances, _Messenger_namespace, _Messenger_parent, _Messenger_actions, _Messenger_events, _Messenger_deferredPublishes, _Messenger_subscriptionDelegationTargets, _Messenger_actionDelegationTargets, _Messenger_initialEventPayloadGetters, _Messenger_eventPayloadCache, _Messenger_registerActionHandler, _Messenger_unregisterActionHandler, _Messenger_registerInitialEventPayload, _Messenger_publish, _Messenger_deliverToSubscribers, _Messenger_subscribe, _Messenger_isInCurrentNamespace;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Messenger = exports.MOCK_ANY_NAMESPACE = void 0;
|
|
16
|
+
/**
|
|
17
|
+
* Messenger namespace checks can be disabled by using this as the `namespace` constructor
|
|
18
|
+
* parameter, and using `MockAnyNamespace` as the Namespace type parameter.
|
|
19
|
+
*
|
|
20
|
+
* This is useful for mocking a variety of different actions/events in unit tests. Please do not
|
|
21
|
+
* use this in production code.
|
|
22
|
+
*/
|
|
23
|
+
exports.MOCK_ANY_NAMESPACE = 'MOCK_ANY_NAMESPACE';
|
|
24
|
+
/**
|
|
25
|
+
* A message broker for "actions" and "events".
|
|
26
|
+
*
|
|
27
|
+
* The messenger allows registering functions as 'actions' that can be called elsewhere,
|
|
28
|
+
* and it allows publishing and subscribing to events. Both actions and events are identified by
|
|
29
|
+
* unique strings prefixed by a namespace (which is delimited by a colon, e.g.
|
|
30
|
+
* `Namespace:actionName`).
|
|
31
|
+
*
|
|
32
|
+
* @template Action - A type union of all Action types.
|
|
33
|
+
* @template Event - A type union of all Event types.
|
|
34
|
+
* @template Namespace - The namespace for the messenger.
|
|
35
|
+
*/
|
|
36
|
+
class Messenger {
|
|
37
|
+
/**
|
|
38
|
+
* Construct a messenger.
|
|
39
|
+
*
|
|
40
|
+
* If a parent messenger is given, all actions and events under this messenger's namespace will
|
|
41
|
+
* be delegated to the parent automatically.
|
|
42
|
+
*
|
|
43
|
+
* @param args - Constructor arguments
|
|
44
|
+
* @param args.captureException - Reports an error to an error monitoring service.
|
|
45
|
+
* @param args.namespace - The messenger namespace.
|
|
46
|
+
* @param args.parent - The parent messenger.
|
|
47
|
+
*/
|
|
48
|
+
constructor({ captureException, namespace, parent, }) {
|
|
49
|
+
_Messenger_instances.add(this);
|
|
50
|
+
_Messenger_namespace.set(this, void 0);
|
|
51
|
+
/**
|
|
52
|
+
* The parent messenger. All actions/events under this namespace are automatically delegated to
|
|
53
|
+
* the parent messenger.
|
|
54
|
+
*/
|
|
55
|
+
_Messenger_parent.set(this, void 0);
|
|
56
|
+
_Messenger_actions.set(this, new Map());
|
|
57
|
+
_Messenger_events.set(this, new Map());
|
|
58
|
+
/**
|
|
59
|
+
* In-progress publishes, keyed by event type. A key is present for the
|
|
60
|
+
* duration of a publish (so presence means "publishing"); its array collects
|
|
61
|
+
* re-entrant publishes of that event, drained when the publish finishes.
|
|
62
|
+
*/
|
|
63
|
+
_Messenger_deferredPublishes.set(this, new Map());
|
|
64
|
+
/**
|
|
65
|
+
* The set of messengers we've delegated events to and their event handlers, by event type.
|
|
66
|
+
*/
|
|
67
|
+
_Messenger_subscriptionDelegationTargets.set(this, new Map());
|
|
68
|
+
/**
|
|
69
|
+
* The set of messengers we've delegated actions to, by action type.
|
|
70
|
+
*/
|
|
71
|
+
_Messenger_actionDelegationTargets.set(this, new Map());
|
|
72
|
+
/**
|
|
73
|
+
* A map of functions for getting the initial event payload.
|
|
74
|
+
*
|
|
75
|
+
* Used only for events that represent state changes.
|
|
76
|
+
*/
|
|
77
|
+
_Messenger_initialEventPayloadGetters.set(this, new Map());
|
|
78
|
+
/**
|
|
79
|
+
* A cache of selector return values for their respective handlers.
|
|
80
|
+
*/
|
|
81
|
+
_Messenger_eventPayloadCache.set(this, new Map());
|
|
82
|
+
__classPrivateFieldSet(this, _Messenger_namespace, namespace, "f");
|
|
83
|
+
__classPrivateFieldSet(this, _Messenger_parent, parent, "f");
|
|
84
|
+
this.captureException = captureException ?? __classPrivateFieldGet(this, _Messenger_parent, "f")?.captureException;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Register an action handler.
|
|
88
|
+
*
|
|
89
|
+
* This will make the registered function available to call via the `call` method.
|
|
90
|
+
*
|
|
91
|
+
* The action being registered must be under the same namespace as the messenger.
|
|
92
|
+
*
|
|
93
|
+
* @param actionType - The action type. This is a unique identifier for this action.
|
|
94
|
+
* @param handler - The action handler. This function gets called when the `call` method is
|
|
95
|
+
* invoked with the given action type.
|
|
96
|
+
* @throws Will throw when a handler has been registered for this action type already.
|
|
97
|
+
* @template ActionType - A type union of Action type strings under this messenger's namespace.
|
|
98
|
+
*/
|
|
99
|
+
registerActionHandler(actionType, handler) {
|
|
100
|
+
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, actionType)) {
|
|
101
|
+
throw new Error(`Only allowed registering action handlers prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);
|
|
102
|
+
}
|
|
103
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_registerActionHandler).call(this, actionType, handler);
|
|
104
|
+
if (__classPrivateFieldGet(this, _Messenger_parent, "f")) {
|
|
105
|
+
// @ts-expect-error The parent type isn't constructed in a way that proves it supports this
|
|
106
|
+
// action, but this is OK because it's validated in the constructor.
|
|
107
|
+
this.delegate({ actions: [actionType], messenger: __classPrivateFieldGet(this, _Messenger_parent, "f") });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Registers action handlers for a list of methods on a messenger client
|
|
112
|
+
*
|
|
113
|
+
* @param messengerClient - The object that is expected to make use of the messenger.
|
|
114
|
+
* @param methodNames - The names of the methods on the messenger client to register as action
|
|
115
|
+
* handlers.
|
|
116
|
+
* @template MessengerClient - The type expected to make use of the messenger.
|
|
117
|
+
* @template MethodNames - The type union of method names to register as action handlers.
|
|
118
|
+
*/
|
|
119
|
+
registerMethodActionHandlers(messengerClient, methodNames) {
|
|
120
|
+
for (const methodName of methodNames) {
|
|
121
|
+
const method = messengerClient[methodName];
|
|
122
|
+
if (typeof method === 'function') {
|
|
123
|
+
const actionType = `${messengerClient.name}:${methodName}`;
|
|
124
|
+
this.registerActionHandler(actionType, method.bind(messengerClient));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Unregister an action handler.
|
|
130
|
+
*
|
|
131
|
+
* This will prevent this action from being called.
|
|
132
|
+
*
|
|
133
|
+
* The action being unregistered must be under the same namespace as the messenger.
|
|
134
|
+
*
|
|
135
|
+
* @param actionType - The action type. This is a unique identifier for this action.
|
|
136
|
+
* @template ActionType - A type union of Action type strings under this messenger's namespace.
|
|
137
|
+
*/
|
|
138
|
+
unregisterActionHandler(actionType) {
|
|
139
|
+
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, actionType)) {
|
|
140
|
+
throw new Error(`Only allowed unregistering action handlers prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);
|
|
141
|
+
}
|
|
142
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_unregisterActionHandler).call(this, actionType);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Unregister all action handlers.
|
|
146
|
+
*
|
|
147
|
+
* This prevents all actions from being called.
|
|
148
|
+
*/
|
|
149
|
+
clearActions() {
|
|
150
|
+
for (const actionType of __classPrivateFieldGet(this, _Messenger_actions, "f").keys()) {
|
|
151
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_unregisterActionHandler).call(this, actionType);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Get the types of all actions that this messenger can call directly.
|
|
156
|
+
*
|
|
157
|
+
* This includes actions registered on this messenger as well as actions that
|
|
158
|
+
* have been delegated to it from another messenger.
|
|
159
|
+
*
|
|
160
|
+
* Note that this reflects the registrations on this specific messenger
|
|
161
|
+
* instance.
|
|
162
|
+
*
|
|
163
|
+
* @returns An array of every action type this messenger can call directly.
|
|
164
|
+
*/
|
|
165
|
+
getRegisteredActionTypes() {
|
|
166
|
+
return [...__classPrivateFieldGet(this, _Messenger_actions, "f").keys()];
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Get the action handler for a given action type.
|
|
170
|
+
*
|
|
171
|
+
* This is a protected method to allow subclasses to override the way action
|
|
172
|
+
* handlers are retrieved, for example to implement custom delegation logic.
|
|
173
|
+
*
|
|
174
|
+
* @param actionType - The action type. This is a unique identifier for this
|
|
175
|
+
* action.
|
|
176
|
+
* @returns The action handler for the given action type, or undefined if no
|
|
177
|
+
* handler has been registered.
|
|
178
|
+
*/
|
|
179
|
+
getAction(actionType) {
|
|
180
|
+
return __classPrivateFieldGet(this, _Messenger_actions, "f").get(actionType);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Create a new messenger as a child of this messenger (the "parent").
|
|
184
|
+
* All actions/events are delegated from the child to the parent, and the specified actions/events are delegated from the parent to the child.
|
|
185
|
+
*
|
|
186
|
+
* @param args - Arguments.
|
|
187
|
+
* @param args.namespace - The child messenger namespace.
|
|
188
|
+
* @param args.actions - A list of action types to delegate to the child messenger.
|
|
189
|
+
* @param args.events - A list of event types to delegate to the child messenger.
|
|
190
|
+
* @returns The child messenger.
|
|
191
|
+
*/
|
|
192
|
+
buildChild({ namespace, actions, events, }) {
|
|
193
|
+
const childMessenger = new Messenger({
|
|
194
|
+
namespace,
|
|
195
|
+
// @ts-expect-error TypeScript cannot correctly infer this, but should be safe
|
|
196
|
+
// given `ChildAction extends Action` and `ChildEvent extends Event`.
|
|
197
|
+
parent: this,
|
|
198
|
+
});
|
|
199
|
+
this.delegate({
|
|
200
|
+
messenger: childMessenger,
|
|
201
|
+
actions,
|
|
202
|
+
events,
|
|
203
|
+
});
|
|
204
|
+
return childMessenger;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Call an action.
|
|
208
|
+
*
|
|
209
|
+
* This function will call the action handler corresponding to the given action type, passing
|
|
210
|
+
* along any parameters given.
|
|
211
|
+
*
|
|
212
|
+
* @param actionType - The action type. This is a unique identifier for this action.
|
|
213
|
+
* @param params - The action parameters. These must match the type of the parameters of the
|
|
214
|
+
* registered action handler.
|
|
215
|
+
* @throws Will throw when no handler has been registered for the given type.
|
|
216
|
+
* @template ActionType - A type union of Action type strings.
|
|
217
|
+
* @returns The action return value.
|
|
218
|
+
*/
|
|
219
|
+
call(actionType, ...params) {
|
|
220
|
+
const handler = this.getAction(actionType);
|
|
221
|
+
if (!handler) {
|
|
222
|
+
throw new Error(__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, actionType)
|
|
223
|
+
? `A handler for ${actionType} has not been registered`
|
|
224
|
+
: `A handler for ${actionType} has not been delegated to ${__classPrivateFieldGet(this, _Messenger_namespace, "f")}`);
|
|
225
|
+
}
|
|
226
|
+
return handler(...params);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Register a function for getting the initial payload for an event.
|
|
230
|
+
*
|
|
231
|
+
* This is used for events that represent a state change, where the payload is the state.
|
|
232
|
+
* Registering a function for getting the payload allows event selectors to have a point of
|
|
233
|
+
* comparison the first time state changes.
|
|
234
|
+
*
|
|
235
|
+
* The event type must be under the same namespace as the messenger.
|
|
236
|
+
*
|
|
237
|
+
* @param args - The arguments to this function
|
|
238
|
+
* @param args.eventType - The event type to register a payload for.
|
|
239
|
+
* @param args.getPayload - A function for retrieving the event payload.
|
|
240
|
+
* @template EventType - A type union of Event type strings under this messenger's namespace.
|
|
241
|
+
*/
|
|
242
|
+
registerInitialEventPayload({ eventType, getPayload, }) {
|
|
243
|
+
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, eventType)) {
|
|
244
|
+
throw new Error(`Only allowed registering initial payloads for events prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);
|
|
245
|
+
}
|
|
246
|
+
if (__classPrivateFieldGet(this, _Messenger_parent, "f") &&
|
|
247
|
+
!__classPrivateFieldGet(this, _Messenger_subscriptionDelegationTargets, "f").get(eventType)?.has(__classPrivateFieldGet(this, _Messenger_parent, "f"))) {
|
|
248
|
+
// @ts-expect-error The parent type isn't constructed in a way that proves it supports this
|
|
249
|
+
// event, but this is OK because it's validated in the constructor.
|
|
250
|
+
this.delegate({ events: [eventType], messenger: __classPrivateFieldGet(this, _Messenger_parent, "f") });
|
|
251
|
+
}
|
|
252
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_registerInitialEventPayload).call(this, { eventType, getPayload });
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Publish an event.
|
|
256
|
+
*
|
|
257
|
+
* Publishes the given payload to all subscribers of the given event type.
|
|
258
|
+
*
|
|
259
|
+
* Note that this method should never throw directly. Any errors from
|
|
260
|
+
* subscribers are captured and re-thrown in a timeout handler.
|
|
261
|
+
*
|
|
262
|
+
* The event being published must be under the same namespace as the messenger.
|
|
263
|
+
*
|
|
264
|
+
* @param eventType - The event type. This is a unique identifier for this event.
|
|
265
|
+
* @param payload - The event payload. The type of the parameters for each event handler must
|
|
266
|
+
* match the type of this payload.
|
|
267
|
+
* @template EventType - A type union of Event type strings under this messenger's namespace.
|
|
268
|
+
*/
|
|
269
|
+
publish(eventType, ...payload) {
|
|
270
|
+
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, eventType)) {
|
|
271
|
+
throw new Error(`Only allowed publishing events prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);
|
|
272
|
+
}
|
|
273
|
+
if (__classPrivateFieldGet(this, _Messenger_parent, "f") &&
|
|
274
|
+
!__classPrivateFieldGet(this, _Messenger_subscriptionDelegationTargets, "f").get(eventType)?.has(__classPrivateFieldGet(this, _Messenger_parent, "f"))) {
|
|
275
|
+
// @ts-expect-error The parent type isn't constructed in a way that proves it supports this
|
|
276
|
+
// event, but this is OK because it's validated in the constructor.
|
|
277
|
+
this.delegate({ events: [eventType], messenger: __classPrivateFieldGet(this, _Messenger_parent, "f") });
|
|
278
|
+
}
|
|
279
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_publish).call(this, eventType, ...payload);
|
|
280
|
+
}
|
|
281
|
+
subscribe(eventType, handler, selector) {
|
|
282
|
+
// Widen type of event handler by dropping ReturnType parameter.
|
|
283
|
+
//
|
|
284
|
+
// We need to drop it here because it's used as the parameter to the event handler, and
|
|
285
|
+
// functions in general are contravariant over the parameter type. This means the type is no
|
|
286
|
+
// longer valid once it's added to a broader type union with other handlers (because as far
|
|
287
|
+
// as TypeScript knows, we might call the handler with output from a different selector).
|
|
288
|
+
//
|
|
289
|
+
// This cast means the type system is not guaranteeing the handler is called with the matching
|
|
290
|
+
// input selector return value. The parameter types do ensure they match when `subscribe` is
|
|
291
|
+
// called, but past that point we need to make sure of that with manual review and tests
|
|
292
|
+
// instead.
|
|
293
|
+
const widenedHandler = handler;
|
|
294
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_subscribe).call(this, eventType, widenedHandler, { delegation: false, selector });
|
|
295
|
+
if (selector) {
|
|
296
|
+
const getPayload = __classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").get(eventType);
|
|
297
|
+
if (getPayload) {
|
|
298
|
+
const initialValue = selector(...getPayload());
|
|
299
|
+
__classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").set(widenedHandler, initialValue);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
subscribeOnce(eventType, handler, options) {
|
|
304
|
+
const { selector, condition } = options ?? {};
|
|
305
|
+
// Casting to unknown to handle both the code path where a selector is defined and where it is omitted.
|
|
306
|
+
const internalHandler = (...args) => {
|
|
307
|
+
if (condition &&
|
|
308
|
+
!condition(...args)) {
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
this.unsubscribe(eventType, internalHandler);
|
|
312
|
+
handler(...args);
|
|
313
|
+
};
|
|
314
|
+
this.subscribe(eventType, internalHandler, selector);
|
|
315
|
+
}
|
|
316
|
+
waitUntil(eventType, options) {
|
|
317
|
+
return new Promise((resolve) => {
|
|
318
|
+
this.subscribeOnce(eventType, (...args) => resolve(args), options);
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Unsubscribe from an event.
|
|
323
|
+
*
|
|
324
|
+
* Unregisters the given function as an event handler for the given event.
|
|
325
|
+
*
|
|
326
|
+
* @param eventType - The event type. This is a unique identifier for this event.
|
|
327
|
+
* @param handler - The event handler to unregister.
|
|
328
|
+
* @throws Will throw when the given event handler is not registered for this event.
|
|
329
|
+
* @template EventType - A type union of Event type strings.
|
|
330
|
+
* @template SelectorReturnValue - The selector return value.
|
|
331
|
+
*/
|
|
332
|
+
unsubscribe(eventType, handler) {
|
|
333
|
+
const subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
334
|
+
// Widen type of event handler by dropping ReturnType parameter.
|
|
335
|
+
//
|
|
336
|
+
// We need to drop it here because it's used as the parameter to the event handler, and
|
|
337
|
+
// functions in general are contravariant over the parameter type. This means the type is no
|
|
338
|
+
// longer valid once it's added to a broader type union with other handlers (because as far
|
|
339
|
+
// as TypeScript knows, we might call the handler with output from a different selector).
|
|
340
|
+
//
|
|
341
|
+
// This poses no risk in this case, since we never call the handler past this point.
|
|
342
|
+
const widenedHandler = handler;
|
|
343
|
+
if (!subscribers) {
|
|
344
|
+
throw new Error(`Subscription not found for event: ${eventType}`);
|
|
345
|
+
}
|
|
346
|
+
const metadata = subscribers.get(widenedHandler);
|
|
347
|
+
if (!metadata) {
|
|
348
|
+
throw new Error(`Subscription not found for event: ${eventType}`);
|
|
349
|
+
}
|
|
350
|
+
if (metadata.selector) {
|
|
351
|
+
__classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").delete(widenedHandler);
|
|
352
|
+
}
|
|
353
|
+
subscribers.delete(widenedHandler);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Clear subscriptions for a specific event.
|
|
357
|
+
*
|
|
358
|
+
* This will remove all subscribed handlers for this event registered from this messenger. The
|
|
359
|
+
* event may still have subscribers if it has been delegated to another messenger.
|
|
360
|
+
*
|
|
361
|
+
* @param eventType - The event type. This is a unique identifier for this event.
|
|
362
|
+
* @template EventType - A type union of Event type strings.
|
|
363
|
+
*/
|
|
364
|
+
clearEventSubscriptions(eventType) {
|
|
365
|
+
const subscriptions = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
366
|
+
if (!subscriptions) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
for (const [handler, metadata] of subscriptions.entries()) {
|
|
370
|
+
if (metadata.delegation) {
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
subscriptions.delete(handler);
|
|
374
|
+
}
|
|
375
|
+
if (subscriptions.size === 0) {
|
|
376
|
+
__classPrivateFieldGet(this, _Messenger_events, "f").delete(eventType);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Clear all subscriptions.
|
|
381
|
+
*
|
|
382
|
+
* This will remove all subscribed handlers for all events registered from this messenger. Events
|
|
383
|
+
* may still have subscribers if they are delegated to another messenger.
|
|
384
|
+
*/
|
|
385
|
+
clearSubscriptions() {
|
|
386
|
+
for (const eventType of __classPrivateFieldGet(this, _Messenger_events, "f").keys()) {
|
|
387
|
+
this.clearEventSubscriptions(eventType);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Delegate actions and/or events to another messenger.
|
|
392
|
+
*
|
|
393
|
+
* The messenger these actions/events are delegated to will be able to call these actions and
|
|
394
|
+
* subscribe to these events.
|
|
395
|
+
*
|
|
396
|
+
* Note that the messenger these actions/events are delegated to must still have these
|
|
397
|
+
* actions/events included in its type definition (as part of the Action and Event type
|
|
398
|
+
* parameters). Actions and events are statically type checked, they cannot be delegated
|
|
399
|
+
* dynamically at runtime.
|
|
400
|
+
*
|
|
401
|
+
* @param args - Arguments.
|
|
402
|
+
* @param args.actions - The action types to delegate.
|
|
403
|
+
* @param args.events - The event types to delegate.
|
|
404
|
+
* @param args.messenger - The messenger to delegate to.
|
|
405
|
+
* @template Delegatee - The messenger the actions/events are delegated to.
|
|
406
|
+
* @template DelegatedActions - An array of delegated action types.
|
|
407
|
+
* @template DelegatedEvents - An array of delegated event types.
|
|
408
|
+
*/
|
|
409
|
+
delegate({ actions, events, messenger, }) {
|
|
410
|
+
for (const actionType of actions ?? []) {
|
|
411
|
+
const delegatedActionHandler = (...args) => {
|
|
412
|
+
// Cast to get more specific type, for this specific action
|
|
413
|
+
// The types get collapsed by `this.#actions`
|
|
414
|
+
const actionHandler = this.getAction(actionType);
|
|
415
|
+
if (!actionHandler) {
|
|
416
|
+
throw new Error(`A handler for ${actionType} has not been registered`);
|
|
417
|
+
}
|
|
418
|
+
return actionHandler(...args);
|
|
419
|
+
};
|
|
420
|
+
let delegationTargets = __classPrivateFieldGet(this, _Messenger_actionDelegationTargets, "f").get(actionType);
|
|
421
|
+
if (!delegationTargets) {
|
|
422
|
+
delegationTargets = new Set();
|
|
423
|
+
__classPrivateFieldGet(this, _Messenger_actionDelegationTargets, "f").set(actionType, delegationTargets);
|
|
424
|
+
}
|
|
425
|
+
if (delegationTargets.has(messenger)) {
|
|
426
|
+
throw new Error(`The action '${actionType}' has already been delegated to this messenger`);
|
|
427
|
+
}
|
|
428
|
+
delegationTargets.add(messenger);
|
|
429
|
+
messenger._internalRegisterDelegatedActionHandler(actionType, delegatedActionHandler);
|
|
430
|
+
}
|
|
431
|
+
for (const eventType of events ?? []) {
|
|
432
|
+
const untypedSubscriber = (...payload) => {
|
|
433
|
+
messenger._internalPublishDelegated(eventType, ...payload);
|
|
434
|
+
};
|
|
435
|
+
// Cast to get more specific subscriber type for this specific event.
|
|
436
|
+
// The types get collapsed here to the type union of all delegated
|
|
437
|
+
// events, rather than the single subscriber type corresponding to this
|
|
438
|
+
// event.
|
|
439
|
+
const subscriber = untypedSubscriber;
|
|
440
|
+
let delegatedEventSubscriptions = __classPrivateFieldGet(this, _Messenger_subscriptionDelegationTargets, "f").get(eventType);
|
|
441
|
+
if (!delegatedEventSubscriptions) {
|
|
442
|
+
delegatedEventSubscriptions = new Map();
|
|
443
|
+
__classPrivateFieldGet(this, _Messenger_subscriptionDelegationTargets, "f").set(eventType, delegatedEventSubscriptions);
|
|
444
|
+
}
|
|
445
|
+
if (delegatedEventSubscriptions.has(messenger)) {
|
|
446
|
+
throw new Error(`The event '${eventType}' has already been delegated to this messenger`);
|
|
447
|
+
}
|
|
448
|
+
delegatedEventSubscriptions.set(messenger, subscriber);
|
|
449
|
+
const getPayload = __classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").get(eventType);
|
|
450
|
+
if (getPayload) {
|
|
451
|
+
messenger._internalRegisterDelegatedInitialEventPayload({
|
|
452
|
+
eventType,
|
|
453
|
+
getPayload,
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_subscribe).call(this, eventType, subscriber, { delegation: true });
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Delegate all external actions and events to another messenger, with
|
|
461
|
+
* compile-time exhaustiveness checking.
|
|
462
|
+
*
|
|
463
|
+
* Unlike {@link delegate}, which accepts a partial list of actions/events,
|
|
464
|
+
* this method requires that **every** action and event the delegatee needs
|
|
465
|
+
* from outside its own namespace is included. If any are missing, TypeScript
|
|
466
|
+
* produces a type error showing the missing items.
|
|
467
|
+
*
|
|
468
|
+
* The source messenger's action/event types must include every required
|
|
469
|
+
* external item. Items the source cannot provide still appear in the
|
|
470
|
+
* missing set, so incomplete source typing fails loudly instead of being
|
|
471
|
+
* silently skipped.
|
|
472
|
+
*
|
|
473
|
+
* Use this when a single source messenger provides all external
|
|
474
|
+
* actions/events for a child messenger (the common pattern in controller
|
|
475
|
+
* initialisation).
|
|
476
|
+
*
|
|
477
|
+
* @param args - Arguments.
|
|
478
|
+
* @param args.actions - The action types to delegate. Must include every
|
|
479
|
+
* action type defined on the delegatee that is **not** under its own
|
|
480
|
+
* namespace.
|
|
481
|
+
* @param args.events - The event types to delegate. Must include every event
|
|
482
|
+
* type defined on the delegatee that is **not** under its own namespace.
|
|
483
|
+
* @param args.messenger - The messenger to delegate to.
|
|
484
|
+
* @template Delegatee - The messenger the actions/events are delegated to.
|
|
485
|
+
* @template DelegatedActions - An array of delegated action type strings.
|
|
486
|
+
* @template DelegatedEvents - An array of delegated event type strings.
|
|
487
|
+
*/
|
|
488
|
+
delegateAll({ actions, events, messenger, }) {
|
|
489
|
+
this.delegate({ actions, events, messenger });
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Revoke delegated actions and/or events from another messenger.
|
|
493
|
+
*
|
|
494
|
+
* The messenger these actions/events are delegated to will no longer be able to call these
|
|
495
|
+
* actions or subscribe to these events.
|
|
496
|
+
*
|
|
497
|
+
* @param args - Arguments.
|
|
498
|
+
* @param args.actions - The action types to revoke.
|
|
499
|
+
* @param args.events - The event types to revoke.
|
|
500
|
+
* @param args.messenger - The messenger these actions/events were delegated to.
|
|
501
|
+
* @template Delegatee - The messenger the actions/events are being revoked from.
|
|
502
|
+
* @template DelegatedActions - An array of delegated action types.
|
|
503
|
+
* @template DelegatedEvents - An array of delegated event types.
|
|
504
|
+
*/
|
|
505
|
+
revoke({ actions, events, messenger, }) {
|
|
506
|
+
if (messenger === __classPrivateFieldGet(this, _Messenger_parent, "f")) {
|
|
507
|
+
throw new Error('Cannot revoke from parent');
|
|
508
|
+
}
|
|
509
|
+
for (const actionType of actions ?? []) {
|
|
510
|
+
const delegationTargets = __classPrivateFieldGet(this, _Messenger_actionDelegationTargets, "f").get(actionType);
|
|
511
|
+
if (!delegationTargets?.has(messenger)) {
|
|
512
|
+
// Nothing to revoke
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
messenger._internalUnregisterDelegatedActionHandler(actionType);
|
|
516
|
+
delegationTargets.delete(messenger);
|
|
517
|
+
if (delegationTargets.size === 0) {
|
|
518
|
+
__classPrivateFieldGet(this, _Messenger_actionDelegationTargets, "f").delete(actionType);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
for (const eventType of events ?? []) {
|
|
522
|
+
const delegationTargets = __classPrivateFieldGet(this, _Messenger_subscriptionDelegationTargets, "f").get(eventType);
|
|
523
|
+
if (!delegationTargets) {
|
|
524
|
+
// Nothing to revoke
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
const delegatedSubscriber = delegationTargets.get(messenger);
|
|
528
|
+
if (!delegatedSubscriber) {
|
|
529
|
+
// Nothing to revoke
|
|
530
|
+
continue;
|
|
531
|
+
}
|
|
532
|
+
this.unsubscribe(eventType, delegatedSubscriber);
|
|
533
|
+
delegationTargets.delete(messenger);
|
|
534
|
+
if (delegationTargets.size === 0) {
|
|
535
|
+
__classPrivateFieldGet(this, _Messenger_subscriptionDelegationTargets, "f").delete(eventType);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Register an action handler for an action delegated from another messenger.
|
|
541
|
+
*
|
|
542
|
+
* This will make the registered function available to call via the `call` method.
|
|
543
|
+
*
|
|
544
|
+
* Note: This is an internal method. Never access this property from another module. This must be
|
|
545
|
+
* exposed as a public property so that these methods can be called internally on other messenger
|
|
546
|
+
* instances.
|
|
547
|
+
*
|
|
548
|
+
* @deprecated Internal use only. Use the `delegate` method for delegation.
|
|
549
|
+
* @param actionType - The action type. This is a unique identifier for this action.
|
|
550
|
+
* @param handler - The action handler. This function gets called when the `call` method is
|
|
551
|
+
* invoked with the given action type.
|
|
552
|
+
* @throws Will throw when a handler has been registered for this action type already.
|
|
553
|
+
* @template ActionType - A type union of Action type strings.
|
|
554
|
+
*/
|
|
555
|
+
_internalRegisterDelegatedActionHandler(actionType,
|
|
556
|
+
// Using wider `ActionConstraint` type here rather than `Action` because the `Action` type is
|
|
557
|
+
// contravariant over the handler parameter type. Using `Action` would lead to a type error
|
|
558
|
+
// here because the messenger we've delegated to supports _additional_ actions.
|
|
559
|
+
handler) {
|
|
560
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_registerActionHandler).call(this, actionType, handler);
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Unregister an action handler for an action delegated from another messenger.
|
|
564
|
+
*
|
|
565
|
+
* This will prevent this action from being called.
|
|
566
|
+
*
|
|
567
|
+
* Note: This is an internal method. Never access this property from another module. This must be
|
|
568
|
+
* exposed as a public property so that these methods can be called internally on other messenger
|
|
569
|
+
* instances.
|
|
570
|
+
*
|
|
571
|
+
* @deprecated Internal use only. Use the `delegate` method for delegation.
|
|
572
|
+
* @param actionType - The action type. This is a unqiue identifier for this action.
|
|
573
|
+
* @template ActionType - A type union of Action type strings.
|
|
574
|
+
*/
|
|
575
|
+
_internalUnregisterDelegatedActionHandler(actionType) {
|
|
576
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_unregisterActionHandler).call(this, actionType);
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Register a function for getting the initial payload for an event that has been delegated from
|
|
580
|
+
* another messenger.
|
|
581
|
+
*
|
|
582
|
+
* This is used for events that represent a state change, where the payload is the state.
|
|
583
|
+
* Registering a function for getting the payload allows event selectors to have a point of
|
|
584
|
+
* comparison the first time state changes.
|
|
585
|
+
*
|
|
586
|
+
* Note: This is an internal method. Never access this property from another module. This must be
|
|
587
|
+
* exposed as a public property so that these methods can be called internally on other messenger
|
|
588
|
+
* instances.
|
|
589
|
+
*
|
|
590
|
+
* @deprecated Internal use only. Use the `delegate` method for delegation.
|
|
591
|
+
* @param args - The arguments to this function
|
|
592
|
+
* @param args.eventType - The event type to register a payload for.
|
|
593
|
+
* @param args.getPayload - A function for retrieving the event payload.
|
|
594
|
+
*/
|
|
595
|
+
_internalRegisterDelegatedInitialEventPayload({ eventType, getPayload, }) {
|
|
596
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_registerInitialEventPayload).call(this, { eventType, getPayload });
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Publish an event that was delegated from another messenger.
|
|
600
|
+
*
|
|
601
|
+
* Publishes the given payload to all subscribers of the given event type.
|
|
602
|
+
*
|
|
603
|
+
* Note that this method should never throw directly. Any errors from
|
|
604
|
+
* subscribers are captured and re-thrown in a timeout handler.
|
|
605
|
+
*
|
|
606
|
+
* Note: This is an internal method. Never access this property from another module. This must be
|
|
607
|
+
* exposed as a public property so that these methods can be called internally on other messenger
|
|
608
|
+
* instances.
|
|
609
|
+
*
|
|
610
|
+
* @deprecated Internal use only. Use the `delegate` method for delegation.
|
|
611
|
+
* @param eventType - The event type. This is a unique identifier for this event.
|
|
612
|
+
* @param payload - The event payload. The type of the parameters for each event handler must
|
|
613
|
+
* match the type of this payload.
|
|
614
|
+
* @template EventType - A type union of Event type strings.
|
|
615
|
+
*/
|
|
616
|
+
_internalPublishDelegated(eventType, ...payload) {
|
|
617
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_publish).call(this, eventType, ...payload);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
exports.Messenger = Messenger;
|
|
621
|
+
_Messenger_namespace = new WeakMap(), _Messenger_parent = new WeakMap(), _Messenger_actions = new WeakMap(), _Messenger_events = new WeakMap(), _Messenger_deferredPublishes = new WeakMap(), _Messenger_subscriptionDelegationTargets = new WeakMap(), _Messenger_actionDelegationTargets = new WeakMap(), _Messenger_initialEventPayloadGetters = new WeakMap(), _Messenger_eventPayloadCache = new WeakMap(), _Messenger_instances = new WeakSet(), _Messenger_registerActionHandler = function _Messenger_registerActionHandler(actionType, handler) {
|
|
622
|
+
if (__classPrivateFieldGet(this, _Messenger_actions, "f").has(actionType)) {
|
|
623
|
+
throw new Error(`A handler for ${actionType} has already been registered`);
|
|
624
|
+
}
|
|
625
|
+
__classPrivateFieldGet(this, _Messenger_actions, "f").set(actionType, handler);
|
|
626
|
+
}, _Messenger_unregisterActionHandler = function _Messenger_unregisterActionHandler(actionType) {
|
|
627
|
+
__classPrivateFieldGet(this, _Messenger_actions, "f").delete(actionType);
|
|
628
|
+
}, _Messenger_registerInitialEventPayload = function _Messenger_registerInitialEventPayload({ eventType, getPayload, }) {
|
|
629
|
+
__classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").set(eventType, getPayload);
|
|
630
|
+
const delegationTargets = __classPrivateFieldGet(this, _Messenger_subscriptionDelegationTargets, "f").get(eventType);
|
|
631
|
+
if (!delegationTargets) {
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
for (const messenger of delegationTargets.keys()) {
|
|
635
|
+
messenger._internalRegisterDelegatedInitialEventPayload({
|
|
636
|
+
eventType,
|
|
637
|
+
getPayload,
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
}, _Messenger_publish = function _Messenger_publish(eventType, ...payload) {
|
|
641
|
+
// Defer a re-entrant publish of the same event (e.g. a subscriber that
|
|
642
|
+
// publishes the event it is handling). Delivering it inline would let the
|
|
643
|
+
// in-progress publish resume and re-deliver its now-stale payload to the
|
|
644
|
+
// subscribers it had not reached yet.
|
|
645
|
+
const inProgress = __classPrivateFieldGet(this, _Messenger_deferredPublishes, "f").get(eventType);
|
|
646
|
+
if (inProgress) {
|
|
647
|
+
inProgress.push(() => __classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_deliverToSubscribers).call(this, eventType, ...payload));
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
const deferred = [];
|
|
651
|
+
__classPrivateFieldGet(this, _Messenger_deferredPublishes, "f").set(eventType, deferred);
|
|
652
|
+
try {
|
|
653
|
+
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_deliverToSubscribers).call(this, eventType, ...payload);
|
|
654
|
+
// Drain deferred publishes in order. The array grows as further
|
|
655
|
+
// re-entrant publishes push onto it; the iterator reads those too.
|
|
656
|
+
for (const run of deferred) {
|
|
657
|
+
run();
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
finally {
|
|
661
|
+
__classPrivateFieldGet(this, _Messenger_deferredPublishes, "f").delete(eventType);
|
|
662
|
+
}
|
|
663
|
+
}, _Messenger_deliverToSubscribers = function _Messenger_deliverToSubscribers(eventType, ...payload) {
|
|
664
|
+
const subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
665
|
+
if (subscribers) {
|
|
666
|
+
for (const [handler, { selector }] of subscribers.entries()) {
|
|
667
|
+
try {
|
|
668
|
+
if (selector) {
|
|
669
|
+
const previousValue = __classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").get(handler);
|
|
670
|
+
const newValue = selector(...payload);
|
|
671
|
+
if (newValue !== previousValue) {
|
|
672
|
+
__classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").set(handler, newValue);
|
|
673
|
+
handler(newValue, previousValue);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
else {
|
|
677
|
+
handler(...payload);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
catch (error) {
|
|
681
|
+
// Capture error without interrupting the event publishing.
|
|
682
|
+
if (this.captureException) {
|
|
683
|
+
this.captureException(error instanceof Error ? error : new Error(String(error)));
|
|
684
|
+
}
|
|
685
|
+
else {
|
|
686
|
+
console.error(error);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}, _Messenger_subscribe = function _Messenger_subscribe(eventType, handler, metadata) {
|
|
692
|
+
let subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
693
|
+
if (!subscribers) {
|
|
694
|
+
subscribers = new Map();
|
|
695
|
+
__classPrivateFieldGet(this, _Messenger_events, "f").set(eventType, subscribers);
|
|
696
|
+
}
|
|
697
|
+
subscribers.set(handler, metadata);
|
|
698
|
+
}, _Messenger_isInCurrentNamespace = function _Messenger_isInCurrentNamespace(name) {
|
|
699
|
+
return (__classPrivateFieldGet(this, _Messenger_namespace, "f") === exports.MOCK_ANY_NAMESPACE ||
|
|
700
|
+
name.startsWith(`${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:`));
|
|
701
|
+
};
|
|
702
|
+
//# sourceMappingURL=Messenger.cjs.map
|