You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

@metamask/messenger

Package Overview
Dependencies
Maintainers
9
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask/messenger - npm Package Compare versions

Comparing version
0.1.0
to
0.2.0
+13
-1
CHANGELOG.md

@@ -10,2 +10,13 @@ # Changelog

## [0.2.0]
### Added
- Allow disabling namespace checks in unit tests using the new `MOCK_ANY_NAMESPACE` constant and `MockAnyNamespace` type ([#6420](https://github.com/MetaMask/core/pull/6420))
- To disable namespace checks, use `MockAnyNamespace` as the `Namespace` type parameter, and use `MOCK_ANY_NAMESPACE` as the `namespace` constructor parameter.
### Changed
- Keep delegated handlers when unregistering actions ([#6395](https://github.com/MetaMask/core/pull/6395))
## [0.1.0]

@@ -38,3 +49,4 @@

[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/messenger@0.1.0...HEAD
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/messenger@0.2.0...HEAD
[0.2.0]: https://github.com/MetaMask/core/compare/@metamask/messenger@0.1.0...@metamask/messenger@0.2.0
[0.1.0]: https://github.com/MetaMask/core/releases/tag/@metamask/messenger@0.1.0
+2
-1
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Messenger = void 0;
exports.Messenger = exports.MOCK_ANY_NAMESPACE = void 0;
var Messenger_1 = require("./Messenger.cjs");
Object.defineProperty(exports, "MOCK_ANY_NAMESPACE", { enumerable: true, get: function () { return Messenger_1.MOCK_ANY_NAMESPACE; } });
Object.defineProperty(exports, "Messenger", { enumerable: true, get: function () { return Messenger_1.Messenger; } });
//# sourceMappingURL=index.cjs.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAgBA,6CAAwC;AAA/B,sGAAA,SAAS,OAAA","sourcesContent":["export type {\n ActionHandler,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n GenericEventHandler,\n SelectorFunction,\n ActionConstraint,\n EventConstraint,\n MessengerActions,\n MessengerEvents,\n NamespacedBy,\n NotNamespacedBy,\n NamespacedName,\n} from './Messenger';\nexport { Messenger } from './Messenger';\n"]}
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAiBA,6CAA4D;AAAnD,+GAAA,kBAAkB,OAAA;AAAE,sGAAA,SAAS,OAAA","sourcesContent":["export type {\n ActionHandler,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n GenericEventHandler,\n SelectorFunction,\n ActionConstraint,\n EventConstraint,\n MessengerActions,\n MessengerEvents,\n MockAnyNamespace,\n NamespacedBy,\n NotNamespacedBy,\n NamespacedName,\n} from './Messenger';\nexport { MOCK_ANY_NAMESPACE, Messenger } from './Messenger';\n"]}

@@ -1,3 +0,3 @@

export type { ActionHandler, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, GenericEventHandler, SelectorFunction, ActionConstraint, EventConstraint, MessengerActions, MessengerEvents, NamespacedBy, NotNamespacedBy, NamespacedName, } from "./Messenger.cjs";
export { Messenger } from "./Messenger.cjs";
export type { ActionHandler, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, GenericEventHandler, SelectorFunction, ActionConstraint, EventConstraint, MessengerActions, MessengerEvents, MockAnyNamespace, NamespacedBy, NotNamespacedBy, NamespacedName, } from "./Messenger.cjs";
export { MOCK_ANY_NAMESPACE, Messenger } from "./Messenger.cjs";
//# sourceMappingURL=index.d.cts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,eAAe,EACf,cAAc,GACf,wBAAoB;AACrB,OAAO,EAAE,SAAS,EAAE,wBAAoB"}
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,cAAc,GACf,wBAAoB;AACrB,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,wBAAoB"}

@@ -1,3 +0,3 @@

export type { ActionHandler, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, GenericEventHandler, SelectorFunction, ActionConstraint, EventConstraint, MessengerActions, MessengerEvents, NamespacedBy, NotNamespacedBy, NamespacedName, } from "./Messenger.mjs";
export { Messenger } from "./Messenger.mjs";
export type { ActionHandler, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, GenericEventHandler, SelectorFunction, ActionConstraint, EventConstraint, MessengerActions, MessengerEvents, MockAnyNamespace, NamespacedBy, NotNamespacedBy, NamespacedName, } from "./Messenger.mjs";
export { MOCK_ANY_NAMESPACE, Messenger } from "./Messenger.mjs";
//# sourceMappingURL=index.d.mts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,eAAe,EACf,cAAc,GACf,wBAAoB;AACrB,OAAO,EAAE,SAAS,EAAE,wBAAoB"}
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,cAAc,GACf,wBAAoB;AACrB,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,wBAAoB"}

@@ -1,2 +0,2 @@

export { Messenger } from "./Messenger.mjs";
export { MOCK_ANY_NAMESPACE, Messenger } from "./Messenger.mjs";
//# sourceMappingURL=index.mjs.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,SAAS,EAAE,wBAAoB","sourcesContent":["export type {\n ActionHandler,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n GenericEventHandler,\n SelectorFunction,\n ActionConstraint,\n EventConstraint,\n MessengerActions,\n MessengerEvents,\n NamespacedBy,\n NotNamespacedBy,\n NamespacedName,\n} from './Messenger';\nexport { Messenger } from './Messenger';\n"]}
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,wBAAoB","sourcesContent":["export type {\n ActionHandler,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n GenericEventHandler,\n SelectorFunction,\n ActionConstraint,\n EventConstraint,\n MessengerActions,\n MessengerEvents,\n MockAnyNamespace,\n NamespacedBy,\n NotNamespacedBy,\n NamespacedName,\n} from './Messenger';\nexport { MOCK_ANY_NAMESPACE, Messenger } from './Messenger';\n"]}

@@ -15,4 +15,12 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.Messenger = void 0;
exports.Messenger = exports.MOCK_ANY_NAMESPACE = void 0;
/**
* Messenger namespace checks can be disabled by using this as the `namespace` constructor
* parameter, and using `MockAnyNamespace` as the Namespace type parameter.
*
* This is useful for mocking a variety of different actions/events in unit tests. Please do not
* use this in production code.
*/
exports.MOCK_ANY_NAMESPACE = 'MOCK_ANY_NAMESPACE';
/**
* A message broker for "actions" and "events".

@@ -85,3 +93,2 @@ *

registerActionHandler(actionType, handler) {
/* istanbul ignore if */ // Branch unreachable with valid types
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, actionType)) {

@@ -126,3 +133,2 @@ throw new Error(`Only allowed registering action handlers prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);

unregisterActionHandler(actionType) {
/* istanbul ignore if */ // Branch unreachable with valid types
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, actionType)) {

@@ -178,3 +184,2 @@ throw new Error(`Only allowed unregistering action handlers prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);

registerInitialEventPayload({ eventType, getPayload, }) {
/* istanbul ignore if */ // Branch unreachable with valid types
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, eventType)) {

@@ -207,3 +212,2 @@ throw new Error(`Only allowed registering initial payloads for events prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);

publish(eventType, ...payload) {
/* istanbul ignore if */ // Branch unreachable with valid types
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, eventType)) {

@@ -337,3 +341,3 @@ throw new Error(`Only allowed publishing events prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);

if (!actionHandler) {
throw new Error(`Cannot call '${actionType}', action not registered.`);
throw new Error(`A handler for ${actionType} has not been registered`);
}

@@ -518,10 +522,2 @@ return actionHandler(...args);

__classPrivateFieldGet(this, _Messenger_actions, "f").delete(actionType);
const delegationTargets = __classPrivateFieldGet(this, _Messenger_actionDelegationTargets, "f").get(actionType);
if (!delegationTargets) {
return;
}
for (const messenger of delegationTargets) {
messenger._internalUnregisterDelegatedActionHandler(actionType);
}
__classPrivateFieldGet(this, _Messenger_actionDelegationTargets, "f").delete(actionType);
}, _Messenger_registerInitialEventPayload = function _Messenger_registerInitialEventPayload({ eventType, getPayload, }) {

@@ -573,4 +569,5 @@ __classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").set(eventType, getPayload);

}, _Messenger_isInCurrentNamespace = function _Messenger_isInCurrentNamespace(name) {
return name.startsWith(`${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:`);
return (__classPrivateFieldGet(this, _Messenger_namespace, "f") === exports.MOCK_ANY_NAMESPACE ||
name.startsWith(`${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:`));
};
//# sourceMappingURL=Messenger.cjs.map

@@ -1,1 +0,1 @@

{"version":3,"file":"Messenger.cjs","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAuKA;;;;;;;;;;;GAWG;AACH,MAAa,SAAS;IA4DpB;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,MAAM,GAQP;;QAlEQ,uCAAsB;QAE/B;;;WAGG;QACM,oCAA6B;QAE7B,6BAAW,IAAI,GAAG,EAAqC,EAAC;QAExD,4BAAU,IAAI,GAAG,EAA8C,EAAC;QAEzE;;WAEG;QACM,mDAAiC,IAAI,GAAG,EAG9C,EAAC;QAEJ;;WAEG;QACM,6CAA2B,IAAI,GAAG,EAGxC,EAAC;QAEJ;;;;WAIG;QACM,gDAA8B,IAAI,GAAG,EAG3C,EAAC;QAEJ;;WAEG;QACM,uCAAqB,IAAI,GAAG,EAGlC,EAAC;QAuBF,uBAAA,IAAI,wBAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,qBAAW,MAAM,MAA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AAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC9C,IAAI,CAAC,WAAW,EAAE;QAChB,WAAW,GAAG,IAAI,GAAG,EAAE,CAAC;QACxB,uBAAA,IAAI,yBAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;KAC1C;IACD,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AACrC,CAAC,6EAiXqB,IAAY;IAChC,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,4BAAW,GAAG,CAAC,CAAC;AAChD,CAAC","sourcesContent":["export 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 = unknown,\n> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue = unknown> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: NamespacedName;\n handler: ((...args: never) => unknown) | ((...args: never[]) => unknown);\n};\nexport type EventConstraint = {\n type: NamespacedName;\n payload: unknown[];\n};\n\n/**\n * Extract action types from a Messenger type.\n *\n * @template Subject - The messenger type to extract from.\n */\nexport type MessengerActions<\n Subject extends Messenger<string, ActionConstraint, EventConstraint>,\n> =\n Subject extends Messenger<string, infer Action, EventConstraint>\n ? Action\n : never;\n\n/**\n * Extract event types from a Messenger type.\n *\n * @template Subject - The messenger type to extract from.\n */\nexport type MessengerEvents<\n Subject extends Messenger<string, ActionConstraint, EventConstraint>,\n> =\n Subject extends Messenger<string, ActionConstraint, infer Event>\n ? Event\n : never;\n\n/**\n * Metadata for a single event subscription.\n *\n * @template Event - The event this subscription is for.\n */\ntype SubscriptionMetadata<Event extends EventConstraint> = {\n /**\n * Whether this subscription is for a delegated messenger. Delegation subscriptions are ignored\n * when clearing subscriptions.\n */\n delegation: boolean;\n /**\n * The optional selector function for this subscription.\n */\n selector?: SelectorFunction<Event, Event['type']>;\n};\n\n/**\n * A map of event handlers for a specific event.\n *\n * The key is the handler function, and the value contains additional subscription metadata.\n *\n * @template Event - The event these handlers are for.\n */\ntype EventSubscriptionMap<Event extends EventConstraint> = Map<\n GenericEventHandler | SelectorEventHandler,\n SubscriptionMetadata<Event>\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\n/**\n * A messenger that actions and/or events can be delegated to.\n *\n * This is a minimal type interface to avoid complex incompatibilities resulting from generics over\n * invariant types.\n */\ntype DelegatedMessenger = Pick<\n // The type is broadened to all actions/events because some messenger methods are contravariant\n // over this type (`registerDelegatedActionHandler` and `publishDelegated` for example). If this\n // type is narrowed to just the delegated actions/events, the types for event payload and action\n // parameters would not be wide enough.\n Messenger<string, ActionConstraint, EventConstraint>,\n | '_internalPublishDelegated'\n | '_internalRegisterDelegatedActionHandler'\n | '_internalRegisterDelegatedInitialEventPayload'\n | '_internalUnregisterDelegatedActionHandler'\n>;\n\ntype StripNamespace<Namespaced extends NamespacedName> =\n Namespaced extends `${string}:${infer Name}` ? Name : 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 prefixed by a namespace (which is delimited by a colon, e.g.\n * `Namespace:actionName`).\n *\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template Namespace - The namespace for the messenger.\n */\nexport class Messenger<\n Namespace extends string,\n Action extends ActionConstraint = never,\n Event extends EventConstraint = never,\n Parent extends Messenger<\n string,\n ActionConstraint,\n EventConstraint,\n // Use `any` to avoid preventing a parent from having a parent. `any` is harmless in a type\n // constraint anyway, it's the one totally safe place to use it.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n > = never,\n> {\n readonly #namespace: Namespace;\n\n /**\n * The parent messenger. All actions/events under this namespace are automatically delegated to\n * the parent messenger.\n */\n readonly #parent?: DelegatedMessenger;\n\n readonly #actions = new Map<Action['type'], Action['handler']>();\n\n readonly #events = new Map<Event['type'], EventSubscriptionMap<Event>>();\n\n /**\n * The set of messengers we've delegated events to and their event handlers, by event type.\n */\n readonly #subscriptionDelegationTargets = new Map<\n Event['type'],\n Map<DelegatedMessenger, ExtractEventHandler<Event, Event['type']>>\n >();\n\n /**\n * The set of messengers we've delegated actions to, by action type.\n */\n readonly #actionDelegationTargets = new Map<\n Action['type'],\n Set<DelegatedMessenger>\n >();\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 * Construct a messenger.\n *\n * If a parent messenger is given, all actions and events under this messenger's namespace will\n * be delegated to the parent automatically.\n *\n * @param args - Constructor arguments\n * @param args.namespace - The messenger namespace.\n * @param args.parent - The parent messenger.\n */\n constructor({\n namespace,\n parent,\n }: {\n namespace: Namespace;\n parent?: Action['type'] extends MessengerActions<Parent>['type']\n ? Event['type'] extends MessengerEvents<Parent>['type']\n ? Parent\n : never\n : never;\n }) {\n this.#namespace = namespace;\n this.#parent = parent;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action being registered must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n registerActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(actionType: ActionType, handler: ActionHandler<Action, ActionType>) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(actionType)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#registerActionHandler(actionType, handler);\n if (this.#parent) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // action, but this is OK because it's validated in the constructor.\n this.delegate({ actions: [actionType], messenger: this.#parent });\n }\n }\n\n #registerActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n handler: ActionHandler<ActionConstraint, 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 * @template MessengerClient - The type expected to make use of the messenger.\n * @template MethodNames - The type union of method names to register as action handlers.\n */\n registerMethodActionHandlers<\n MessengerClient extends { name: Namespace },\n MethodNames extends keyof MessengerClient & StripNamespace<Action['type']>,\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 * The action being unregistered must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n unregisterActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(actionType: ActionType) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(actionType)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#unregisterActionHandler(actionType);\n }\n\n #unregisterActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#actions.delete(actionType);\n const delegationTargets = this.#actionDelegationTargets.get(actionType);\n if (!delegationTargets) {\n return;\n }\n for (const messenger of delegationTargets) {\n messenger._internalUnregisterDelegatedActionHandler(actionType);\n }\n this.#actionDelegationTargets.delete(actionType);\n }\n\n /**\n * Unregister all action handlers.\n *\n * This prevents all actions from being called.\n */\n clearActions() {\n for (const actionType of this.#actions.keys()) {\n this.#unregisterActionHandler(actionType);\n }\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 * The event type must be under the same namespace as the messenger.\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 * @template EventType - A type union of Event type strings under this messenger's namespace.\n */\n registerInitialEventPayload<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(eventType)) {\n throw new Error(\n `Only allowed registering initial payloads for events prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n if (\n this.#parent &&\n !this.#subscriptionDelegationTargets.get(eventType)?.has(this.#parent)\n ) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // event, but this is OK because it's validated in the constructor.\n this.delegate({ events: [eventType], messenger: this.#parent });\n }\n this.#registerInitialEventPayload({ eventType, getPayload });\n }\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 const delegationTargets =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegationTargets) {\n return;\n }\n for (const messenger of delegationTargets.keys()) {\n messenger._internalRegisterDelegatedInitialEventPayload({\n eventType,\n getPayload,\n });\n }\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 * The event being published must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n publish<EventType extends Event['type'] & NamespacedName<Namespace>>(\n eventType: EventType & NamespacedName<Namespace>,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(eventType)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n if (\n this.#parent &&\n !this.#subscriptionDelegationTargets.get(eventType)?.has(this.#parent)\n ) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // event, but this is OK because it's validated in the constructor.\n this.delegate({ events: [eventType], messenger: this.#parent });\n }\n this.#publish(eventType, ...payload);\n }\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:\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler<SelectorReturnValue>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void {\n // Widen type of event handler by dropping ReturnType parameter.\n //\n // We need to drop it here because it's used as the parameter to the event handler, and\n // functions in general are contravariant over the parameter type. This means the type is no\n // longer valid once it's added to a broader type union with other handlers (because as far\n // as TypeScript knows, we might call the handler with output from a different selector).\n //\n // This cast means the type system is not guaranteeing the handler is called with the matching\n // input selector return value. The parameter types do ensure they match when `subscribe` is\n // called, but past that point we need to make sure of that with manual review and tests\n // instead.\n const widenedHandler = handler as\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler;\n this.#subscribe(eventType, widenedHandler, { delegation: false, selector });\n\n if (selector) {\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n const initialValue = selector(...getPayload());\n this.#eventPayloadCache.set(widenedHandler, initialValue);\n }\n }\n }\n\n /**\n * Subscribe to an event.\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 * @param metadata - Event metadata.\n * @template SubscribedEvent - The event being subscribed to.\n * @template SelectorReturnValue - The selector return value.\n */\n #subscribe<SubscribedEvent extends EventConstraint>(\n eventType: SubscribedEvent['type'],\n handler:\n | ExtractEventHandler<SubscribedEvent, SubscribedEvent['type']>\n | SelectorEventHandler,\n metadata: SubscriptionMetadata<SubscribedEvent>,\n ): void {\n let subscribers = this.#events.get(eventType);\n if (!subscribers) {\n subscribers = new Map();\n this.#events.set(eventType, subscribers);\n }\n subscribers.set(handler, metadata);\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 * @template SelectorReturnValue - The selector return value.\n */\n unsubscribe<EventType extends Event['type'], SelectorReturnValue = unknown>(\n eventType: EventType,\n handler:\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler<SelectorReturnValue>,\n ) {\n const subscribers = this.#events.get(eventType);\n\n // Widen type of event handler by dropping ReturnType parameter.\n //\n // We need to drop it here because it's used as the parameter to the event handler, and\n // functions in general are contravariant over the parameter type. This means the type is no\n // longer valid once it's added to a broader type union with other handlers (because as far\n // as TypeScript knows, we might call the handler with output from a different selector).\n //\n // This poses no risk in this case, since we never call the handler past this point.\n const widenedHandler = handler as\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler;\n if (!subscribers) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n const metadata = subscribers.get(widenedHandler);\n if (!metadata) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n if (metadata.selector) {\n this.#eventPayloadCache.delete(widenedHandler);\n }\n\n subscribers.delete(widenedHandler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event registered from this messenger. The\n * event may still have subscribers if it has been delegated to another messenger.\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 const subscriptions = this.#events.get(eventType);\n if (!subscriptions) {\n return;\n }\n\n for (const [handler, metadata] of subscriptions.entries()) {\n if (metadata.delegation) {\n continue;\n }\n subscriptions.delete(handler);\n }\n\n if (subscriptions.size === 0) {\n this.#events.delete(eventType);\n }\n }\n\n /**\n * Clear all subscriptions.\n *\n * This will remove all subscribed handlers for all events registered from this messenger. Events\n * may still have subscribers if they are delegated to another messenger.\n */\n clearSubscriptions() {\n for (const eventType of this.#events.keys()) {\n this.clearEventSubscriptions(eventType);\n }\n }\n\n /**\n * Delegate actions and/or events to another messenger.\n *\n * The messenger these actions/events are delegated to will be able to call these actions and\n * subscribe to these events.\n *\n * Note that the messenger these actions/events are delegated to must still have these\n * actions/events included in its type definition (as part of the Action and Event type\n * parameters). Actions and events are statically type checked, they cannot be delegated\n * dynamically at runtime.\n *\n * @param args - Arguments.\n * @param args.actions - The action types to delegate.\n * @param args.events - The event types to delegate.\n * @param args.messenger - The messenger to delegate to.\n * @template Delegatee - The messenger the actions/events are delegated to.\n * @template DelegatedActions - An array of delegated action types.\n * @template DelegatedEvents - An array of delegated event types.\n */\n delegate<\n Delegatee extends Messenger<string, ActionConstraint, EventConstraint>,\n DelegatedActions extends (MessengerActions<Delegatee> & Action)['type'][],\n DelegatedEvents extends (MessengerEvents<Delegatee> & Event)['type'][],\n >({\n actions,\n events,\n messenger,\n }: {\n actions?: DelegatedActions;\n events?: DelegatedEvents;\n messenger: Delegatee;\n }) {\n for (const actionType of actions || []) {\n const delegatedActionHandler = (\n ...args: ExtractActionParameters<\n MessengerActions<Delegatee> & Action,\n typeof actionType\n >\n ) => {\n // Cast to get more specific type, for this specific action\n // The types get collapsed by `this.#actions`\n const actionHandler = this.#actions.get(actionType) as\n | ActionHandler<\n MessengerActions<Delegatee> & Action,\n typeof actionType\n >\n | undefined;\n if (!actionHandler) {\n throw new Error(\n `Cannot call '${actionType}', action not registered.`,\n );\n }\n return actionHandler(...args);\n };\n let delegationTargets = this.#actionDelegationTargets.get(actionType);\n if (!delegationTargets) {\n delegationTargets = new Set<DelegatedMessenger>();\n this.#actionDelegationTargets.set(actionType, delegationTargets);\n }\n if (delegationTargets.has(messenger)) {\n throw new Error(\n `The action '${actionType}' has already been delegated to this messenger`,\n );\n }\n delegationTargets.add(messenger);\n\n messenger._internalRegisterDelegatedActionHandler(\n actionType,\n delegatedActionHandler,\n );\n }\n for (const eventType of events || []) {\n const untypedSubscriber = (\n ...payload: ExtractEventPayload<\n MessengerEvents<Delegatee> & Event,\n typeof eventType\n >\n ) => {\n messenger._internalPublishDelegated(eventType, ...payload);\n };\n // Cast to get more specific subscriber type for this specific event.\n // The types get collapsed here to the type union of all delegated\n // events, rather than the single subscriber type corresponding to this\n // event.\n const subscriber = untypedSubscriber as ExtractEventHandler<\n MessengerEvents<Delegatee> & Event,\n typeof eventType\n >;\n let delegatedEventSubscriptions =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegatedEventSubscriptions) {\n delegatedEventSubscriptions = new Map();\n this.#subscriptionDelegationTargets.set(\n eventType,\n delegatedEventSubscriptions,\n );\n }\n if (delegatedEventSubscriptions.has(messenger)) {\n throw new Error(\n `The event '${eventType}' has already been delegated to this messenger`,\n );\n }\n delegatedEventSubscriptions.set(messenger, subscriber);\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n messenger._internalRegisterDelegatedInitialEventPayload({\n eventType,\n getPayload,\n });\n }\n\n this.#subscribe(eventType, subscriber, { delegation: true });\n }\n }\n\n /**\n * Revoke delegated actions and/or events from another messenger.\n *\n * The messenger these actions/events are delegated to will no longer be able to call these\n * actions or subscribe to these events.\n *\n * @param args - Arguments.\n * @param args.actions - The action types to revoke.\n * @param args.events - The event types to revoke.\n * @param args.messenger - The messenger these actions/events were delegated to.\n * @template Delegatee - The messenger the actions/events are being revoked from.\n * @template DelegatedActions - An array of delegated action types.\n * @template DelegatedEvents - An array of delegated event types.\n */\n revoke<\n Delegatee extends Messenger<string, ActionConstraint, EventConstraint>,\n DelegatedActions extends (MessengerActions<Delegatee> & Action)['type'][],\n DelegatedEvents extends (MessengerEvents<Delegatee> & Event)['type'][],\n >({\n actions,\n events,\n messenger,\n }: {\n actions?: DelegatedActions;\n events?: DelegatedEvents;\n messenger: Delegatee;\n }) {\n if (messenger === this.#parent) {\n throw new Error('Cannot revoke from parent');\n }\n for (const actionType of actions || []) {\n const delegationTargets = this.#actionDelegationTargets.get(actionType);\n if (!delegationTargets || !delegationTargets.has(messenger)) {\n // Nothing to revoke\n continue;\n }\n messenger._internalUnregisterDelegatedActionHandler(actionType);\n delegationTargets.delete(messenger);\n if (delegationTargets.size === 0) {\n this.#actionDelegationTargets.delete(actionType);\n }\n }\n for (const eventType of events || []) {\n const delegationTargets =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegationTargets) {\n // Nothing to revoke\n continue;\n }\n const delegatedSubscriber = delegationTargets.get(messenger);\n if (!delegatedSubscriber) {\n // Nothing to revoke\n continue;\n }\n this.unsubscribe(eventType, delegatedSubscriber);\n delegationTargets.delete(messenger);\n if (delegationTargets.size === 0) {\n this.#subscriptionDelegationTargets.delete(eventType);\n }\n }\n }\n\n /**\n * Register an action handler for an action delegated from another messenger.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalRegisterDelegatedActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n // Using wider `ActionConstraint` type here rather than `Action` because the `Action` type is\n // contravariant over the handler parameter type. Using `Action` would lead to a type error\n // here because the messenger we've delegated to supports _additional_ actions.\n handler: ActionHandler<ActionConstraint, ActionType>,\n ) {\n this.#registerActionHandler(actionType, handler);\n }\n\n /**\n * Unregister an action handler for an action delegated from another messenger.\n *\n * This will prevent this action from being called.\n *\n * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @template ActionType - A type union of Action type strings.\n */\n _internalUnregisterDelegatedActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#unregisterActionHandler(actionType);\n }\n\n /**\n * Register a function for getting the initial payload for an event that has been delegated from\n * another messenger.\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 * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalRegisterDelegatedInitialEventPayload<\n EventType extends Event['type'],\n >({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n this.#registerInitialEventPayload({ eventType, getPayload });\n }\n\n /**\n * Publish an event that was delegated from another messenger.\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 * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalPublishDelegated<EventType extends Event['type']>(\n eventType: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n this.#publish(eventType, ...payload);\n }\n\n /**\n * Determine whether the given name is within the current namespace.\n *\n * @param name - The name to check\n * @returns Whether the name is within the current namespace\n */\n #isInCurrentNamespace(name: string): name is NamespacedName<Namespace> {\n return name.startsWith(`${this.#namespace}:`);\n }\n}\n"]}
{"version":3,"file":"Messenger.cjs","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAgGA;;;;;;GAMG;AACU,QAAA,kBAAkB,GAAG,oBAAoB,CAAC;AAiFvD;;;;;;;;;;;GAWG;AACH,MAAa,SAAS;IA4DpB;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,MAAM,GAQP;;QAlEQ,uCAAsB;QAE/B;;;WAGG;QACM,oCAA6B;QAE7B,6BAAW,IAAI,GAAG,EAAqC,EAAC;QAExD,4BAAU,IAAI,GAAG,EAA8C,EAAC;QAEzE;;WAEG;QACM,mDAAiC,IAAI,GAAG,EAG9C,EAAC;QAEJ;;WAEG;QACM,6CAA2B,IAAI,GAAG,EAGxC,EAAC;QAEJ;;;;WAIG;QACM,gDAA8B,IAAI,GAAG,EAG3C,EAAC;QAEJ;;WAEG;QACM,uCAAqB,IAAI,GAAG,EAGlC,EAAC;QAuBF,uBAAA,IAAI,wBAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,qBAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,qBAAqB,CAEnB,UAAsB,EAAE,OAA0C;QAClE,IAAI,CAAC,uBAAA,IAAI,6DAAsB,MAA1B,IAAI,EAAuB,UAAU,CAAC,EAAE;YAC3C,MAAM,IAAI,KAAK,CACb,yDACE,uBAAA,IAAI,4BACN,IAAI,CACL,CAAC;SACH;QACD,uBAAA,IAAI,8DAAuB,MAA3B,IAAI,EAAwB,UAAU,EAAE,OAAO,CAAC,CAAC;QACjD,IAAI,uBAAA,IAAI,yBAAQ,EAAE;YAChB,2FAA2F;YAC3F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oCAAmB,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;wBAC/C,OAAO,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC;qBAClC;iBACF;qBAAM;oBACJ,OAA+B,CAAC,GAAG,OAAO,CAAC,CAAC;iBAC9C;aACF;YAAC,OAAO,KAAK,EAAE;gBACd,qEAAqE;gBACrE,6DAA6D;gBAC7D,UAAU,CAAC,GAAG,EAAE;oBACd,MAAM,KAAK,CAAC;gBACd,CAAC,CAAC,CAAC;aACJ;SACF;KACF;AACH,CAAC,uDAmFC,SAAkC,EAClC,OAEwB,EACxB,QAA+C;IAE/C,IAAI,WAAW,GAAG,uBAAA,IAAI,yBAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC9C,IAAI,CAAC,WAAW,EAAE;QAChB,WAAW,GAAG,IAAI,GAAG,EAAE,CAAC;QACxB,uBAAA,IAAI,yBAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;KAC1C;IACD,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AACrC,CAAC,6EAmXqB,IAAY;IAChC,OAAO,CACL,uBAAA,IAAI,4BAAW,KAAK,0BAAkB;QACtC,IAAI,CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,4BAAW,GAAG,CAAC,CACvC,CAAC;AACJ,CAAC","sourcesContent":["export 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 = unknown,\n> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue = unknown> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: NamespacedName;\n handler: ((...args: never) => unknown) | ((...args: never[]) => unknown);\n};\nexport type EventConstraint = {\n type: NamespacedName;\n payload: unknown[];\n};\n\n/**\n * Extract action types from a Messenger type.\n *\n * @template Subject - The messenger type to extract from.\n */\nexport type MessengerActions<\n Subject extends Messenger<string, ActionConstraint, EventConstraint>,\n> =\n Subject extends Messenger<string, infer Action, EventConstraint>\n ? Action\n : never;\n\n/**\n * Extract event types from a Messenger type.\n *\n * @template Subject - The messenger type to extract from.\n */\nexport type MessengerEvents<\n Subject extends Messenger<string, ActionConstraint, EventConstraint>,\n> =\n Subject extends Messenger<string, ActionConstraint, infer Event>\n ? Event\n : never;\n\n/**\n * Messenger namespace checks can be disabled by using this as the `namespace` constructor\n * parameter, and using `MockAnyNamespace` as the Namespace type parameter.\n *\n * This is useful for mocking a variety of different actions/events in unit tests. Please do not\n * use this in production code.\n */\nexport const MOCK_ANY_NAMESPACE = 'MOCK_ANY_NAMESPACE';\n\n/**\n * A type representing any namespace.\n *\n * This is useful for mocking a variety of different actions/events in unit tests. Please do not\n * use this in production code.\n */\nexport type MockAnyNamespace = string;\n\n/**\n * Metadata for a single event subscription.\n *\n * @template Event - The event this subscription is for.\n */\ntype SubscriptionMetadata<Event extends EventConstraint> = {\n /**\n * Whether this subscription is for a delegated messenger. Delegation subscriptions are ignored\n * when clearing subscriptions.\n */\n delegation: boolean;\n /**\n * The optional selector function for this subscription.\n */\n selector?: SelectorFunction<Event, Event['type']>;\n};\n\n/**\n * A map of event handlers for a specific event.\n *\n * The key is the handler function, and the value contains additional subscription metadata.\n *\n * @template Event - The event these handlers are for.\n */\ntype EventSubscriptionMap<Event extends EventConstraint> = Map<\n GenericEventHandler | SelectorEventHandler,\n SubscriptionMetadata<Event>\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\n/**\n * A messenger that actions and/or events can be delegated to.\n *\n * This is a minimal type interface to avoid complex incompatibilities resulting from generics over\n * invariant types.\n */\ntype DelegatedMessenger = Pick<\n // The type is broadened to all actions/events because some messenger methods are contravariant\n // over this type (`registerDelegatedActionHandler` and `publishDelegated` for example). If this\n // type is narrowed to just the delegated actions/events, the types for event payload and action\n // parameters would not be wide enough.\n Messenger<string, ActionConstraint, EventConstraint>,\n | '_internalPublishDelegated'\n | '_internalRegisterDelegatedActionHandler'\n | '_internalRegisterDelegatedInitialEventPayload'\n | '_internalUnregisterDelegatedActionHandler'\n>;\n\ntype StripNamespace<Namespaced extends NamespacedName> =\n Namespaced extends `${string}:${infer Name}` ? Name : 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 prefixed by a namespace (which is delimited by a colon, e.g.\n * `Namespace:actionName`).\n *\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template Namespace - The namespace for the messenger.\n */\nexport class Messenger<\n Namespace extends string,\n Action extends ActionConstraint = never,\n Event extends EventConstraint = never,\n Parent extends Messenger<\n string,\n ActionConstraint,\n EventConstraint,\n // Use `any` to avoid preventing a parent from having a parent. `any` is harmless in a type\n // constraint anyway, it's the one totally safe place to use it.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n > = never,\n> {\n readonly #namespace: Namespace;\n\n /**\n * The parent messenger. All actions/events under this namespace are automatically delegated to\n * the parent messenger.\n */\n readonly #parent?: DelegatedMessenger;\n\n readonly #actions = new Map<Action['type'], Action['handler']>();\n\n readonly #events = new Map<Event['type'], EventSubscriptionMap<Event>>();\n\n /**\n * The set of messengers we've delegated events to and their event handlers, by event type.\n */\n readonly #subscriptionDelegationTargets = new Map<\n Event['type'],\n Map<DelegatedMessenger, ExtractEventHandler<Event, Event['type']>>\n >();\n\n /**\n * The set of messengers we've delegated actions to, by action type.\n */\n readonly #actionDelegationTargets = new Map<\n Action['type'],\n Set<DelegatedMessenger>\n >();\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 * Construct a messenger.\n *\n * If a parent messenger is given, all actions and events under this messenger's namespace will\n * be delegated to the parent automatically.\n *\n * @param args - Constructor arguments\n * @param args.namespace - The messenger namespace.\n * @param args.parent - The parent messenger.\n */\n constructor({\n namespace,\n parent,\n }: {\n namespace: Namespace;\n parent?: Action['type'] extends MessengerActions<Parent>['type']\n ? Event['type'] extends MessengerEvents<Parent>['type']\n ? Parent\n : never\n : never;\n }) {\n this.#namespace = namespace;\n this.#parent = parent;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action being registered must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n registerActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(actionType: ActionType, handler: ActionHandler<Action, ActionType>) {\n if (!this.#isInCurrentNamespace(actionType)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#registerActionHandler(actionType, handler);\n if (this.#parent) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // action, but this is OK because it's validated in the constructor.\n this.delegate({ actions: [actionType], messenger: this.#parent });\n }\n }\n\n #registerActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n handler: ActionHandler<ActionConstraint, 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 * @template MessengerClient - The type expected to make use of the messenger.\n * @template MethodNames - The type union of method names to register as action handlers.\n */\n registerMethodActionHandlers<\n MessengerClient extends { name: Namespace },\n MethodNames extends keyof MessengerClient & StripNamespace<Action['type']>,\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 * The action being unregistered must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n unregisterActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(actionType: ActionType) {\n if (!this.#isInCurrentNamespace(actionType)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#unregisterActionHandler(actionType);\n }\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 for (const actionType of this.#actions.keys()) {\n this.#unregisterActionHandler(actionType);\n }\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 * The event type must be under the same namespace as the messenger.\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 * @template EventType - A type union of Event type strings under this messenger's namespace.\n */\n registerInitialEventPayload<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n if (!this.#isInCurrentNamespace(eventType)) {\n throw new Error(\n `Only allowed registering initial payloads for events prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n if (\n this.#parent &&\n !this.#subscriptionDelegationTargets.get(eventType)?.has(this.#parent)\n ) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // event, but this is OK because it's validated in the constructor.\n this.delegate({ events: [eventType], messenger: this.#parent });\n }\n this.#registerInitialEventPayload({ eventType, getPayload });\n }\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 const delegationTargets =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegationTargets) {\n return;\n }\n for (const messenger of delegationTargets.keys()) {\n messenger._internalRegisterDelegatedInitialEventPayload({\n eventType,\n getPayload,\n });\n }\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 * The event being published must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n publish<EventType extends Event['type'] & NamespacedName<Namespace>>(\n eventType: EventType & NamespacedName<Namespace>,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n if (!this.#isInCurrentNamespace(eventType)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n if (\n this.#parent &&\n !this.#subscriptionDelegationTargets.get(eventType)?.has(this.#parent)\n ) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // event, but this is OK because it's validated in the constructor.\n this.delegate({ events: [eventType], messenger: this.#parent });\n }\n this.#publish(eventType, ...payload);\n }\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:\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler<SelectorReturnValue>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void {\n // Widen type of event handler by dropping ReturnType parameter.\n //\n // We need to drop it here because it's used as the parameter to the event handler, and\n // functions in general are contravariant over the parameter type. This means the type is no\n // longer valid once it's added to a broader type union with other handlers (because as far\n // as TypeScript knows, we might call the handler with output from a different selector).\n //\n // This cast means the type system is not guaranteeing the handler is called with the matching\n // input selector return value. The parameter types do ensure they match when `subscribe` is\n // called, but past that point we need to make sure of that with manual review and tests\n // instead.\n const widenedHandler = handler as\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler;\n this.#subscribe(eventType, widenedHandler, { delegation: false, selector });\n\n if (selector) {\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n const initialValue = selector(...getPayload());\n this.#eventPayloadCache.set(widenedHandler, initialValue);\n }\n }\n }\n\n /**\n * Subscribe to an event.\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 * @param metadata - Event metadata.\n * @template SubscribedEvent - The event being subscribed to.\n * @template SelectorReturnValue - The selector return value.\n */\n #subscribe<SubscribedEvent extends EventConstraint>(\n eventType: SubscribedEvent['type'],\n handler:\n | ExtractEventHandler<SubscribedEvent, SubscribedEvent['type']>\n | SelectorEventHandler,\n metadata: SubscriptionMetadata<SubscribedEvent>,\n ): void {\n let subscribers = this.#events.get(eventType);\n if (!subscribers) {\n subscribers = new Map();\n this.#events.set(eventType, subscribers);\n }\n subscribers.set(handler, metadata);\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 * @template SelectorReturnValue - The selector return value.\n */\n unsubscribe<EventType extends Event['type'], SelectorReturnValue = unknown>(\n eventType: EventType,\n handler:\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler<SelectorReturnValue>,\n ) {\n const subscribers = this.#events.get(eventType);\n\n // Widen type of event handler by dropping ReturnType parameter.\n //\n // We need to drop it here because it's used as the parameter to the event handler, and\n // functions in general are contravariant over the parameter type. This means the type is no\n // longer valid once it's added to a broader type union with other handlers (because as far\n // as TypeScript knows, we might call the handler with output from a different selector).\n //\n // This poses no risk in this case, since we never call the handler past this point.\n const widenedHandler = handler as\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler;\n if (!subscribers) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n const metadata = subscribers.get(widenedHandler);\n if (!metadata) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n if (metadata.selector) {\n this.#eventPayloadCache.delete(widenedHandler);\n }\n\n subscribers.delete(widenedHandler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event registered from this messenger. The\n * event may still have subscribers if it has been delegated to another messenger.\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 const subscriptions = this.#events.get(eventType);\n if (!subscriptions) {\n return;\n }\n\n for (const [handler, metadata] of subscriptions.entries()) {\n if (metadata.delegation) {\n continue;\n }\n subscriptions.delete(handler);\n }\n\n if (subscriptions.size === 0) {\n this.#events.delete(eventType);\n }\n }\n\n /**\n * Clear all subscriptions.\n *\n * This will remove all subscribed handlers for all events registered from this messenger. Events\n * may still have subscribers if they are delegated to another messenger.\n */\n clearSubscriptions() {\n for (const eventType of this.#events.keys()) {\n this.clearEventSubscriptions(eventType);\n }\n }\n\n /**\n * Delegate actions and/or events to another messenger.\n *\n * The messenger these actions/events are delegated to will be able to call these actions and\n * subscribe to these events.\n *\n * Note that the messenger these actions/events are delegated to must still have these\n * actions/events included in its type definition (as part of the Action and Event type\n * parameters). Actions and events are statically type checked, they cannot be delegated\n * dynamically at runtime.\n *\n * @param args - Arguments.\n * @param args.actions - The action types to delegate.\n * @param args.events - The event types to delegate.\n * @param args.messenger - The messenger to delegate to.\n * @template Delegatee - The messenger the actions/events are delegated to.\n * @template DelegatedActions - An array of delegated action types.\n * @template DelegatedEvents - An array of delegated event types.\n */\n delegate<\n Delegatee extends Messenger<string, ActionConstraint, EventConstraint>,\n DelegatedActions extends (MessengerActions<Delegatee> & Action)['type'][],\n DelegatedEvents extends (MessengerEvents<Delegatee> & Event)['type'][],\n >({\n actions,\n events,\n messenger,\n }: {\n actions?: DelegatedActions;\n events?: DelegatedEvents;\n messenger: Delegatee;\n }) {\n for (const actionType of actions || []) {\n const delegatedActionHandler = (\n ...args: ExtractActionParameters<\n MessengerActions<Delegatee> & Action,\n typeof actionType\n >\n ) => {\n // Cast to get more specific type, for this specific action\n // The types get collapsed by `this.#actions`\n const actionHandler = this.#actions.get(actionType) as\n | ActionHandler<\n MessengerActions<Delegatee> & Action,\n typeof actionType\n >\n | undefined;\n if (!actionHandler) {\n throw new Error(\n `A handler for ${actionType} has not been registered`,\n );\n }\n return actionHandler(...args);\n };\n let delegationTargets = this.#actionDelegationTargets.get(actionType);\n if (!delegationTargets) {\n delegationTargets = new Set<DelegatedMessenger>();\n this.#actionDelegationTargets.set(actionType, delegationTargets);\n }\n if (delegationTargets.has(messenger)) {\n throw new Error(\n `The action '${actionType}' has already been delegated to this messenger`,\n );\n }\n delegationTargets.add(messenger);\n\n messenger._internalRegisterDelegatedActionHandler(\n actionType,\n delegatedActionHandler,\n );\n }\n for (const eventType of events || []) {\n const untypedSubscriber = (\n ...payload: ExtractEventPayload<\n MessengerEvents<Delegatee> & Event,\n typeof eventType\n >\n ) => {\n messenger._internalPublishDelegated(eventType, ...payload);\n };\n // Cast to get more specific subscriber type for this specific event.\n // The types get collapsed here to the type union of all delegated\n // events, rather than the single subscriber type corresponding to this\n // event.\n const subscriber = untypedSubscriber as ExtractEventHandler<\n MessengerEvents<Delegatee> & Event,\n typeof eventType\n >;\n let delegatedEventSubscriptions =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegatedEventSubscriptions) {\n delegatedEventSubscriptions = new Map();\n this.#subscriptionDelegationTargets.set(\n eventType,\n delegatedEventSubscriptions,\n );\n }\n if (delegatedEventSubscriptions.has(messenger)) {\n throw new Error(\n `The event '${eventType}' has already been delegated to this messenger`,\n );\n }\n delegatedEventSubscriptions.set(messenger, subscriber);\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n messenger._internalRegisterDelegatedInitialEventPayload({\n eventType,\n getPayload,\n });\n }\n\n this.#subscribe(eventType, subscriber, { delegation: true });\n }\n }\n\n /**\n * Revoke delegated actions and/or events from another messenger.\n *\n * The messenger these actions/events are delegated to will no longer be able to call these\n * actions or subscribe to these events.\n *\n * @param args - Arguments.\n * @param args.actions - The action types to revoke.\n * @param args.events - The event types to revoke.\n * @param args.messenger - The messenger these actions/events were delegated to.\n * @template Delegatee - The messenger the actions/events are being revoked from.\n * @template DelegatedActions - An array of delegated action types.\n * @template DelegatedEvents - An array of delegated event types.\n */\n revoke<\n Delegatee extends Messenger<string, ActionConstraint, EventConstraint>,\n DelegatedActions extends (MessengerActions<Delegatee> & Action)['type'][],\n DelegatedEvents extends (MessengerEvents<Delegatee> & Event)['type'][],\n >({\n actions,\n events,\n messenger,\n }: {\n actions?: DelegatedActions;\n events?: DelegatedEvents;\n messenger: Delegatee;\n }) {\n if (messenger === this.#parent) {\n throw new Error('Cannot revoke from parent');\n }\n for (const actionType of actions || []) {\n const delegationTargets = this.#actionDelegationTargets.get(actionType);\n if (!delegationTargets || !delegationTargets.has(messenger)) {\n // Nothing to revoke\n continue;\n }\n messenger._internalUnregisterDelegatedActionHandler(actionType);\n delegationTargets.delete(messenger);\n if (delegationTargets.size === 0) {\n this.#actionDelegationTargets.delete(actionType);\n }\n }\n for (const eventType of events || []) {\n const delegationTargets =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegationTargets) {\n // Nothing to revoke\n continue;\n }\n const delegatedSubscriber = delegationTargets.get(messenger);\n if (!delegatedSubscriber) {\n // Nothing to revoke\n continue;\n }\n this.unsubscribe(eventType, delegatedSubscriber);\n delegationTargets.delete(messenger);\n if (delegationTargets.size === 0) {\n this.#subscriptionDelegationTargets.delete(eventType);\n }\n }\n }\n\n /**\n * Register an action handler for an action delegated from another messenger.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalRegisterDelegatedActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n // Using wider `ActionConstraint` type here rather than `Action` because the `Action` type is\n // contravariant over the handler parameter type. Using `Action` would lead to a type error\n // here because the messenger we've delegated to supports _additional_ actions.\n handler: ActionHandler<ActionConstraint, ActionType>,\n ) {\n this.#registerActionHandler(actionType, handler);\n }\n\n /**\n * Unregister an action handler for an action delegated from another messenger.\n *\n * This will prevent this action from being called.\n *\n * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @template ActionType - A type union of Action type strings.\n */\n _internalUnregisterDelegatedActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#unregisterActionHandler(actionType);\n }\n\n /**\n * Register a function for getting the initial payload for an event that has been delegated from\n * another messenger.\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 * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalRegisterDelegatedInitialEventPayload<\n EventType extends Event['type'],\n >({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n this.#registerInitialEventPayload({ eventType, getPayload });\n }\n\n /**\n * Publish an event that was delegated from another messenger.\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 * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalPublishDelegated<EventType extends Event['type']>(\n eventType: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n this.#publish(eventType, ...payload);\n }\n\n /**\n * Determine whether the given name is within the current namespace.\n *\n * If the current namespace is MOCK_ANY_NAMESPACE, this check always returns true.\n *\n * @param name - The name to check\n * @returns Whether the name is within the current namespace\n */\n #isInCurrentNamespace(name: string): name is NamespacedName<Namespace> {\n return (\n this.#namespace === MOCK_ANY_NAMESPACE ||\n name.startsWith(`${this.#namespace}:`)\n );\n }\n}\n"]}

@@ -42,2 +42,17 @@ export type ActionHandler<Action extends ActionConstraint, ActionType = Action['type']> = (...args: ExtractActionParameters<Action, ActionType>) => ExtractActionResponse<Action, ActionType>;

/**
* Messenger namespace checks can be disabled by using this as the `namespace` constructor
* parameter, and using `MockAnyNamespace` as the Namespace type parameter.
*
* This is useful for mocking a variety of different actions/events in unit tests. Please do not
* use this in production code.
*/
export declare const MOCK_ANY_NAMESPACE = "MOCK_ANY_NAMESPACE";
/**
* A type representing any namespace.
*
* This is useful for mocking a variety of different actions/events in unit tests. Please do not
* use this in production code.
*/
export type MockAnyNamespace = string;
/**
* A namespaced string

@@ -44,0 +59,0 @@ *

@@ -1,1 +0,1 @@

{"version":3,"file":"Messenger.d.cts","sourceRoot":"","sources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;;;;OAaG;IACH,MAAM,CACJ,SAAS,SAAS,SAAS,CAAC,MAAM,EAAE,gBAAgB,EAAE,eAAe,CAAC,EACtE,gBAAgB,SAAS,CAAC,gBAAgB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,CAAC,MAAM,CAAC,EAAE,EACzE,eAAe,SAAS,CAAC,eAAe,CAAC,SAAS,CAAC,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,EAAE,EACtE,EACA,OAAO,EACP,MAAM,EACN,SAAS,GACV,EAAE;QACD,OAAO,CAAC,EAAE,gBAAgB,CAAC;QAC3B,MAAM,CAAC,EAAE,eAAe,CAAC;QACzB,SAAS,EAAE,SAAS,CAAC;KACtB;IAoCD;;;;;;;;;;;;;;;OAeG;IACH,uCAAuC,CAAC,UAAU,SAAS,MAAM,CAAC,MAAM,CAAC,EACvE,UAAU,EAAE,UAAU,EAItB,OAAO,EAAE,aAAa,CAAC,gBAAgB,EAAE,UAAU,CAAC;IAKtD;;;;;;;;;;;;OAYG;IACH,yCAAyC,CAAC,UAAU,SAAS,MAAM,CAAC,MAAM,CAAC,EACzE,UAAU,EAAE,UAAU;IAKxB;;;;;;;;;;;;;;;;OAgBG;IACH,6CAA6C,CAC3C,SAAS,SAAS,KAAK,CAAC,MAAM,CAAC,EAC/B,EACA,SAAS,EACT,UAAU,GACX,EAAE;QACD,SAAS,EAAE,SAAS,CAAC;QACrB,UAAU,EAAE,MAAM,mBAAmB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;KACzD;IAID;;;;;;;;;;;;;;;;;OAiBG;IACH,yBAAyB,CAAC,SAAS,SAAS,KAAK,CAAC,MAAM,CAAC,EACvD,SAAS,EAAE,SAAS,EACpB,GAAG,OAAO,EAAE,mBAAmB,CAAC,KAAK,EAAE,SAAS,CAAC;CAmBpD"}

@@ -42,2 +42,17 @@ export type ActionHandler<Action extends ActionConstraint, ActionType = Action['type']> = (...args: ExtractActionParameters<Action, ActionType>) => ExtractActionResponse<Action, ActionType>;

/**
* Messenger namespace checks can be disabled by using this as the `namespace` constructor
* parameter, and using `MockAnyNamespace` as the Namespace type parameter.
*
* This is useful for mocking a variety of different actions/events in unit tests. Please do not
* use this in production code.
*/
export declare const MOCK_ANY_NAMESPACE = "MOCK_ANY_NAMESPACE";
/**
* A type representing any namespace.
*
* This is useful for mocking a variety of different actions/events in unit tests. Please do not
* use this in production code.
*/
export type MockAnyNamespace = string;
/**
* A namespaced string

@@ -44,0 +59,0 @@ *

@@ -1,1 +0,1 @@

{"version":3,"file":"Messenger.d.mts","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,CACvB,MAAM,SAAS,gBAAgB,EAC/B,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IACzB,CACF,GAAG,IAAI,EAAE,uBAAuB,CAAC,MAAM,EAAE,UAAU,CAAC,KACjD,qBAAqB,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAE/C,MAAM,MAAM,uBAAuB,CACjC,MAAM,SAAS,gBAAgB,EAC/B,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IACzB,MAAM,SAAS;IACjB,IAAI,EAAE,UAAU,CAAC;IACjB,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,WAAW,KAAK,OAAO,CAAC;CAClD,GACG,WAAW,GACX,KAAK,CAAC;AAEV,MAAM,MAAM,qBAAqB,CAC/B,MAAM,SAAS,gBAAgB,EAC/B,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IACzB,MAAM,SAAS;IACjB,IAAI,EAAE,UAAU,CAAC;IACjB,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,CAAC,KAAK,MAAM,kBAAkB,CAAC;CACzD,GACG,kBAAkB,GAClB,KAAK,CAAC;AAEV,MAAM,MAAM,mBAAmB,CAC7B,KAAK,SAAS,eAAe,EAC7B,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC,IACvB,KAAK,SAAS;IAChB,IA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AAE,MAAM,mBAAmB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;KACzD;IAID;;;;;;;;;;;;;;;;;OAiBG;IACH,yBAAyB,CAAC,SAAS,SAAS,KAAK,CAAC,MAAM,CAAC,EACvD,SAAS,EAAE,SAAS,EACpB,GAAG,OAAO,EAAE,mBAAmB,CAAC,KAAK,EAAE,SAAS,CAAC;CAmBpD"}

@@ -14,2 +14,10 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {

/**
* Messenger namespace checks can be disabled by using this as the `namespace` constructor
* parameter, and using `MockAnyNamespace` as the Namespace type parameter.
*
* This is useful for mocking a variety of different actions/events in unit tests. Please do not
* use this in production code.
*/
export const MOCK_ANY_NAMESPACE = 'MOCK_ANY_NAMESPACE';
/**
* A message broker for "actions" and "events".

@@ -82,3 +90,2 @@ *

registerActionHandler(actionType, handler) {
/* istanbul ignore if */ // Branch unreachable with valid types
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, actionType)) {

@@ -123,3 +130,2 @@ throw new Error(`Only allowed registering action handlers prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);

unregisterActionHandler(actionType) {
/* istanbul ignore if */ // Branch unreachable with valid types
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, actionType)) {

@@ -175,3 +181,2 @@ throw new Error(`Only allowed unregistering action handlers prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);

registerInitialEventPayload({ eventType, getPayload, }) {
/* istanbul ignore if */ // Branch unreachable with valid types
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, eventType)) {

@@ -204,3 +209,2 @@ throw new Error(`Only allowed registering initial payloads for events prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);

publish(eventType, ...payload) {
/* istanbul ignore if */ // Branch unreachable with valid types
if (!__classPrivateFieldGet(this, _Messenger_instances, "m", _Messenger_isInCurrentNamespace).call(this, eventType)) {

@@ -334,3 +338,3 @@ throw new Error(`Only allowed publishing events prefixed by '${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:'`);

if (!actionHandler) {
throw new Error(`Cannot call '${actionType}', action not registered.`);
throw new Error(`A handler for ${actionType} has not been registered`);
}

@@ -514,10 +518,2 @@ return actionHandler(...args);

__classPrivateFieldGet(this, _Messenger_actions, "f").delete(actionType);
const delegationTargets = __classPrivateFieldGet(this, _Messenger_actionDelegationTargets, "f").get(actionType);
if (!delegationTargets) {
return;
}
for (const messenger of delegationTargets) {
messenger._internalUnregisterDelegatedActionHandler(actionType);
}
__classPrivateFieldGet(this, _Messenger_actionDelegationTargets, "f").delete(actionType);
}, _Messenger_registerInitialEventPayload = function _Messenger_registerInitialEventPayload({ eventType, getPayload, }) {

@@ -569,4 +565,5 @@ __classPrivateFieldGet(this, _Messenger_initialEventPayloadGetters, "f").set(eventType, getPayload);

}, _Messenger_isInCurrentNamespace = function _Messenger_isInCurrentNamespace(name) {
return name.startsWith(`${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:`);
return (__classPrivateFieldGet(this, _Messenger_namespace, "f") === MOCK_ANY_NAMESPACE ||
name.startsWith(`${__classPrivateFieldGet(this, _Messenger_namespace, "f")}:`));
};
//# sourceMappingURL=Messenger.mjs.map

@@ -1,1 +0,1 @@

{"version":3,"file":"Messenger.mjs","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":";;;;;;;;;;;;AAuKA;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,SAAS;IA4DpB;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,MAAM,GAQP;;QAlEQ,uCAAsB;QAE/B;;;WAGG;QACM,oCAA6B;QAE7B,6BAAW,IAAI,GAAG,EAAqC,EAAC;QAExD,4BAAU,IAAI,GAAG,EAA8C,EAAC;QAEzE;;WAEG;QACM,mDAAiC,IAAI,GAAG,EAG9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K,EAAE;gBACd,qEAAqE;gBACrE,6DAA6D;gBAC7D,UAAU,CAAC,GAAG,EAAE;oBACd,MAAM,KAAK,CAAC;gBACd,CAAC,CAAC,CAAC;aACJ;SACF;KACF;AACH,CAAC,uDAmFC,SAAkC,EAClC,OAEwB,EACxB,QAA+C;IAE/C,IAAI,WAAW,GAAG,uBAAA,IAAI,yBAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC9C,IAAI,CAAC,WAAW,EAAE;QAChB,WAAW,GAAG,IAAI,GAAG,EAAE,CAAC;QACxB,uBAAA,IAAI,yBAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;KAC1C;IACD,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AACrC,CAAC,6EAiXqB,IAAY;IAChC,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,4BAAW,GAAG,CAAC,CAAC;AAChD,CAAC","sourcesContent":["export 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 = unknown,\n> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue = unknown> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: NamespacedName;\n handler: ((...args: never) => unknown) | ((...args: never[]) => unknown);\n};\nexport type EventConstraint = {\n type: NamespacedName;\n payload: unknown[];\n};\n\n/**\n * Extract action types from a Messenger type.\n *\n * @template Subject - The messenger type to extract from.\n */\nexport type MessengerActions<\n Subject extends Messenger<string, ActionConstraint, EventConstraint>,\n> =\n Subject extends Messenger<string, infer Action, EventConstraint>\n ? Action\n : never;\n\n/**\n * Extract event types from a Messenger type.\n *\n * @template Subject - The messenger type to extract from.\n */\nexport type MessengerEvents<\n Subject extends Messenger<string, ActionConstraint, EventConstraint>,\n> =\n Subject extends Messenger<string, ActionConstraint, infer Event>\n ? Event\n : never;\n\n/**\n * Metadata for a single event subscription.\n *\n * @template Event - The event this subscription is for.\n */\ntype SubscriptionMetadata<Event extends EventConstraint> = {\n /**\n * Whether this subscription is for a delegated messenger. Delegation subscriptions are ignored\n * when clearing subscriptions.\n */\n delegation: boolean;\n /**\n * The optional selector function for this subscription.\n */\n selector?: SelectorFunction<Event, Event['type']>;\n};\n\n/**\n * A map of event handlers for a specific event.\n *\n * The key is the handler function, and the value contains additional subscription metadata.\n *\n * @template Event - The event these handlers are for.\n */\ntype EventSubscriptionMap<Event extends EventConstraint> = Map<\n GenericEventHandler | SelectorEventHandler,\n SubscriptionMetadata<Event>\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\n/**\n * A messenger that actions and/or events can be delegated to.\n *\n * This is a minimal type interface to avoid complex incompatibilities resulting from generics over\n * invariant types.\n */\ntype DelegatedMessenger = Pick<\n // The type is broadened to all actions/events because some messenger methods are contravariant\n // over this type (`registerDelegatedActionHandler` and `publishDelegated` for example). If this\n // type is narrowed to just the delegated actions/events, the types for event payload and action\n // parameters would not be wide enough.\n Messenger<string, ActionConstraint, EventConstraint>,\n | '_internalPublishDelegated'\n | '_internalRegisterDelegatedActionHandler'\n | '_internalRegisterDelegatedInitialEventPayload'\n | '_internalUnregisterDelegatedActionHandler'\n>;\n\ntype StripNamespace<Namespaced extends NamespacedName> =\n Namespaced extends `${string}:${infer Name}` ? Name : 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 prefixed by a namespace (which is delimited by a colon, e.g.\n * `Namespace:actionName`).\n *\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template Namespace - The namespace for the messenger.\n */\nexport class Messenger<\n Namespace extends string,\n Action extends ActionConstraint = never,\n Event extends EventConstraint = never,\n Parent extends Messenger<\n string,\n ActionConstraint,\n EventConstraint,\n // Use `any` to avoid preventing a parent from having a parent. `any` is harmless in a type\n // constraint anyway, it's the one totally safe place to use it.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n > = never,\n> {\n readonly #namespace: Namespace;\n\n /**\n * The parent messenger. All actions/events under this namespace are automatically delegated to\n * the parent messenger.\n */\n readonly #parent?: DelegatedMessenger;\n\n readonly #actions = new Map<Action['type'], Action['handler']>();\n\n readonly #events = new Map<Event['type'], EventSubscriptionMap<Event>>();\n\n /**\n * The set of messengers we've delegated events to and their event handlers, by event type.\n */\n readonly #subscriptionDelegationTargets = new Map<\n Event['type'],\n Map<DelegatedMessenger, ExtractEventHandler<Event, Event['type']>>\n >();\n\n /**\n * The set of messengers we've delegated actions to, by action type.\n */\n readonly #actionDelegationTargets = new Map<\n Action['type'],\n Set<DelegatedMessenger>\n >();\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 * Construct a messenger.\n *\n * If a parent messenger is given, all actions and events under this messenger's namespace will\n * be delegated to the parent automatically.\n *\n * @param args - Constructor arguments\n * @param args.namespace - The messenger namespace.\n * @param args.parent - The parent messenger.\n */\n constructor({\n namespace,\n parent,\n }: {\n namespace: Namespace;\n parent?: Action['type'] extends MessengerActions<Parent>['type']\n ? Event['type'] extends MessengerEvents<Parent>['type']\n ? Parent\n : never\n : never;\n }) {\n this.#namespace = namespace;\n this.#parent = parent;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action being registered must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n registerActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(actionType: ActionType, handler: ActionHandler<Action, ActionType>) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(actionType)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#registerActionHandler(actionType, handler);\n if (this.#parent) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // action, but this is OK because it's validated in the constructor.\n this.delegate({ actions: [actionType], messenger: this.#parent });\n }\n }\n\n #registerActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n handler: ActionHandler<ActionConstraint, 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 * @template MessengerClient - The type expected to make use of the messenger.\n * @template MethodNames - The type union of method names to register as action handlers.\n */\n registerMethodActionHandlers<\n MessengerClient extends { name: Namespace },\n MethodNames extends keyof MessengerClient & StripNamespace<Action['type']>,\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 * The action being unregistered must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n unregisterActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(actionType: ActionType) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(actionType)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#unregisterActionHandler(actionType);\n }\n\n #unregisterActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#actions.delete(actionType);\n const delegationTargets = this.#actionDelegationTargets.get(actionType);\n if (!delegationTargets) {\n return;\n }\n for (const messenger of delegationTargets) {\n messenger._internalUnregisterDelegatedActionHandler(actionType);\n }\n this.#actionDelegationTargets.delete(actionType);\n }\n\n /**\n * Unregister all action handlers.\n *\n * This prevents all actions from being called.\n */\n clearActions() {\n for (const actionType of this.#actions.keys()) {\n this.#unregisterActionHandler(actionType);\n }\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 * The event type must be under the same namespace as the messenger.\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 * @template EventType - A type union of Event type strings under this messenger's namespace.\n */\n registerInitialEventPayload<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(eventType)) {\n throw new Error(\n `Only allowed registering initial payloads for events prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n if (\n this.#parent &&\n !this.#subscriptionDelegationTargets.get(eventType)?.has(this.#parent)\n ) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // event, but this is OK because it's validated in the constructor.\n this.delegate({ events: [eventType], messenger: this.#parent });\n }\n this.#registerInitialEventPayload({ eventType, getPayload });\n }\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 const delegationTargets =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegationTargets) {\n return;\n }\n for (const messenger of delegationTargets.keys()) {\n messenger._internalRegisterDelegatedInitialEventPayload({\n eventType,\n getPayload,\n });\n }\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 * The event being published must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n publish<EventType extends Event['type'] & NamespacedName<Namespace>>(\n eventType: EventType & NamespacedName<Namespace>,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(eventType)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n if (\n this.#parent &&\n !this.#subscriptionDelegationTargets.get(eventType)?.has(this.#parent)\n ) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // event, but this is OK because it's validated in the constructor.\n this.delegate({ events: [eventType], messenger: this.#parent });\n }\n this.#publish(eventType, ...payload);\n }\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:\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler<SelectorReturnValue>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void {\n // Widen type of event handler by dropping ReturnType parameter.\n //\n // We need to drop it here because it's used as the parameter to the event handler, and\n // functions in general are contravariant over the parameter type. This means the type is no\n // longer valid once it's added to a broader type union with other handlers (because as far\n // as TypeScript knows, we might call the handler with output from a different selector).\n //\n // This cast means the type system is not guaranteeing the handler is called with the matching\n // input selector return value. The parameter types do ensure they match when `subscribe` is\n // called, but past that point we need to make sure of that with manual review and tests\n // instead.\n const widenedHandler = handler as\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler;\n this.#subscribe(eventType, widenedHandler, { delegation: false, selector });\n\n if (selector) {\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n const initialValue = selector(...getPayload());\n this.#eventPayloadCache.set(widenedHandler, initialValue);\n }\n }\n }\n\n /**\n * Subscribe to an event.\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 * @param metadata - Event metadata.\n * @template SubscribedEvent - The event being subscribed to.\n * @template SelectorReturnValue - The selector return value.\n */\n #subscribe<SubscribedEvent extends EventConstraint>(\n eventType: SubscribedEvent['type'],\n handler:\n | ExtractEventHandler<SubscribedEvent, SubscribedEvent['type']>\n | SelectorEventHandler,\n metadata: SubscriptionMetadata<SubscribedEvent>,\n ): void {\n let subscribers = this.#events.get(eventType);\n if (!subscribers) {\n subscribers = new Map();\n this.#events.set(eventType, subscribers);\n }\n subscribers.set(handler, metadata);\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 * @template SelectorReturnValue - The selector return value.\n */\n unsubscribe<EventType extends Event['type'], SelectorReturnValue = unknown>(\n eventType: EventType,\n handler:\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler<SelectorReturnValue>,\n ) {\n const subscribers = this.#events.get(eventType);\n\n // Widen type of event handler by dropping ReturnType parameter.\n //\n // We need to drop it here because it's used as the parameter to the event handler, and\n // functions in general are contravariant over the parameter type. This means the type is no\n // longer valid once it's added to a broader type union with other handlers (because as far\n // as TypeScript knows, we might call the handler with output from a different selector).\n //\n // This poses no risk in this case, since we never call the handler past this point.\n const widenedHandler = handler as\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler;\n if (!subscribers) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n const metadata = subscribers.get(widenedHandler);\n if (!metadata) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n if (metadata.selector) {\n this.#eventPayloadCache.delete(widenedHandler);\n }\n\n subscribers.delete(widenedHandler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event registered from this messenger. The\n * event may still have subscribers if it has been delegated to another messenger.\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 const subscriptions = this.#events.get(eventType);\n if (!subscriptions) {\n return;\n }\n\n for (const [handler, metadata] of subscriptions.entries()) {\n if (metadata.delegation) {\n continue;\n }\n subscriptions.delete(handler);\n }\n\n if (subscriptions.size === 0) {\n this.#events.delete(eventType);\n }\n }\n\n /**\n * Clear all subscriptions.\n *\n * This will remove all subscribed handlers for all events registered from this messenger. Events\n * may still have subscribers if they are delegated to another messenger.\n */\n clearSubscriptions() {\n for (const eventType of this.#events.keys()) {\n this.clearEventSubscriptions(eventType);\n }\n }\n\n /**\n * Delegate actions and/or events to another messenger.\n *\n * The messenger these actions/events are delegated to will be able to call these actions and\n * subscribe to these events.\n *\n * Note that the messenger these actions/events are delegated to must still have these\n * actions/events included in its type definition (as part of the Action and Event type\n * parameters). Actions and events are statically type checked, they cannot be delegated\n * dynamically at runtime.\n *\n * @param args - Arguments.\n * @param args.actions - The action types to delegate.\n * @param args.events - The event types to delegate.\n * @param args.messenger - The messenger to delegate to.\n * @template Delegatee - The messenger the actions/events are delegated to.\n * @template DelegatedActions - An array of delegated action types.\n * @template DelegatedEvents - An array of delegated event types.\n */\n delegate<\n Delegatee extends Messenger<string, ActionConstraint, EventConstraint>,\n DelegatedActions extends (MessengerActions<Delegatee> & Action)['type'][],\n DelegatedEvents extends (MessengerEvents<Delegatee> & Event)['type'][],\n >({\n actions,\n events,\n messenger,\n }: {\n actions?: DelegatedActions;\n events?: DelegatedEvents;\n messenger: Delegatee;\n }) {\n for (const actionType of actions || []) {\n const delegatedActionHandler = (\n ...args: ExtractActionParameters<\n MessengerActions<Delegatee> & Action,\n typeof actionType\n >\n ) => {\n // Cast to get more specific type, for this specific action\n // The types get collapsed by `this.#actions`\n const actionHandler = this.#actions.get(actionType) as\n | ActionHandler<\n MessengerActions<Delegatee> & Action,\n typeof actionType\n >\n | undefined;\n if (!actionHandler) {\n throw new Error(\n `Cannot call '${actionType}', action not registered.`,\n );\n }\n return actionHandler(...args);\n };\n let delegationTargets = this.#actionDelegationTargets.get(actionType);\n if (!delegationTargets) {\n delegationTargets = new Set<DelegatedMessenger>();\n this.#actionDelegationTargets.set(actionType, delegationTargets);\n }\n if (delegationTargets.has(messenger)) {\n throw new Error(\n `The action '${actionType}' has already been delegated to this messenger`,\n );\n }\n delegationTargets.add(messenger);\n\n messenger._internalRegisterDelegatedActionHandler(\n actionType,\n delegatedActionHandler,\n );\n }\n for (const eventType of events || []) {\n const untypedSubscriber = (\n ...payload: ExtractEventPayload<\n MessengerEvents<Delegatee> & Event,\n typeof eventType\n >\n ) => {\n messenger._internalPublishDelegated(eventType, ...payload);\n };\n // Cast to get more specific subscriber type for this specific event.\n // The types get collapsed here to the type union of all delegated\n // events, rather than the single subscriber type corresponding to this\n // event.\n const subscriber = untypedSubscriber as ExtractEventHandler<\n MessengerEvents<Delegatee> & Event,\n typeof eventType\n >;\n let delegatedEventSubscriptions =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegatedEventSubscriptions) {\n delegatedEventSubscriptions = new Map();\n this.#subscriptionDelegationTargets.set(\n eventType,\n delegatedEventSubscriptions,\n );\n }\n if (delegatedEventSubscriptions.has(messenger)) {\n throw new Error(\n `The event '${eventType}' has already been delegated to this messenger`,\n );\n }\n delegatedEventSubscriptions.set(messenger, subscriber);\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n messenger._internalRegisterDelegatedInitialEventPayload({\n eventType,\n getPayload,\n });\n }\n\n this.#subscribe(eventType, subscriber, { delegation: true });\n }\n }\n\n /**\n * Revoke delegated actions and/or events from another messenger.\n *\n * The messenger these actions/events are delegated to will no longer be able to call these\n * actions or subscribe to these events.\n *\n * @param args - Arguments.\n * @param args.actions - The action types to revoke.\n * @param args.events - The event types to revoke.\n * @param args.messenger - The messenger these actions/events were delegated to.\n * @template Delegatee - The messenger the actions/events are being revoked from.\n * @template DelegatedActions - An array of delegated action types.\n * @template DelegatedEvents - An array of delegated event types.\n */\n revoke<\n Delegatee extends Messenger<string, ActionConstraint, EventConstraint>,\n DelegatedActions extends (MessengerActions<Delegatee> & Action)['type'][],\n DelegatedEvents extends (MessengerEvents<Delegatee> & Event)['type'][],\n >({\n actions,\n events,\n messenger,\n }: {\n actions?: DelegatedActions;\n events?: DelegatedEvents;\n messenger: Delegatee;\n }) {\n if (messenger === this.#parent) {\n throw new Error('Cannot revoke from parent');\n }\n for (const actionType of actions || []) {\n const delegationTargets = this.#actionDelegationTargets.get(actionType);\n if (!delegationTargets || !delegationTargets.has(messenger)) {\n // Nothing to revoke\n continue;\n }\n messenger._internalUnregisterDelegatedActionHandler(actionType);\n delegationTargets.delete(messenger);\n if (delegationTargets.size === 0) {\n this.#actionDelegationTargets.delete(actionType);\n }\n }\n for (const eventType of events || []) {\n const delegationTargets =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegationTargets) {\n // Nothing to revoke\n continue;\n }\n const delegatedSubscriber = delegationTargets.get(messenger);\n if (!delegatedSubscriber) {\n // Nothing to revoke\n continue;\n }\n this.unsubscribe(eventType, delegatedSubscriber);\n delegationTargets.delete(messenger);\n if (delegationTargets.size === 0) {\n this.#subscriptionDelegationTargets.delete(eventType);\n }\n }\n }\n\n /**\n * Register an action handler for an action delegated from another messenger.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalRegisterDelegatedActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n // Using wider `ActionConstraint` type here rather than `Action` because the `Action` type is\n // contravariant over the handler parameter type. Using `Action` would lead to a type error\n // here because the messenger we've delegated to supports _additional_ actions.\n handler: ActionHandler<ActionConstraint, ActionType>,\n ) {\n this.#registerActionHandler(actionType, handler);\n }\n\n /**\n * Unregister an action handler for an action delegated from another messenger.\n *\n * This will prevent this action from being called.\n *\n * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @template ActionType - A type union of Action type strings.\n */\n _internalUnregisterDelegatedActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#unregisterActionHandler(actionType);\n }\n\n /**\n * Register a function for getting the initial payload for an event that has been delegated from\n * another messenger.\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 * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalRegisterDelegatedInitialEventPayload<\n EventType extends Event['type'],\n >({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n this.#registerInitialEventPayload({ eventType, getPayload });\n }\n\n /**\n * Publish an event that was delegated from another messenger.\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 * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalPublishDelegated<EventType extends Event['type']>(\n eventType: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n this.#publish(eventType, ...payload);\n }\n\n /**\n * Determine whether the given name is within the current namespace.\n *\n * @param name - The name to check\n * @returns Whether the name is within the current namespace\n */\n #isInCurrentNamespace(name: string): name is NamespacedName<Namespace> {\n return name.startsWith(`${this.#namespace}:`);\n }\n}\n"]}
{"version":3,"file":"Messenger.mjs","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":";;;;;;;;;;;;AAgGA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,oBAAoB,CAAC;AAiFvD;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,SAAS;IA4DpB;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,MAAM,GAQP;;QAlEQ,uCAAsB;QAE/B;;;WAGG;QACM,oCAA6B;QAE7B,6BAAW,IAAI,GAAG,EAAqC,EAAC;QAExD,4BAAU,IAAI,GAAG,EAA8C,EAAC;QAEzE;;WAEG;QACM,mDAAiC,IAAI,GAAG,EAG9C,EAAC;QAEJ;;WAEG;QACM,6CAA2B,IAAI,GAAG,EAGxC,EAAC;QAEJ;;;;WAIG;QACM,gDAA8B,IAAI,GAAG,EAG3C,EAAC;QAEJ;;WAEG;QACM,uCAAqB,IAAI,GAAG,EAGlC,EAAC;QAuBF,uBAAA,IAAI,wBAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,qBAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,qBAAqB,CAEnB,UAAsB,EAAE,OAA0C;QAClE,IAAI,CAAC,uBAAA,IAAI,6DAAsB,MA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ACF,IAAI,QAAQ,EAAE;oBACZ,MAAM,aAAa,GAAG,uBAAA,IAAI,oCAAmB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;oBAC3D,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,OAAO,CAAC,CAAC;oBAEtC,IAAI,QAAQ,KAAK,aAAa,EAAE;wBAC9B,uBAAA,IAAI,oCAAmB,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;wBAC/C,OAAO,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC;qBAClC;iBACF;qBAAM;oBACJ,OAA+B,CAAC,GAAG,OAAO,CAAC,CAAC;iBAC9C;aACF;YAAC,OAAO,KAAK,EAAE;gBACd,qEAAqE;gBACrE,6DAA6D;gBAC7D,UAAU,CAAC,GAAG,EAAE;oBACd,MAAM,KAAK,CAAC;gBACd,CAAC,CAAC,CAAC;aACJ;SACF;KACF;AACH,CAAC,uDAmFC,SAAkC,EAClC,OAEwB,EACxB,QAA+C;IAE/C,IAAI,WAAW,GAAG,uBAAA,IAAI,yBAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC9C,IAAI,CAAC,WAAW,EAAE;QAChB,WAAW,GAAG,IAAI,GAAG,EAAE,CAAC;QACxB,uBAAA,IAAI,yBAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;KAC1C;IACD,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AACrC,CAAC,6EAmXqB,IAAY;IAChC,OAAO,CACL,uBAAA,IAAI,4BAAW,KAAK,kBAAkB;QACtC,IAAI,CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,4BAAW,GAAG,CAAC,CACvC,CAAC;AACJ,CAAC","sourcesContent":["export 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 = unknown,\n> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue = unknown> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: NamespacedName;\n handler: ((...args: never) => unknown) | ((...args: never[]) => unknown);\n};\nexport type EventConstraint = {\n type: NamespacedName;\n payload: unknown[];\n};\n\n/**\n * Extract action types from a Messenger type.\n *\n * @template Subject - The messenger type to extract from.\n */\nexport type MessengerActions<\n Subject extends Messenger<string, ActionConstraint, EventConstraint>,\n> =\n Subject extends Messenger<string, infer Action, EventConstraint>\n ? Action\n : never;\n\n/**\n * Extract event types from a Messenger type.\n *\n * @template Subject - The messenger type to extract from.\n */\nexport type MessengerEvents<\n Subject extends Messenger<string, ActionConstraint, EventConstraint>,\n> =\n Subject extends Messenger<string, ActionConstraint, infer Event>\n ? Event\n : never;\n\n/**\n * Messenger namespace checks can be disabled by using this as the `namespace` constructor\n * parameter, and using `MockAnyNamespace` as the Namespace type parameter.\n *\n * This is useful for mocking a variety of different actions/events in unit tests. Please do not\n * use this in production code.\n */\nexport const MOCK_ANY_NAMESPACE = 'MOCK_ANY_NAMESPACE';\n\n/**\n * A type representing any namespace.\n *\n * This is useful for mocking a variety of different actions/events in unit tests. Please do not\n * use this in production code.\n */\nexport type MockAnyNamespace = string;\n\n/**\n * Metadata for a single event subscription.\n *\n * @template Event - The event this subscription is for.\n */\ntype SubscriptionMetadata<Event extends EventConstraint> = {\n /**\n * Whether this subscription is for a delegated messenger. Delegation subscriptions are ignored\n * when clearing subscriptions.\n */\n delegation: boolean;\n /**\n * The optional selector function for this subscription.\n */\n selector?: SelectorFunction<Event, Event['type']>;\n};\n\n/**\n * A map of event handlers for a specific event.\n *\n * The key is the handler function, and the value contains additional subscription metadata.\n *\n * @template Event - The event these handlers are for.\n */\ntype EventSubscriptionMap<Event extends EventConstraint> = Map<\n GenericEventHandler | SelectorEventHandler,\n SubscriptionMetadata<Event>\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\n/**\n * A messenger that actions and/or events can be delegated to.\n *\n * This is a minimal type interface to avoid complex incompatibilities resulting from generics over\n * invariant types.\n */\ntype DelegatedMessenger = Pick<\n // The type is broadened to all actions/events because some messenger methods are contravariant\n // over this type (`registerDelegatedActionHandler` and `publishDelegated` for example). If this\n // type is narrowed to just the delegated actions/events, the types for event payload and action\n // parameters would not be wide enough.\n Messenger<string, ActionConstraint, EventConstraint>,\n | '_internalPublishDelegated'\n | '_internalRegisterDelegatedActionHandler'\n | '_internalRegisterDelegatedInitialEventPayload'\n | '_internalUnregisterDelegatedActionHandler'\n>;\n\ntype StripNamespace<Namespaced extends NamespacedName> =\n Namespaced extends `${string}:${infer Name}` ? Name : 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 prefixed by a namespace (which is delimited by a colon, e.g.\n * `Namespace:actionName`).\n *\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template Namespace - The namespace for the messenger.\n */\nexport class Messenger<\n Namespace extends string,\n Action extends ActionConstraint = never,\n Event extends EventConstraint = never,\n Parent extends Messenger<\n string,\n ActionConstraint,\n EventConstraint,\n // Use `any` to avoid preventing a parent from having a parent. `any` is harmless in a type\n // constraint anyway, it's the one totally safe place to use it.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n > = never,\n> {\n readonly #namespace: Namespace;\n\n /**\n * The parent messenger. All actions/events under this namespace are automatically delegated to\n * the parent messenger.\n */\n readonly #parent?: DelegatedMessenger;\n\n readonly #actions = new Map<Action['type'], Action['handler']>();\n\n readonly #events = new Map<Event['type'], EventSubscriptionMap<Event>>();\n\n /**\n * The set of messengers we've delegated events to and their event handlers, by event type.\n */\n readonly #subscriptionDelegationTargets = new Map<\n Event['type'],\n Map<DelegatedMessenger, ExtractEventHandler<Event, Event['type']>>\n >();\n\n /**\n * The set of messengers we've delegated actions to, by action type.\n */\n readonly #actionDelegationTargets = new Map<\n Action['type'],\n Set<DelegatedMessenger>\n >();\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 * Construct a messenger.\n *\n * If a parent messenger is given, all actions and events under this messenger's namespace will\n * be delegated to the parent automatically.\n *\n * @param args - Constructor arguments\n * @param args.namespace - The messenger namespace.\n * @param args.parent - The parent messenger.\n */\n constructor({\n namespace,\n parent,\n }: {\n namespace: Namespace;\n parent?: Action['type'] extends MessengerActions<Parent>['type']\n ? Event['type'] extends MessengerEvents<Parent>['type']\n ? Parent\n : never\n : never;\n }) {\n this.#namespace = namespace;\n this.#parent = parent;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action being registered must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n registerActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(actionType: ActionType, handler: ActionHandler<Action, ActionType>) {\n if (!this.#isInCurrentNamespace(actionType)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#registerActionHandler(actionType, handler);\n if (this.#parent) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // action, but this is OK because it's validated in the constructor.\n this.delegate({ actions: [actionType], messenger: this.#parent });\n }\n }\n\n #registerActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n handler: ActionHandler<ActionConstraint, 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 * @template MessengerClient - The type expected to make use of the messenger.\n * @template MethodNames - The type union of method names to register as action handlers.\n */\n registerMethodActionHandlers<\n MessengerClient extends { name: Namespace },\n MethodNames extends keyof MessengerClient & StripNamespace<Action['type']>,\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 * The action being unregistered must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n unregisterActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(actionType: ActionType) {\n if (!this.#isInCurrentNamespace(actionType)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#unregisterActionHandler(actionType);\n }\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 for (const actionType of this.#actions.keys()) {\n this.#unregisterActionHandler(actionType);\n }\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 * The event type must be under the same namespace as the messenger.\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 * @template EventType - A type union of Event type strings under this messenger's namespace.\n */\n registerInitialEventPayload<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n if (!this.#isInCurrentNamespace(eventType)) {\n throw new Error(\n `Only allowed registering initial payloads for events prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n if (\n this.#parent &&\n !this.#subscriptionDelegationTargets.get(eventType)?.has(this.#parent)\n ) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // event, but this is OK because it's validated in the constructor.\n this.delegate({ events: [eventType], messenger: this.#parent });\n }\n this.#registerInitialEventPayload({ eventType, getPayload });\n }\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 const delegationTargets =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegationTargets) {\n return;\n }\n for (const messenger of delegationTargets.keys()) {\n messenger._internalRegisterDelegatedInitialEventPayload({\n eventType,\n getPayload,\n });\n }\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 * The event being published must be under the same namespace as the messenger.\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 under this messenger's namespace.\n */\n publish<EventType extends Event['type'] & NamespacedName<Namespace>>(\n eventType: EventType & NamespacedName<Namespace>,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n if (!this.#isInCurrentNamespace(eventType)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n if (\n this.#parent &&\n !this.#subscriptionDelegationTargets.get(eventType)?.has(this.#parent)\n ) {\n // @ts-expect-error The parent type isn't constructed in a way that proves it supports this\n // event, but this is OK because it's validated in the constructor.\n this.delegate({ events: [eventType], messenger: this.#parent });\n }\n this.#publish(eventType, ...payload);\n }\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:\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler<SelectorReturnValue>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void {\n // Widen type of event handler by dropping ReturnType parameter.\n //\n // We need to drop it here because it's used as the parameter to the event handler, and\n // functions in general are contravariant over the parameter type. This means the type is no\n // longer valid once it's added to a broader type union with other handlers (because as far\n // as TypeScript knows, we might call the handler with output from a different selector).\n //\n // This cast means the type system is not guaranteeing the handler is called with the matching\n // input selector return value. The parameter types do ensure they match when `subscribe` is\n // called, but past that point we need to make sure of that with manual review and tests\n // instead.\n const widenedHandler = handler as\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler;\n this.#subscribe(eventType, widenedHandler, { delegation: false, selector });\n\n if (selector) {\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n const initialValue = selector(...getPayload());\n this.#eventPayloadCache.set(widenedHandler, initialValue);\n }\n }\n }\n\n /**\n * Subscribe to an event.\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 * @param metadata - Event metadata.\n * @template SubscribedEvent - The event being subscribed to.\n * @template SelectorReturnValue - The selector return value.\n */\n #subscribe<SubscribedEvent extends EventConstraint>(\n eventType: SubscribedEvent['type'],\n handler:\n | ExtractEventHandler<SubscribedEvent, SubscribedEvent['type']>\n | SelectorEventHandler,\n metadata: SubscriptionMetadata<SubscribedEvent>,\n ): void {\n let subscribers = this.#events.get(eventType);\n if (!subscribers) {\n subscribers = new Map();\n this.#events.set(eventType, subscribers);\n }\n subscribers.set(handler, metadata);\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 * @template SelectorReturnValue - The selector return value.\n */\n unsubscribe<EventType extends Event['type'], SelectorReturnValue = unknown>(\n eventType: EventType,\n handler:\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler<SelectorReturnValue>,\n ) {\n const subscribers = this.#events.get(eventType);\n\n // Widen type of event handler by dropping ReturnType parameter.\n //\n // We need to drop it here because it's used as the parameter to the event handler, and\n // functions in general are contravariant over the parameter type. This means the type is no\n // longer valid once it's added to a broader type union with other handlers (because as far\n // as TypeScript knows, we might call the handler with output from a different selector).\n //\n // This poses no risk in this case, since we never call the handler past this point.\n const widenedHandler = handler as\n | ExtractEventHandler<Event, EventType>\n | SelectorEventHandler;\n if (!subscribers) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n const metadata = subscribers.get(widenedHandler);\n if (!metadata) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n if (metadata.selector) {\n this.#eventPayloadCache.delete(widenedHandler);\n }\n\n subscribers.delete(widenedHandler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event registered from this messenger. The\n * event may still have subscribers if it has been delegated to another messenger.\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 const subscriptions = this.#events.get(eventType);\n if (!subscriptions) {\n return;\n }\n\n for (const [handler, metadata] of subscriptions.entries()) {\n if (metadata.delegation) {\n continue;\n }\n subscriptions.delete(handler);\n }\n\n if (subscriptions.size === 0) {\n this.#events.delete(eventType);\n }\n }\n\n /**\n * Clear all subscriptions.\n *\n * This will remove all subscribed handlers for all events registered from this messenger. Events\n * may still have subscribers if they are delegated to another messenger.\n */\n clearSubscriptions() {\n for (const eventType of this.#events.keys()) {\n this.clearEventSubscriptions(eventType);\n }\n }\n\n /**\n * Delegate actions and/or events to another messenger.\n *\n * The messenger these actions/events are delegated to will be able to call these actions and\n * subscribe to these events.\n *\n * Note that the messenger these actions/events are delegated to must still have these\n * actions/events included in its type definition (as part of the Action and Event type\n * parameters). Actions and events are statically type checked, they cannot be delegated\n * dynamically at runtime.\n *\n * @param args - Arguments.\n * @param args.actions - The action types to delegate.\n * @param args.events - The event types to delegate.\n * @param args.messenger - The messenger to delegate to.\n * @template Delegatee - The messenger the actions/events are delegated to.\n * @template DelegatedActions - An array of delegated action types.\n * @template DelegatedEvents - An array of delegated event types.\n */\n delegate<\n Delegatee extends Messenger<string, ActionConstraint, EventConstraint>,\n DelegatedActions extends (MessengerActions<Delegatee> & Action)['type'][],\n DelegatedEvents extends (MessengerEvents<Delegatee> & Event)['type'][],\n >({\n actions,\n events,\n messenger,\n }: {\n actions?: DelegatedActions;\n events?: DelegatedEvents;\n messenger: Delegatee;\n }) {\n for (const actionType of actions || []) {\n const delegatedActionHandler = (\n ...args: ExtractActionParameters<\n MessengerActions<Delegatee> & Action,\n typeof actionType\n >\n ) => {\n // Cast to get more specific type, for this specific action\n // The types get collapsed by `this.#actions`\n const actionHandler = this.#actions.get(actionType) as\n | ActionHandler<\n MessengerActions<Delegatee> & Action,\n typeof actionType\n >\n | undefined;\n if (!actionHandler) {\n throw new Error(\n `A handler for ${actionType} has not been registered`,\n );\n }\n return actionHandler(...args);\n };\n let delegationTargets = this.#actionDelegationTargets.get(actionType);\n if (!delegationTargets) {\n delegationTargets = new Set<DelegatedMessenger>();\n this.#actionDelegationTargets.set(actionType, delegationTargets);\n }\n if (delegationTargets.has(messenger)) {\n throw new Error(\n `The action '${actionType}' has already been delegated to this messenger`,\n );\n }\n delegationTargets.add(messenger);\n\n messenger._internalRegisterDelegatedActionHandler(\n actionType,\n delegatedActionHandler,\n );\n }\n for (const eventType of events || []) {\n const untypedSubscriber = (\n ...payload: ExtractEventPayload<\n MessengerEvents<Delegatee> & Event,\n typeof eventType\n >\n ) => {\n messenger._internalPublishDelegated(eventType, ...payload);\n };\n // Cast to get more specific subscriber type for this specific event.\n // The types get collapsed here to the type union of all delegated\n // events, rather than the single subscriber type corresponding to this\n // event.\n const subscriber = untypedSubscriber as ExtractEventHandler<\n MessengerEvents<Delegatee> & Event,\n typeof eventType\n >;\n let delegatedEventSubscriptions =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegatedEventSubscriptions) {\n delegatedEventSubscriptions = new Map();\n this.#subscriptionDelegationTargets.set(\n eventType,\n delegatedEventSubscriptions,\n );\n }\n if (delegatedEventSubscriptions.has(messenger)) {\n throw new Error(\n `The event '${eventType}' has already been delegated to this messenger`,\n );\n }\n delegatedEventSubscriptions.set(messenger, subscriber);\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n messenger._internalRegisterDelegatedInitialEventPayload({\n eventType,\n getPayload,\n });\n }\n\n this.#subscribe(eventType, subscriber, { delegation: true });\n }\n }\n\n /**\n * Revoke delegated actions and/or events from another messenger.\n *\n * The messenger these actions/events are delegated to will no longer be able to call these\n * actions or subscribe to these events.\n *\n * @param args - Arguments.\n * @param args.actions - The action types to revoke.\n * @param args.events - The event types to revoke.\n * @param args.messenger - The messenger these actions/events were delegated to.\n * @template Delegatee - The messenger the actions/events are being revoked from.\n * @template DelegatedActions - An array of delegated action types.\n * @template DelegatedEvents - An array of delegated event types.\n */\n revoke<\n Delegatee extends Messenger<string, ActionConstraint, EventConstraint>,\n DelegatedActions extends (MessengerActions<Delegatee> & Action)['type'][],\n DelegatedEvents extends (MessengerEvents<Delegatee> & Event)['type'][],\n >({\n actions,\n events,\n messenger,\n }: {\n actions?: DelegatedActions;\n events?: DelegatedEvents;\n messenger: Delegatee;\n }) {\n if (messenger === this.#parent) {\n throw new Error('Cannot revoke from parent');\n }\n for (const actionType of actions || []) {\n const delegationTargets = this.#actionDelegationTargets.get(actionType);\n if (!delegationTargets || !delegationTargets.has(messenger)) {\n // Nothing to revoke\n continue;\n }\n messenger._internalUnregisterDelegatedActionHandler(actionType);\n delegationTargets.delete(messenger);\n if (delegationTargets.size === 0) {\n this.#actionDelegationTargets.delete(actionType);\n }\n }\n for (const eventType of events || []) {\n const delegationTargets =\n this.#subscriptionDelegationTargets.get(eventType);\n if (!delegationTargets) {\n // Nothing to revoke\n continue;\n }\n const delegatedSubscriber = delegationTargets.get(messenger);\n if (!delegatedSubscriber) {\n // Nothing to revoke\n continue;\n }\n this.unsubscribe(eventType, delegatedSubscriber);\n delegationTargets.delete(messenger);\n if (delegationTargets.size === 0) {\n this.#subscriptionDelegationTargets.delete(eventType);\n }\n }\n }\n\n /**\n * Register an action handler for an action delegated from another messenger.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalRegisterDelegatedActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n // Using wider `ActionConstraint` type here rather than `Action` because the `Action` type is\n // contravariant over the handler parameter type. Using `Action` would lead to a type error\n // here because the messenger we've delegated to supports _additional_ actions.\n handler: ActionHandler<ActionConstraint, ActionType>,\n ) {\n this.#registerActionHandler(actionType, handler);\n }\n\n /**\n * Unregister an action handler for an action delegated from another messenger.\n *\n * This will prevent this action from being called.\n *\n * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @template ActionType - A type union of Action type strings.\n */\n _internalUnregisterDelegatedActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#unregisterActionHandler(actionType);\n }\n\n /**\n * Register a function for getting the initial payload for an event that has been delegated from\n * another messenger.\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 * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalRegisterDelegatedInitialEventPayload<\n EventType extends Event['type'],\n >({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n this.#registerInitialEventPayload({ eventType, getPayload });\n }\n\n /**\n * Publish an event that was delegated from another messenger.\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 * Note: This is an internal method. Never access this property from another module. This must be\n * exposed as a public property so that these methods can be called internally on other messenger\n * instances.\n *\n * @deprecated Internal use only. Use the `delegate` method for delegation.\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 _internalPublishDelegated<EventType extends Event['type']>(\n eventType: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n this.#publish(eventType, ...payload);\n }\n\n /**\n * Determine whether the given name is within the current namespace.\n *\n * If the current namespace is MOCK_ANY_NAMESPACE, this check always returns true.\n *\n * @param name - The name to check\n * @returns Whether the name is within the current namespace\n */\n #isInCurrentNamespace(name: string): name is NamespacedName<Namespace> {\n return (\n this.#namespace === MOCK_ANY_NAMESPACE ||\n name.startsWith(`${this.#namespace}:`)\n );\n }\n}\n"]}
{
"name": "@metamask/messenger",
"version": "0.1.0",
"version": "0.2.0",
"description": "A type-safe message bus library",

@@ -5,0 +5,0 @@ "keywords": [