@metamask-previews/messenger 2.0.0-preview-abca9bcea → 3.0.0-preview-0a30e47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -1
- package/dist/{Messenger.d.mts → Messenger.d.ts} +1 -1
- package/dist/Messenger.d.ts.map +1 -0
- package/dist/{Messenger.mjs → Messenger.js} +196 -179
- package/dist/Messenger.js.map +1 -0
- package/dist/{index.d.cts → index.d.ts} +3 -3
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +13 -17
- package/dist/Messenger.cjs +0 -702
- package/dist/Messenger.cjs.map +0 -1
- package/dist/Messenger.d.cts +0 -587
- package/dist/Messenger.d.cts.map +0 -1
- package/dist/Messenger.d.mts.map +0 -1
- package/dist/Messenger.mjs.map +0 -1
- package/dist/index.cjs +0 -7
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts +0 -3
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -2
- package/dist/index.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [3.0.0]
|
|
11
|
+
|
|
10
12
|
### Added
|
|
11
13
|
|
|
12
14
|
- Add `delegateAll` method for exhaustive delegation with compile-time checking ([#8338](https://github.com/MetaMask/core/pull/8338))
|
|
@@ -15,6 +17,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
15
17
|
|
|
16
18
|
### Changed
|
|
17
19
|
|
|
20
|
+
- **BREAKING:** Drop CommonJS support ([#9536](https://github.com/MetaMask/core/pull/9536))
|
|
21
|
+
- This package is now ESM-only, but can still be used in CommonJS projects via `require(esm)` in modern Node.js versions (22+), or dynamic imports in older Node.js versions.
|
|
22
|
+
- **BREAKING:** Bump minimum Node.js version to 22 ([#9976](https://github.com/MetaMask/core/pull/9976))
|
|
23
|
+
- **BREAKING:** Bump TypeScript target to ES2022 ([#10019](https://github.com/MetaMask/core/pull/10019))
|
|
24
|
+
- This package now ships ES2022 code, requiring a compatible modern environment or bundler configuration to consume.
|
|
18
25
|
- Bump `@metamask/utils` from `^11.11.0` to `^11.12.0` ([#10076](https://github.com/MetaMask/core/pull/10076))
|
|
19
26
|
|
|
20
27
|
### Fixed
|
|
@@ -131,7 +138,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
131
138
|
- Existing `RestrictedMessenger` instances should be replaced with a `Messenger` with the `parent` constructor parameter set to the global messenger. We can now use the same class everywhere, passing capabilities using `delegate`.
|
|
132
139
|
- See this ADR for details: https://github.com/MetaMask/decisions/blob/main/decisions/core/0012-messenger-delegation.md
|
|
133
140
|
|
|
134
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/messenger@
|
|
141
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/messenger@3.0.0...HEAD
|
|
142
|
+
[3.0.0]: https://github.com/MetaMask/core/compare/@metamask/messenger@2.0.0...@metamask/messenger@3.0.0
|
|
135
143
|
[2.0.0]: https://github.com/MetaMask/core/compare/@metamask/messenger@1.2.0...@metamask/messenger@2.0.0
|
|
136
144
|
[1.2.0]: https://github.com/MetaMask/core/compare/@metamask/messenger@1.1.1...@metamask/messenger@1.2.0
|
|
137
145
|
[1.1.1]: https://github.com/MetaMask/core/compare/@metamask/messenger@1.1.0...@metamask/messenger@1.1.1
|
|
@@ -584,4 +584,4 @@ export declare class Messenger<Namespace extends string, Action extends ActionCo
|
|
|
584
584
|
_internalPublishDelegated<EventType extends Event['type']>(eventType: EventType, ...payload: ExtractEventPayload<Event, EventType>): void;
|
|
585
585
|
}
|
|
586
586
|
export {};
|
|
587
|
-
//# sourceMappingURL=Messenger.d.
|
|
587
|
+
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G,IAAI,CAEP;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,yBAAyB,CAAC,SAAS,SAAS,KAAK,CAAC,MAAM,CAAC,EACvD,SAAS,EAAE,SAAS,EACpB,GAAG,OAAO,EAAE,mBAAmB,CAAC,KAAK,EAAE,SAAS,CAAC,GAChD,IAAI,CAEN;CAgBF"}
|
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
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");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
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;
|
|
13
1
|
/**
|
|
14
2
|
* Messenger namespace checks can be disabled by using this as the `namespace` constructor
|
|
15
3
|
* parameter, and using `MockAnyNamespace` as the Namespace type parameter.
|
|
@@ -31,6 +19,44 @@ export const MOCK_ANY_NAMESPACE = 'MOCK_ANY_NAMESPACE';
|
|
|
31
19
|
* @template Namespace - The namespace for the messenger.
|
|
32
20
|
*/
|
|
33
21
|
export class Messenger {
|
|
22
|
+
#namespace;
|
|
23
|
+
/**
|
|
24
|
+
* The parent messenger. All actions/events under this namespace are automatically delegated to
|
|
25
|
+
* the parent messenger.
|
|
26
|
+
*/
|
|
27
|
+
#parent;
|
|
28
|
+
#actions = new Map();
|
|
29
|
+
#events = new Map();
|
|
30
|
+
/**
|
|
31
|
+
* In-progress publishes, keyed by event type. A key is present for the
|
|
32
|
+
* duration of a publish (so presence means "publishing"); its array collects
|
|
33
|
+
* re-entrant publishes of that event, drained when the publish finishes.
|
|
34
|
+
*/
|
|
35
|
+
#deferredPublishes = new Map();
|
|
36
|
+
/**
|
|
37
|
+
* The set of messengers we've delegated events to and their event handlers, by event type.
|
|
38
|
+
*/
|
|
39
|
+
#subscriptionDelegationTargets = new Map();
|
|
40
|
+
/**
|
|
41
|
+
* The set of messengers we've delegated actions to, by action type.
|
|
42
|
+
*/
|
|
43
|
+
#actionDelegationTargets = new Map();
|
|
44
|
+
/**
|
|
45
|
+
* A map of functions for getting the initial event payload.
|
|
46
|
+
*
|
|
47
|
+
* Used only for events that represent state changes.
|
|
48
|
+
*/
|
|
49
|
+
#initialEventPayloadGetters = new Map();
|
|
50
|
+
/**
|
|
51
|
+
* A cache of selector return values for their respective handlers.
|
|
52
|
+
*/
|
|
53
|
+
#eventPayloadCache = new Map();
|
|
54
|
+
/**
|
|
55
|
+
* Reports an error to an error monitoring service.
|
|
56
|
+
*
|
|
57
|
+
* @param error - The error to report.
|
|
58
|
+
*/
|
|
59
|
+
captureException;
|
|
34
60
|
/**
|
|
35
61
|
* Construct a messenger.
|
|
36
62
|
*
|
|
@@ -43,42 +69,9 @@ export class Messenger {
|
|
|
43
69
|
* @param args.parent - The parent messenger.
|
|
44
70
|
*/
|
|
45
71
|
constructor({ captureException, namespace, parent, }) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
* The parent messenger. All actions/events under this namespace are automatically delegated to
|
|
50
|
-
* the parent messenger.
|
|
51
|
-
*/
|
|
52
|
-
_Messenger_parent.set(this, void 0);
|
|
53
|
-
_Messenger_actions.set(this, new Map());
|
|
54
|
-
_Messenger_events.set(this, new Map());
|
|
55
|
-
/**
|
|
56
|
-
* In-progress publishes, keyed by event type. A key is present for the
|
|
57
|
-
* duration of a publish (so presence means "publishing"); its array collects
|
|
58
|
-
* re-entrant publishes of that event, drained when the publish finishes.
|
|
59
|
-
*/
|
|
60
|
-
_Messenger_deferredPublishes.set(this, new Map());
|
|
61
|
-
/**
|
|
62
|
-
* The set of messengers we've delegated events to and their event handlers, by event type.
|
|
63
|
-
*/
|
|
64
|
-
_Messenger_subscriptionDelegationTargets.set(this, new Map());
|
|
65
|
-
/**
|
|
66
|
-
* The set of messengers we've delegated actions to, by action type.
|
|
67
|
-
*/
|
|
68
|
-
_Messenger_actionDelegationTargets.set(this, new Map());
|
|
69
|
-
/**
|
|
70
|
-
* A map of functions for getting the initial event payload.
|
|
71
|
-
*
|
|
72
|
-
* Used only for events that represent state changes.
|
|
73
|
-
*/
|
|
74
|
-
_Messenger_initialEventPayloadGetters.set(this, new Map());
|
|
75
|
-
/**
|
|
76
|
-
* A cache of selector return values for their respective handlers.
|
|
77
|
-
*/
|
|
78
|
-
_Messenger_eventPayloadCache.set(this, new Map());
|
|
79
|
-
__classPrivateFieldSet(this, _Messenger_namespace, namespace, "f");
|
|
80
|
-
__classPrivateFieldSet(this, _Messenger_parent, parent, "f");
|
|
81
|
-
this.captureException = captureException ?? __classPrivateFieldGet(this, _Messenger_parent, "f")?.captureException;
|
|
72
|
+
this.#namespace = namespace;
|
|
73
|
+
this.#parent = parent;
|
|
74
|
+
this.captureException = captureException ?? this.#parent?.captureException;
|
|
82
75
|
}
|
|
83
76
|
/**
|
|
84
77
|
* Register an action handler.
|
|
@@ -94,15 +87,21 @@ export class Messenger {
|
|
|
94
87
|
* @template ActionType - A type union of Action type strings under this messenger's namespace.
|
|
95
88
|
*/
|
|
96
89
|
registerActionHandler(actionType, handler) {
|
|
97
|
-
if (!
|
|
98
|
-
throw new Error(`Only allowed registering action handlers prefixed by '${
|
|
90
|
+
if (!this.#isInCurrentNamespace(actionType)) {
|
|
91
|
+
throw new Error(`Only allowed registering action handlers prefixed by '${this.#namespace}:'`);
|
|
99
92
|
}
|
|
100
|
-
|
|
101
|
-
if (
|
|
93
|
+
this.#registerActionHandler(actionType, handler);
|
|
94
|
+
if (this.#parent) {
|
|
102
95
|
// @ts-expect-error The parent type isn't constructed in a way that proves it supports this
|
|
103
96
|
// action, but this is OK because it's validated in the constructor.
|
|
104
|
-
this.delegate({ actions: [actionType], messenger:
|
|
97
|
+
this.delegate({ actions: [actionType], messenger: this.#parent });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
#registerActionHandler(actionType, handler) {
|
|
101
|
+
if (this.#actions.has(actionType)) {
|
|
102
|
+
throw new Error(`A handler for ${actionType} has already been registered`);
|
|
105
103
|
}
|
|
104
|
+
this.#actions.set(actionType, handler);
|
|
106
105
|
}
|
|
107
106
|
/**
|
|
108
107
|
* Registers action handlers for a list of methods on a messenger client
|
|
@@ -133,10 +132,13 @@ export class Messenger {
|
|
|
133
132
|
* @template ActionType - A type union of Action type strings under this messenger's namespace.
|
|
134
133
|
*/
|
|
135
134
|
unregisterActionHandler(actionType) {
|
|
136
|
-
if (!
|
|
137
|
-
throw new Error(`Only allowed unregistering action handlers prefixed by '${
|
|
135
|
+
if (!this.#isInCurrentNamespace(actionType)) {
|
|
136
|
+
throw new Error(`Only allowed unregistering action handlers prefixed by '${this.#namespace}:'`);
|
|
138
137
|
}
|
|
139
|
-
|
|
138
|
+
this.#unregisterActionHandler(actionType);
|
|
139
|
+
}
|
|
140
|
+
#unregisterActionHandler(actionType) {
|
|
141
|
+
this.#actions.delete(actionType);
|
|
140
142
|
}
|
|
141
143
|
/**
|
|
142
144
|
* Unregister all action handlers.
|
|
@@ -144,8 +146,8 @@ export class Messenger {
|
|
|
144
146
|
* This prevents all actions from being called.
|
|
145
147
|
*/
|
|
146
148
|
clearActions() {
|
|
147
|
-
for (const actionType of
|
|
148
|
-
|
|
149
|
+
for (const actionType of this.#actions.keys()) {
|
|
150
|
+
this.#unregisterActionHandler(actionType);
|
|
149
151
|
}
|
|
150
152
|
}
|
|
151
153
|
/**
|
|
@@ -160,7 +162,7 @@ export class Messenger {
|
|
|
160
162
|
* @returns An array of every action type this messenger can call directly.
|
|
161
163
|
*/
|
|
162
164
|
getRegisteredActionTypes() {
|
|
163
|
-
return [...
|
|
165
|
+
return [...this.#actions.keys()];
|
|
164
166
|
}
|
|
165
167
|
/**
|
|
166
168
|
* Get the action handler for a given action type.
|
|
@@ -174,7 +176,7 @@ export class Messenger {
|
|
|
174
176
|
* handler has been registered.
|
|
175
177
|
*/
|
|
176
178
|
getAction(actionType) {
|
|
177
|
-
return
|
|
179
|
+
return this.#actions.get(actionType);
|
|
178
180
|
}
|
|
179
181
|
/**
|
|
180
182
|
* Create a new messenger as a child of this messenger (the "parent").
|
|
@@ -216,9 +218,9 @@ export class Messenger {
|
|
|
216
218
|
call(actionType, ...params) {
|
|
217
219
|
const handler = this.getAction(actionType);
|
|
218
220
|
if (!handler) {
|
|
219
|
-
throw new Error(
|
|
221
|
+
throw new Error(this.#isInCurrentNamespace(actionType)
|
|
220
222
|
? `A handler for ${actionType} has not been registered`
|
|
221
|
-
: `A handler for ${actionType} has not been delegated to ${
|
|
223
|
+
: `A handler for ${actionType} has not been delegated to ${this.#namespace}`);
|
|
222
224
|
}
|
|
223
225
|
return handler(...params);
|
|
224
226
|
}
|
|
@@ -237,16 +239,29 @@ export class Messenger {
|
|
|
237
239
|
* @template EventType - A type union of Event type strings under this messenger's namespace.
|
|
238
240
|
*/
|
|
239
241
|
registerInitialEventPayload({ eventType, getPayload, }) {
|
|
240
|
-
if (!
|
|
241
|
-
throw new Error(`Only allowed registering initial payloads for events prefixed by '${
|
|
242
|
+
if (!this.#isInCurrentNamespace(eventType)) {
|
|
243
|
+
throw new Error(`Only allowed registering initial payloads for events prefixed by '${this.#namespace}:'`);
|
|
242
244
|
}
|
|
243
|
-
if (
|
|
244
|
-
!
|
|
245
|
+
if (this.#parent &&
|
|
246
|
+
!this.#subscriptionDelegationTargets.get(eventType)?.has(this.#parent)) {
|
|
245
247
|
// @ts-expect-error The parent type isn't constructed in a way that proves it supports this
|
|
246
248
|
// event, but this is OK because it's validated in the constructor.
|
|
247
|
-
this.delegate({ events: [eventType], messenger:
|
|
249
|
+
this.delegate({ events: [eventType], messenger: this.#parent });
|
|
250
|
+
}
|
|
251
|
+
this.#registerInitialEventPayload({ eventType, getPayload });
|
|
252
|
+
}
|
|
253
|
+
#registerInitialEventPayload({ eventType, getPayload, }) {
|
|
254
|
+
this.#initialEventPayloadGetters.set(eventType, getPayload);
|
|
255
|
+
const delegationTargets = this.#subscriptionDelegationTargets.get(eventType);
|
|
256
|
+
if (!delegationTargets) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
for (const messenger of delegationTargets.keys()) {
|
|
260
|
+
messenger._internalRegisterDelegatedInitialEventPayload({
|
|
261
|
+
eventType,
|
|
262
|
+
getPayload,
|
|
263
|
+
});
|
|
248
264
|
}
|
|
249
|
-
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_registerInitialEventPayload).call(this, { eventType, getPayload });
|
|
250
265
|
}
|
|
251
266
|
/**
|
|
252
267
|
* Publish an event.
|
|
@@ -264,16 +279,69 @@ export class Messenger {
|
|
|
264
279
|
* @template EventType - A type union of Event type strings under this messenger's namespace.
|
|
265
280
|
*/
|
|
266
281
|
publish(eventType, ...payload) {
|
|
267
|
-
if (!
|
|
268
|
-
throw new Error(`Only allowed publishing events prefixed by '${
|
|
282
|
+
if (!this.#isInCurrentNamespace(eventType)) {
|
|
283
|
+
throw new Error(`Only allowed publishing events prefixed by '${this.#namespace}:'`);
|
|
269
284
|
}
|
|
270
|
-
if (
|
|
271
|
-
!
|
|
285
|
+
if (this.#parent &&
|
|
286
|
+
!this.#subscriptionDelegationTargets.get(eventType)?.has(this.#parent)) {
|
|
272
287
|
// @ts-expect-error The parent type isn't constructed in a way that proves it supports this
|
|
273
288
|
// event, but this is OK because it's validated in the constructor.
|
|
274
|
-
this.delegate({ events: [eventType], messenger:
|
|
289
|
+
this.delegate({ events: [eventType], messenger: this.#parent });
|
|
290
|
+
}
|
|
291
|
+
this.#publish(eventType, ...payload);
|
|
292
|
+
}
|
|
293
|
+
#publish(eventType, ...payload) {
|
|
294
|
+
// Defer a re-entrant publish of the same event (e.g. a subscriber that
|
|
295
|
+
// publishes the event it is handling). Delivering it inline would let the
|
|
296
|
+
// in-progress publish resume and re-deliver its now-stale payload to the
|
|
297
|
+
// subscribers it had not reached yet.
|
|
298
|
+
const inProgress = this.#deferredPublishes.get(eventType);
|
|
299
|
+
if (inProgress) {
|
|
300
|
+
inProgress.push(() => this.#deliverToSubscribers(eventType, ...payload));
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
const deferred = [];
|
|
304
|
+
this.#deferredPublishes.set(eventType, deferred);
|
|
305
|
+
try {
|
|
306
|
+
this.#deliverToSubscribers(eventType, ...payload);
|
|
307
|
+
// Drain deferred publishes in order. The array grows as further
|
|
308
|
+
// re-entrant publishes push onto it; the iterator reads those too.
|
|
309
|
+
for (const run of deferred) {
|
|
310
|
+
run();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
finally {
|
|
314
|
+
this.#deferredPublishes.delete(eventType);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
#deliverToSubscribers(eventType, ...payload) {
|
|
318
|
+
const subscribers = this.#events.get(eventType);
|
|
319
|
+
if (subscribers) {
|
|
320
|
+
for (const [handler, { selector }] of subscribers.entries()) {
|
|
321
|
+
try {
|
|
322
|
+
if (selector) {
|
|
323
|
+
const previousValue = this.#eventPayloadCache.get(handler);
|
|
324
|
+
const newValue = selector(...payload);
|
|
325
|
+
if (newValue !== previousValue) {
|
|
326
|
+
this.#eventPayloadCache.set(handler, newValue);
|
|
327
|
+
handler(newValue, previousValue);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
handler(...payload);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
catch (error) {
|
|
335
|
+
// Capture error without interrupting the event publishing.
|
|
336
|
+
if (this.captureException) {
|
|
337
|
+
this.captureException(error instanceof Error ? error : new Error(String(error)));
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
console.error(error);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
275
344
|
}
|
|
276
|
-
__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_publish).call(this, eventType, ...payload);
|
|
277
345
|
}
|
|
278
346
|
subscribe(eventType, handler, selector) {
|
|
279
347
|
// Widen type of event handler by dropping ReturnType parameter.
|
|
@@ -288,15 +356,33 @@ export class Messenger {
|
|
|
288
356
|
// called, but past that point we need to make sure of that with manual review and tests
|
|
289
357
|
// instead.
|
|
290
358
|
const widenedHandler = handler;
|
|
291
|
-
|
|
359
|
+
this.#subscribe(eventType, widenedHandler, { delegation: false, selector });
|
|
292
360
|
if (selector) {
|
|
293
|
-
const getPayload =
|
|
361
|
+
const getPayload = this.#initialEventPayloadGetters.get(eventType);
|
|
294
362
|
if (getPayload) {
|
|
295
363
|
const initialValue = selector(...getPayload());
|
|
296
|
-
|
|
364
|
+
this.#eventPayloadCache.set(widenedHandler, initialValue);
|
|
297
365
|
}
|
|
298
366
|
}
|
|
299
367
|
}
|
|
368
|
+
/**
|
|
369
|
+
* Subscribe to an event.
|
|
370
|
+
*
|
|
371
|
+
* @param eventType - The event type. This is a unique identifier for this event.
|
|
372
|
+
* @param handler - The event handler. The type of the parameters for this event handler must
|
|
373
|
+
* match the type of the payload for this event type.
|
|
374
|
+
* @param metadata - Event metadata.
|
|
375
|
+
* @template SubscribedEvent - The event being subscribed to.
|
|
376
|
+
* @template SelectorReturnValue - The selector return value.
|
|
377
|
+
*/
|
|
378
|
+
#subscribe(eventType, handler, metadata) {
|
|
379
|
+
let subscribers = this.#events.get(eventType);
|
|
380
|
+
if (!subscribers) {
|
|
381
|
+
subscribers = new Map();
|
|
382
|
+
this.#events.set(eventType, subscribers);
|
|
383
|
+
}
|
|
384
|
+
subscribers.set(handler, metadata);
|
|
385
|
+
}
|
|
300
386
|
subscribeOnce(eventType, handler, options) {
|
|
301
387
|
const { selector, condition } = options ?? {};
|
|
302
388
|
// Casting to unknown to handle both the code path where a selector is defined and where it is omitted.
|
|
@@ -327,7 +413,7 @@ export class Messenger {
|
|
|
327
413
|
* @template SelectorReturnValue - The selector return value.
|
|
328
414
|
*/
|
|
329
415
|
unsubscribe(eventType, handler) {
|
|
330
|
-
const subscribers =
|
|
416
|
+
const subscribers = this.#events.get(eventType);
|
|
331
417
|
// Widen type of event handler by dropping ReturnType parameter.
|
|
332
418
|
//
|
|
333
419
|
// We need to drop it here because it's used as the parameter to the event handler, and
|
|
@@ -345,7 +431,7 @@ export class Messenger {
|
|
|
345
431
|
throw new Error(`Subscription not found for event: ${eventType}`);
|
|
346
432
|
}
|
|
347
433
|
if (metadata.selector) {
|
|
348
|
-
|
|
434
|
+
this.#eventPayloadCache.delete(widenedHandler);
|
|
349
435
|
}
|
|
350
436
|
subscribers.delete(widenedHandler);
|
|
351
437
|
}
|
|
@@ -359,7 +445,7 @@ export class Messenger {
|
|
|
359
445
|
* @template EventType - A type union of Event type strings.
|
|
360
446
|
*/
|
|
361
447
|
clearEventSubscriptions(eventType) {
|
|
362
|
-
const subscriptions =
|
|
448
|
+
const subscriptions = this.#events.get(eventType);
|
|
363
449
|
if (!subscriptions) {
|
|
364
450
|
return;
|
|
365
451
|
}
|
|
@@ -370,7 +456,7 @@ export class Messenger {
|
|
|
370
456
|
subscriptions.delete(handler);
|
|
371
457
|
}
|
|
372
458
|
if (subscriptions.size === 0) {
|
|
373
|
-
|
|
459
|
+
this.#events.delete(eventType);
|
|
374
460
|
}
|
|
375
461
|
}
|
|
376
462
|
/**
|
|
@@ -380,7 +466,7 @@ export class Messenger {
|
|
|
380
466
|
* may still have subscribers if they are delegated to another messenger.
|
|
381
467
|
*/
|
|
382
468
|
clearSubscriptions() {
|
|
383
|
-
for (const eventType of
|
|
469
|
+
for (const eventType of this.#events.keys()) {
|
|
384
470
|
this.clearEventSubscriptions(eventType);
|
|
385
471
|
}
|
|
386
472
|
}
|
|
@@ -414,10 +500,10 @@ export class Messenger {
|
|
|
414
500
|
}
|
|
415
501
|
return actionHandler(...args);
|
|
416
502
|
};
|
|
417
|
-
let delegationTargets =
|
|
503
|
+
let delegationTargets = this.#actionDelegationTargets.get(actionType);
|
|
418
504
|
if (!delegationTargets) {
|
|
419
505
|
delegationTargets = new Set();
|
|
420
|
-
|
|
506
|
+
this.#actionDelegationTargets.set(actionType, delegationTargets);
|
|
421
507
|
}
|
|
422
508
|
if (delegationTargets.has(messenger)) {
|
|
423
509
|
throw new Error(`The action '${actionType}' has already been delegated to this messenger`);
|
|
@@ -434,23 +520,23 @@ export class Messenger {
|
|
|
434
520
|
// events, rather than the single subscriber type corresponding to this
|
|
435
521
|
// event.
|
|
436
522
|
const subscriber = untypedSubscriber;
|
|
437
|
-
let delegatedEventSubscriptions =
|
|
523
|
+
let delegatedEventSubscriptions = this.#subscriptionDelegationTargets.get(eventType);
|
|
438
524
|
if (!delegatedEventSubscriptions) {
|
|
439
525
|
delegatedEventSubscriptions = new Map();
|
|
440
|
-
|
|
526
|
+
this.#subscriptionDelegationTargets.set(eventType, delegatedEventSubscriptions);
|
|
441
527
|
}
|
|
442
528
|
if (delegatedEventSubscriptions.has(messenger)) {
|
|
443
529
|
throw new Error(`The event '${eventType}' has already been delegated to this messenger`);
|
|
444
530
|
}
|
|
445
531
|
delegatedEventSubscriptions.set(messenger, subscriber);
|
|
446
|
-
const getPayload =
|
|
532
|
+
const getPayload = this.#initialEventPayloadGetters.get(eventType);
|
|
447
533
|
if (getPayload) {
|
|
448
534
|
messenger._internalRegisterDelegatedInitialEventPayload({
|
|
449
535
|
eventType,
|
|
450
536
|
getPayload,
|
|
451
537
|
});
|
|
452
538
|
}
|
|
453
|
-
|
|
539
|
+
this.#subscribe(eventType, subscriber, { delegation: true });
|
|
454
540
|
}
|
|
455
541
|
}
|
|
456
542
|
/**
|
|
@@ -500,11 +586,11 @@ export class Messenger {
|
|
|
500
586
|
* @template DelegatedEvents - An array of delegated event types.
|
|
501
587
|
*/
|
|
502
588
|
revoke({ actions, events, messenger, }) {
|
|
503
|
-
if (messenger ===
|
|
589
|
+
if (messenger === this.#parent) {
|
|
504
590
|
throw new Error('Cannot revoke from parent');
|
|
505
591
|
}
|
|
506
592
|
for (const actionType of actions ?? []) {
|
|
507
|
-
const delegationTargets =
|
|
593
|
+
const delegationTargets = this.#actionDelegationTargets.get(actionType);
|
|
508
594
|
if (!delegationTargets?.has(messenger)) {
|
|
509
595
|
// Nothing to revoke
|
|
510
596
|
continue;
|
|
@@ -512,11 +598,11 @@ export class Messenger {
|
|
|
512
598
|
messenger._internalUnregisterDelegatedActionHandler(actionType);
|
|
513
599
|
delegationTargets.delete(messenger);
|
|
514
600
|
if (delegationTargets.size === 0) {
|
|
515
|
-
|
|
601
|
+
this.#actionDelegationTargets.delete(actionType);
|
|
516
602
|
}
|
|
517
603
|
}
|
|
518
604
|
for (const eventType of events ?? []) {
|
|
519
|
-
const delegationTargets =
|
|
605
|
+
const delegationTargets = this.#subscriptionDelegationTargets.get(eventType);
|
|
520
606
|
if (!delegationTargets) {
|
|
521
607
|
// Nothing to revoke
|
|
522
608
|
continue;
|
|
@@ -529,7 +615,7 @@ export class Messenger {
|
|
|
529
615
|
this.unsubscribe(eventType, delegatedSubscriber);
|
|
530
616
|
delegationTargets.delete(messenger);
|
|
531
617
|
if (delegationTargets.size === 0) {
|
|
532
|
-
|
|
618
|
+
this.#subscriptionDelegationTargets.delete(eventType);
|
|
533
619
|
}
|
|
534
620
|
}
|
|
535
621
|
}
|
|
@@ -554,7 +640,7 @@ export class Messenger {
|
|
|
554
640
|
// contravariant over the handler parameter type. Using `Action` would lead to a type error
|
|
555
641
|
// here because the messenger we've delegated to supports _additional_ actions.
|
|
556
642
|
handler) {
|
|
557
|
-
|
|
643
|
+
this.#registerActionHandler(actionType, handler);
|
|
558
644
|
}
|
|
559
645
|
/**
|
|
560
646
|
* Unregister an action handler for an action delegated from another messenger.
|
|
@@ -570,7 +656,7 @@ export class Messenger {
|
|
|
570
656
|
* @template ActionType - A type union of Action type strings.
|
|
571
657
|
*/
|
|
572
658
|
_internalUnregisterDelegatedActionHandler(actionType) {
|
|
573
|
-
|
|
659
|
+
this.#unregisterActionHandler(actionType);
|
|
574
660
|
}
|
|
575
661
|
/**
|
|
576
662
|
* Register a function for getting the initial payload for an event that has been delegated from
|
|
@@ -590,7 +676,7 @@ export class Messenger {
|
|
|
590
676
|
* @param args.getPayload - A function for retrieving the event payload.
|
|
591
677
|
*/
|
|
592
678
|
_internalRegisterDelegatedInitialEventPayload({ eventType, getPayload, }) {
|
|
593
|
-
|
|
679
|
+
this.#registerInitialEventPayload({ eventType, getPayload });
|
|
594
680
|
}
|
|
595
681
|
/**
|
|
596
682
|
* Publish an event that was delegated from another messenger.
|
|
@@ -611,88 +697,19 @@ export class Messenger {
|
|
|
611
697
|
* @template EventType - A type union of Event type strings.
|
|
612
698
|
*/
|
|
613
699
|
_internalPublishDelegated(eventType, ...payload) {
|
|
614
|
-
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
_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) {
|
|
618
|
-
if (__classPrivateFieldGet(this, _Messenger_actions, "f").has(actionType)) {
|
|
619
|
-
throw new Error(`A handler for ${actionType} has already been registered`);
|
|
620
|
-
}
|
|
621
|
-
__classPrivateFieldGet(this, _Messenger_actions, "f").set(actionType, handler);
|
|
622
|
-
}, _Messenger_unregisterActionHandler = function _Messenger_unregisterActionHandler(actionType) {
|
|
623
|
-
__classPrivateFieldGet(this, _Messenger_actions, "f").delete(actionType);
|
|
624
|
-
}, _Messenger_registerInitialEventPayload = function _Messenger_registerInitialEventPayload({ eventType, getPayload, }) {
|
|
625
|
-
__classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").set(eventType, getPayload);
|
|
626
|
-
const delegationTargets = __classPrivateFieldGet(this, _Messenger_subscriptionDelegationTargets, "f").get(eventType);
|
|
627
|
-
if (!delegationTargets) {
|
|
628
|
-
return;
|
|
629
|
-
}
|
|
630
|
-
for (const messenger of delegationTargets.keys()) {
|
|
631
|
-
messenger._internalRegisterDelegatedInitialEventPayload({
|
|
632
|
-
eventType,
|
|
633
|
-
getPayload,
|
|
634
|
-
});
|
|
635
|
-
}
|
|
636
|
-
}, _Messenger_publish = function _Messenger_publish(eventType, ...payload) {
|
|
637
|
-
// Defer a re-entrant publish of the same event (e.g. a subscriber that
|
|
638
|
-
// publishes the event it is handling). Delivering it inline would let the
|
|
639
|
-
// in-progress publish resume and re-deliver its now-stale payload to the
|
|
640
|
-
// subscribers it had not reached yet.
|
|
641
|
-
const inProgress = __classPrivateFieldGet(this, _Messenger_deferredPublishes, "f").get(eventType);
|
|
642
|
-
if (inProgress) {
|
|
643
|
-
inProgress.push(() => __classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_deliverToSubscribers).call(this, eventType, ...payload));
|
|
644
|
-
return;
|
|
700
|
+
this.#publish(eventType, ...payload);
|
|
645
701
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
__classPrivateFieldGet(this, _Messenger_deferredPublishes, "f").delete(eventType);
|
|
658
|
-
}
|
|
659
|
-
}, _Messenger_deliverToSubscribers = function _Messenger_deliverToSubscribers(eventType, ...payload) {
|
|
660
|
-
const subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
661
|
-
if (subscribers) {
|
|
662
|
-
for (const [handler, { selector }] of subscribers.entries()) {
|
|
663
|
-
try {
|
|
664
|
-
if (selector) {
|
|
665
|
-
const previousValue = __classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").get(handler);
|
|
666
|
-
const newValue = selector(...payload);
|
|
667
|
-
if (newValue !== previousValue) {
|
|
668
|
-
__classPrivateFieldGet(this, _Messenger_eventPayloadCache, "f").set(handler, newValue);
|
|
669
|
-
handler(newValue, previousValue);
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
else {
|
|
673
|
-
handler(...payload);
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
catch (error) {
|
|
677
|
-
// Capture error without interrupting the event publishing.
|
|
678
|
-
if (this.captureException) {
|
|
679
|
-
this.captureException(error instanceof Error ? error : new Error(String(error)));
|
|
680
|
-
}
|
|
681
|
-
else {
|
|
682
|
-
console.error(error);
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
}, _Messenger_subscribe = function _Messenger_subscribe(eventType, handler, metadata) {
|
|
688
|
-
let subscribers = __classPrivateFieldGet(this, _Messenger_events, "f").get(eventType);
|
|
689
|
-
if (!subscribers) {
|
|
690
|
-
subscribers = new Map();
|
|
691
|
-
__classPrivateFieldGet(this, _Messenger_events, "f").set(eventType, subscribers);
|
|
702
|
+
/**
|
|
703
|
+
* Determine whether the given name is within the current namespace.
|
|
704
|
+
*
|
|
705
|
+
* If the current namespace is MOCK_ANY_NAMESPACE, this check always returns true.
|
|
706
|
+
*
|
|
707
|
+
* @param name - The name to check
|
|
708
|
+
* @returns Whether the name is within the current namespace
|
|
709
|
+
*/
|
|
710
|
+
#isInCurrentNamespace(name) {
|
|
711
|
+
return (this.#namespace === MOCK_ANY_NAMESPACE ||
|
|
712
|
+
name.startsWith(`${this.#namespace}:`));
|
|
692
713
|
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
return (__classPrivateFieldGet(this, _Messenger_namespace, "f") === MOCK_ANY_NAMESPACE ||
|
|
696
|
-
name.startsWith(`${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:`));
|
|
697
|
-
};
|
|
698
|
-
//# sourceMappingURL=Messenger.mjs.map
|
|
714
|
+
}
|
|
715
|
+
//# sourceMappingURL=Messenger.js.map
|