@metamask-previews/base-controller 8.0.1-preview-c2d112dc → 8.0.1-preview-fd1ef74

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+
12
+ - Add `registerMethodActionHandlers` method to `Messenger`, and `RestrictedMessenger` for simplified bulk action handler registration ([#5927](https://github.com/MetaMask/core/pull/5927))
13
+ - Allows registering action handlers that map to methods on a messenger client at once by passing an array of method names
14
+ - Automatically binds action handlers to the given messenger client
15
+
10
16
  ### Changed
11
17
 
12
18
  - Bump `@metamask/utils` from `^11.2.0` to `^11.4.2` ([#6054](https://github.com/MetaMask/core/pull/6054))
@@ -38,7 +38,7 @@ class Messenger {
38
38
  *
39
39
  * This will make the registered function available to call via the `call` method.
40
40
  *
41
- * @param actionType - The action type. This is a unqiue identifier for this action.
41
+ * @param actionType - The action type. This is a unique identifier for this action.
42
42
  * @param handler - The action handler. This function gets called when the `call` method is
43
43
  * invoked with the given action type.
44
44
  * @throws Will throw when a handler has been registered for this action type already.
@@ -50,12 +50,28 @@ class Messenger {
50
50
  }
51
51
  __classPrivateFieldGet(this, _Messenger_actions, "f").set(actionType, handler);
52
52
  }
53
+ /**
54
+ * Registers action handlers for a list of methods on a messenger client
55
+ *
56
+ * @param messengerClient - The object that is expected to make use of the messenger.
57
+ * @param methodNames - The names of the methods on the messenger client to register as action
58
+ * handlers
59
+ */
60
+ registerMethodActionHandlers(messengerClient, methodNames) {
61
+ for (const methodName of methodNames) {
62
+ const method = messengerClient[methodName];
63
+ if (typeof method === 'function') {
64
+ const actionType = `${messengerClient.name}:${methodName}`;
65
+ this.registerActionHandler(actionType, method.bind(messengerClient));
66
+ }
67
+ }
68
+ }
53
69
  /**
54
70
  * Unregister an action handler.
55
71
  *
56
72
  * This will prevent this action from being called.
57
73
  *
58
- * @param actionType - The action type. This is a unqiue identifier for this action.
74
+ * @param actionType - The action type. This is a unique identifier for this action.
59
75
  * @template ActionType - A type union of Action type strings.
60
76
  */
61
77
  unregisterActionHandler(actionType) {
@@ -75,7 +91,7 @@ class Messenger {
75
91
  * This function will call the action handler corresponding to the given action type, passing
76
92
  * along any parameters given.
77
93
  *
78
- * @param actionType - The action type. This is a unqiue identifier for this action.
94
+ * @param actionType - The action type. This is a unique identifier for this action.
79
95
  * @param params - The action parameters. These must match the type of the parameters of the
80
96
  * registered action handler.
81
97
  * @throws Will throw when no handler has been registered for the given type.
@@ -1 +1 @@
1
- {"version":3,"file":"Messenger.cjs","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":";;;;;;;;;AAAA,mEAA4D;AAmH5D;;;;;;;;;GASG;AACH,MAAa,SAAS;IAAtB;QAIW,6BAAW,IAAI,GAAG,EAA2B,EAAC;QAE9C,4BAAU,IAAI,GAAG,EAA8C,EAAC;QAEzE;;;;WAIG;QACM,gDAA8B,IAAI,GAAG,EAG3C,EAAC;QAEJ;;WAEG;QACM,uCAAqB,IAAI,GAAG,EAGlC,EAAC;IAsTN,CAAC;IApTC;;;;;;;;;;OAUG;IACH,qBAAqB,CACnB,UAAsB,EACtB,OAA0C;QAE1C,IAAI,uBAAA,IAAI,0BAAS,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE;YACjC,MAAM,IAAI,KAAK,CACb,iBAAiB,UAAU,8BAA8B,CAC1D,CAAC;SACH;QACD,uBAAA,IAAI,0BAAS,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;IAED;;;;;;;OAOG;IACH,uBAAuB,CACrB,UAAsB;QAEtB,uBAAA,IAAI,0BAAS,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACH,YAAY;QACV,uBAAA,IAAI,0BAAS,CAAC,KAAK,EAAE,CAAC;IACxB,CA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AO,CAAC,CAAC;IAC9B,CAAC;IAED;;;;;;;OAOG;IACH,uBAAuB,CACrB,SAAoB;QAEpB,uBAAA,IAAI,yBAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,kBAAkB;QAChB,uBAAA,IAAI,yBAAQ,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,aAAa,CAIX,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GAWd;QAQC,OAAO,IAAI,yCAAmB,CAAC;YAC7B,SAAS,EAAE,IAAI;YACf,IAAI;YACJ,cAAc;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF;AA9UD,8BA8UC","sourcesContent":["import { RestrictedMessenger } from './RestrictedMessenger';\n\nexport type ActionHandler<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = (\n ...args: ExtractActionParameters<Action, ActionType>\n) => ExtractActionResponse<Action, ActionType>;\n\nexport type ExtractActionParameters<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = Action extends {\n type: ActionType;\n handler: (...args: infer HandlerArgs) => unknown;\n}\n ? HandlerArgs\n : never;\n\nexport type ExtractActionResponse<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = Action extends {\n type: ActionType;\n handler: (...args: infer _) => infer HandlerReturnValue;\n}\n ? HandlerReturnValue\n : never;\n\nexport type ExtractEventHandler<\n Event extends EventConstraint,\n EventType = Event['type'],\n> = Event extends {\n type: EventType;\n payload: infer Payload;\n}\n ? Payload extends unknown[]\n ? (...payload: Payload) => void\n : never\n : never;\n\nexport type ExtractEventPayload<\n Event extends EventConstraint,\n EventType = Event['type'],\n> = Event extends {\n type: EventType;\n payload: infer Payload;\n}\n ? Payload extends unknown[]\n ? Payload\n : never\n : never;\n\nexport type GenericEventHandler = (...args: unknown[]) => void;\n\nexport type SelectorFunction<\n Event extends EventConstraint,\n EventType extends Event['type'],\n ReturnValue,\n> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: string;\n handler: ((...args: never) => unknown) | ((...args: never[]) => unknown);\n};\nexport type EventConstraint = {\n type: string;\n payload: unknown[];\n};\n\ntype EventSubscriptionMap<\n Event extends EventConstraint,\n ReturnValue = unknown,\n> = Map<\n GenericEventHandler | SelectorEventHandler<ReturnValue>,\n SelectorFunction<Event, Event['type'], ReturnValue> | undefined\n>;\n\n/**\n * A namespaced string\n *\n * This type verifies that the string Name is prefixed by the string Name followed by a colon.\n *\n * @template Namespace - The namespace we're checking for.\n * @template Name - The full string, including the namespace.\n */\nexport type NamespacedBy<\n Namespace extends string,\n Name extends string,\n> = Name extends `${Namespace}:${string}` ? Name : never;\n\nexport type NotNamespacedBy<\n Namespace extends string,\n Name extends string,\n> = Name extends `${Namespace}:${string}` ? never : Name;\n\nexport type NamespacedName<Namespace extends string = string> =\n `${Namespace}:${string}`;\n\ntype NarrowToNamespace<Name, Namespace extends string> = Name extends {\n type: `${Namespace}:${string}`;\n}\n ? Name\n : never;\n\ntype NarrowToAllowed<Name, Allowed extends string> = Name extends {\n type: Allowed;\n}\n ? Name\n : never;\n\n/**\n * A message broker for \"actions\" and \"events\".\n *\n * The messenger allows registering functions as 'actions' that can be called elsewhere,\n * and it allows publishing and subscribing to events. Both actions and events are identified by\n * unique strings.\n *\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n */\nexport class Messenger<\n Action extends ActionConstraint,\n Event extends EventConstraint,\n> {\n readonly #actions = new Map<Action['type'], unknown>();\n\n readonly #events = new Map<Event['type'], EventSubscriptionMap<Event>>();\n\n /**\n * A map of functions for getting the initial event payload.\n *\n * Used only for events that represent state changes.\n */\n readonly #initialEventPayloadGetters = new Map<\n Event['type'],\n () => ExtractEventPayload<Event, Event['type']>\n >();\n\n /**\n * A cache of selector return values for their respective handlers.\n */\n readonly #eventPayloadCache = new Map<\n GenericEventHandler,\n unknown | undefined\n >();\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @param handler - The action handler. This function gets called when the `call` method is\n * invoked with the given action type.\n * @throws Will throw when a handler has been registered for this action type already.\n * @template ActionType - A type union of Action type strings.\n */\n registerActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n handler: ActionHandler<Action, ActionType>,\n ) {\n if (this.#actions.has(actionType)) {\n throw new Error(\n `A handler for ${actionType} has already been registered`,\n );\n }\n this.#actions.set(actionType, handler);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @template ActionType - A type union of Action type strings.\n */\n unregisterActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#actions.delete(actionType);\n }\n\n /**\n * Unregister all action handlers.\n *\n * This prevents all actions from being called.\n */\n clearActions() {\n this.#actions.clear();\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @param params - The action parameters. These must match the type of the parameters of the\n * registered action handler.\n * @throws Will throw when no handler has been registered for the given type.\n * @template ActionType - A type union of Action type strings.\n * @returns The action return value.\n */\n call<ActionType extends Action['type']>(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n const handler = this.#actions.get(actionType) as ActionHandler<\n Action,\n ActionType\n >;\n if (!handler) {\n throw new Error(`A handler for ${actionType} has not been registered`);\n }\n return handler(...params);\n }\n\n /**\n * Register a function for getting the initial payload for an event.\n *\n * This is used for events that represent a state change, where the payload is the state.\n * Registering a function for getting the payload allows event selectors to have a point of\n * comparison the first time state changes.\n *\n * @param args - The arguments to this function\n * @param args.eventType - The event type to register a payload for.\n * @param args.getPayload - A function for retrieving the event payload.\n */\n registerInitialEventPayload<EventType extends Event['type']>({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n this.#initialEventPayloadGetters.set(eventType, getPayload);\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * Note that this method should never throw directly. Any errors from\n * subscribers are captured and re-thrown in a timeout handler.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @template EventType - A type union of Event type strings.\n */\n publish<EventType extends Event['type']>(\n eventType: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n const subscribers = this.#events.get(eventType);\n\n if (subscribers) {\n for (const [handler, selector] of subscribers.entries()) {\n try {\n if (selector) {\n const previousValue = this.#eventPayloadCache.get(handler);\n const newValue = selector(...payload);\n\n if (newValue !== previousValue) {\n this.#eventPayloadCache.set(handler, newValue);\n handler(newValue, previousValue);\n }\n } else {\n (handler as GenericEventHandler)(...payload);\n }\n } catch (error) {\n // Throw error after timeout so that it is capured as a console error\n // (and by Sentry) without interrupting the event publishing.\n setTimeout(() => {\n throw error;\n });\n }\n }\n }\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @template EventType - A type union of Event type strings.\n */\n subscribe<EventType extends Event['type']>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void {\n let subscribers = this.#events.get(eventType);\n if (!subscribers) {\n subscribers = new Map();\n this.#events.set(eventType, subscribers);\n }\n\n subscribers.set(handler, selector);\n\n if (selector) {\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n const initialValue = selector(...getPayload());\n this.#eventPayloadCache.set(handler, initialValue);\n }\n }\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw when the given event handler is not registered for this event.\n * @template EventType - A type union of Event type strings.\n */\n unsubscribe<EventType extends Event['type']>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ) {\n const subscribers = this.#events.get(eventType);\n\n if (!subscribers || !subscribers.has(handler)) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n\n const selector = subscribers.get(handler);\n if (selector) {\n this.#eventPayloadCache.delete(handler);\n }\n\n subscribers.delete(handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @template EventType - A type union of Event type strings.\n */\n clearEventSubscriptions<EventType extends Event['type']>(\n eventType: EventType,\n ) {\n this.#events.delete(eventType);\n }\n\n /**\n * Clear all subscriptions.\n *\n * This will remove all subscribed handlers for all events.\n */\n clearSubscriptions() {\n this.#events.clear();\n }\n\n /**\n * Get a restricted messenger\n *\n * Returns a wrapper around the messenger instance that restricts access to actions and events.\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Messenger options.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted messenger returned.\n * @param options.allowedActions - The list of actions that this restricted messenger should be\n * allowed to call.\n * @param options.allowedEvents - The list of events that this restricted messenger should be\n * allowed to subscribe to.\n * @template Namespace - The namespace for this messenger. Typically this is the name of the\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * This must not include internal actions that are in the messenger's namespace.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * This must not include internal events that are in the messenger's namespace.\n * @returns The restricted messenger.\n */\n getRestricted<\n Namespace extends string,\n AllowedAction extends NotNamespacedBy<Namespace, Action['type']> = never,\n AllowedEvent extends NotNamespacedBy<Namespace, Event['type']> = never,\n >({\n name,\n allowedActions,\n allowedEvents,\n }: {\n name: Namespace;\n allowedActions: NotNamespacedBy<\n Namespace,\n Extract<Action['type'], AllowedAction>\n >[];\n allowedEvents: NotNamespacedBy<\n Namespace,\n Extract<Event['type'], AllowedEvent>\n >[];\n }): RestrictedMessenger<\n Namespace,\n | NarrowToNamespace<Action, Namespace>\n | NarrowToAllowed<Action, AllowedAction>,\n NarrowToNamespace<Event, Namespace> | NarrowToAllowed<Event, AllowedEvent>,\n AllowedAction,\n AllowedEvent\n > {\n return new RestrictedMessenger({\n messenger: this,\n name,\n allowedActions,\n allowedEvents,\n });\n }\n}\n"]}
1
+ {"version":3,"file":"Messenger.cjs","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":";;;;;;;;;AAAA,mEAA4D;AAmH5D;;;;;;;;;GASG;AACH,M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C;IACjC,CAAC;IAED;;;;OAIG;IACH,kBAAkB;QAChB,uBAAA,IAAI,yBAAQ,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,aAAa,CAIX,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GAWd;QAQC,OAAO,IAAI,yCAAmB,CAAC;YAC7B,SAAS,EAAE,IAAI;YACf,IAAI;YACJ,cAAc;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF;AAlWD,8BAkWC","sourcesContent":["import { RestrictedMessenger } from './RestrictedMessenger';\n\nexport type ActionHandler<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = (\n ...args: ExtractActionParameters<Action, ActionType>\n) => ExtractActionResponse<Action, ActionType>;\n\nexport type ExtractActionParameters<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = Action extends {\n type: ActionType;\n handler: (...args: infer HandlerArgs) => unknown;\n}\n ? HandlerArgs\n : never;\n\nexport type ExtractActionResponse<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = Action extends {\n type: ActionType;\n handler: (...args: infer _) => infer HandlerReturnValue;\n}\n ? HandlerReturnValue\n : never;\n\nexport type ExtractEventHandler<\n Event extends EventConstraint,\n EventType = Event['type'],\n> = Event extends {\n type: EventType;\n payload: infer Payload;\n}\n ? Payload extends unknown[]\n ? (...payload: Payload) => void\n : never\n : never;\n\nexport type ExtractEventPayload<\n Event extends EventConstraint,\n EventType = Event['type'],\n> = Event extends {\n type: EventType;\n payload: infer Payload;\n}\n ? Payload extends unknown[]\n ? Payload\n : never\n : never;\n\nexport type GenericEventHandler = (...args: unknown[]) => void;\n\nexport type SelectorFunction<\n Event extends EventConstraint,\n EventType extends Event['type'],\n ReturnValue,\n> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: string;\n handler: ((...args: never) => unknown) | ((...args: never[]) => unknown);\n};\nexport type EventConstraint = {\n type: string;\n payload: unknown[];\n};\n\ntype EventSubscriptionMap<\n Event extends EventConstraint,\n ReturnValue = unknown,\n> = Map<\n GenericEventHandler | SelectorEventHandler<ReturnValue>,\n SelectorFunction<Event, Event['type'], ReturnValue> | undefined\n>;\n\n/**\n * A namespaced string\n *\n * This type verifies that the string Name is prefixed by the string Name followed by a colon.\n *\n * @template Namespace - The namespace we're checking for.\n * @template Name - The full string, including the namespace.\n */\nexport type NamespacedBy<\n Namespace extends string,\n Name extends string,\n> = Name extends `${Namespace}:${string}` ? Name : never;\n\nexport type NotNamespacedBy<\n Namespace extends string,\n Name extends string,\n> = Name extends `${Namespace}:${string}` ? never : Name;\n\nexport type NamespacedName<Namespace extends string = string> =\n `${Namespace}:${string}`;\n\ntype NarrowToNamespace<Name, Namespace extends string> = Name extends {\n type: `${Namespace}:${string}`;\n}\n ? Name\n : never;\n\ntype NarrowToAllowed<Name, Allowed extends string> = Name extends {\n type: Allowed;\n}\n ? Name\n : never;\n\n/**\n * A message broker for \"actions\" and \"events\".\n *\n * The messenger allows registering functions as 'actions' that can be called elsewhere,\n * and it allows publishing and subscribing to events. Both actions and events are identified by\n * unique strings.\n *\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n */\nexport class Messenger<\n Action extends ActionConstraint,\n Event extends EventConstraint,\n> {\n readonly #actions = new Map<Action['type'], unknown>();\n\n readonly #events = new Map<Event['type'], EventSubscriptionMap<Event>>();\n\n /**\n * A map of functions for getting the initial event payload.\n *\n * Used only for events that represent state changes.\n */\n readonly #initialEventPayloadGetters = new Map<\n Event['type'],\n () => ExtractEventPayload<Event, Event['type']>\n >();\n\n /**\n * A cache of selector return values for their respective handlers.\n */\n readonly #eventPayloadCache = new Map<\n GenericEventHandler,\n unknown | undefined\n >();\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * @param actionType - The action type. This is a unique identifier for this action.\n * @param handler - The action handler. This function gets called when the `call` method is\n * invoked with the given action type.\n * @throws Will throw when a handler has been registered for this action type already.\n * @template ActionType - A type union of Action type strings.\n */\n registerActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n handler: ActionHandler<Action, ActionType>,\n ) {\n if (this.#actions.has(actionType)) {\n throw new Error(\n `A handler for ${actionType} has already been registered`,\n );\n }\n this.#actions.set(actionType, handler);\n }\n\n /**\n * Registers action handlers for a list of methods on a messenger client\n *\n * @param messengerClient - The object that is expected to make use of the messenger.\n * @param methodNames - The names of the methods on the messenger client to register as action\n * handlers\n */\n registerMethodActionHandlers<\n MessengerClient extends { name: string },\n MethodNames extends keyof MessengerClient & string,\n >(messengerClient: MessengerClient, methodNames: readonly MethodNames[]) {\n for (const methodName of methodNames) {\n const method = messengerClient[methodName];\n if (typeof method === 'function') {\n const actionType = `${messengerClient.name}:${methodName}` as const;\n this.registerActionHandler(actionType, method.bind(messengerClient));\n }\n }\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * @param actionType - The action type. This is a unique identifier for this action.\n * @template ActionType - A type union of Action type strings.\n */\n unregisterActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#actions.delete(actionType);\n }\n\n /**\n * Unregister all action handlers.\n *\n * This prevents all actions from being called.\n */\n clearActions() {\n this.#actions.clear();\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * @param actionType - The action type. This is a unique identifier for this action.\n * @param params - The action parameters. These must match the type of the parameters of the\n * registered action handler.\n * @throws Will throw when no handler has been registered for the given type.\n * @template ActionType - A type union of Action type strings.\n * @returns The action return value.\n */\n call<ActionType extends Action['type']>(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n const handler = this.#actions.get(actionType) as ActionHandler<\n Action,\n ActionType\n >;\n if (!handler) {\n throw new Error(`A handler for ${actionType} has not been registered`);\n }\n return handler(...params);\n }\n\n /**\n * Register a function for getting the initial payload for an event.\n *\n * This is used for events that represent a state change, where the payload is the state.\n * Registering a function for getting the payload allows event selectors to have a point of\n * comparison the first time state changes.\n *\n * @param args - The arguments to this function\n * @param args.eventType - The event type to register a payload for.\n * @param args.getPayload - A function for retrieving the event payload.\n */\n registerInitialEventPayload<EventType extends Event['type']>({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n this.#initialEventPayloadGetters.set(eventType, getPayload);\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * Note that this method should never throw directly. Any errors from\n * subscribers are captured and re-thrown in a timeout handler.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @template EventType - A type union of Event type strings.\n */\n publish<EventType extends Event['type']>(\n eventType: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n const subscribers = this.#events.get(eventType);\n\n if (subscribers) {\n for (const [handler, selector] of subscribers.entries()) {\n try {\n if (selector) {\n const previousValue = this.#eventPayloadCache.get(handler);\n const newValue = selector(...payload);\n\n if (newValue !== previousValue) {\n this.#eventPayloadCache.set(handler, newValue);\n handler(newValue, previousValue);\n }\n } else {\n (handler as GenericEventHandler)(...payload);\n }\n } catch (error) {\n // Throw error after timeout so that it is capured as a console error\n // (and by Sentry) without interrupting the event publishing.\n setTimeout(() => {\n throw error;\n });\n }\n }\n }\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @template EventType - A type union of Event type strings.\n */\n subscribe<EventType extends Event['type']>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void {\n let subscribers = this.#events.get(eventType);\n if (!subscribers) {\n subscribers = new Map();\n this.#events.set(eventType, subscribers);\n }\n\n subscribers.set(handler, selector);\n\n if (selector) {\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n const initialValue = selector(...getPayload());\n this.#eventPayloadCache.set(handler, initialValue);\n }\n }\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw when the given event handler is not registered for this event.\n * @template EventType - A type union of Event type strings.\n */\n unsubscribe<EventType extends Event['type']>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ) {\n const subscribers = this.#events.get(eventType);\n\n if (!subscribers || !subscribers.has(handler)) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n\n const selector = subscribers.get(handler);\n if (selector) {\n this.#eventPayloadCache.delete(handler);\n }\n\n subscribers.delete(handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @template EventType - A type union of Event type strings.\n */\n clearEventSubscriptions<EventType extends Event['type']>(\n eventType: EventType,\n ) {\n this.#events.delete(eventType);\n }\n\n /**\n * Clear all subscriptions.\n *\n * This will remove all subscribed handlers for all events.\n */\n clearSubscriptions() {\n this.#events.clear();\n }\n\n /**\n * Get a restricted messenger\n *\n * Returns a wrapper around the messenger instance that restricts access to actions and events.\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Messenger options.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted messenger returned.\n * @param options.allowedActions - The list of actions that this restricted messenger should be\n * allowed to call.\n * @param options.allowedEvents - The list of events that this restricted messenger should be\n * allowed to subscribe to.\n * @template Namespace - The namespace for this messenger. Typically this is the name of the\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * This must not include internal actions that are in the messenger's namespace.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * This must not include internal events that are in the messenger's namespace.\n * @returns The restricted messenger.\n */\n getRestricted<\n Namespace extends string,\n AllowedAction extends NotNamespacedBy<Namespace, Action['type']> = never,\n AllowedEvent extends NotNamespacedBy<Namespace, Event['type']> = never,\n >({\n name,\n allowedActions,\n allowedEvents,\n }: {\n name: Namespace;\n allowedActions: NotNamespacedBy<\n Namespace,\n Extract<Action['type'], AllowedAction>\n >[];\n allowedEvents: NotNamespacedBy<\n Namespace,\n Extract<Event['type'], AllowedEvent>\n >[];\n }): RestrictedMessenger<\n Namespace,\n | NarrowToNamespace<Action, Namespace>\n | NarrowToAllowed<Action, AllowedAction>,\n NarrowToNamespace<Event, Namespace> | NarrowToAllowed<Event, AllowedEvent>,\n AllowedAction,\n AllowedEvent\n > {\n return new RestrictedMessenger({\n messenger: this,\n name,\n allowedActions,\n allowedEvents,\n });\n }\n}\n"]}
@@ -61,19 +61,29 @@ export declare class Messenger<Action extends ActionConstraint, Event extends Ev
61
61
  *
62
62
  * This will make the registered function available to call via the `call` method.
63
63
  *
64
- * @param actionType - The action type. This is a unqiue identifier for this action.
64
+ * @param actionType - The action type. This is a unique identifier for this action.
65
65
  * @param handler - The action handler. This function gets called when the `call` method is
66
66
  * invoked with the given action type.
67
67
  * @throws Will throw when a handler has been registered for this action type already.
68
68
  * @template ActionType - A type union of Action type strings.
69
69
  */
70
70
  registerActionHandler<ActionType extends Action['type']>(actionType: ActionType, handler: ActionHandler<Action, ActionType>): void;
71
+ /**
72
+ * Registers action handlers for a list of methods on a messenger client
73
+ *
74
+ * @param messengerClient - The object that is expected to make use of the messenger.
75
+ * @param methodNames - The names of the methods on the messenger client to register as action
76
+ * handlers
77
+ */
78
+ registerMethodActionHandlers<MessengerClient extends {
79
+ name: string;
80
+ }, MethodNames extends keyof MessengerClient & string>(messengerClient: MessengerClient, methodNames: readonly MethodNames[]): void;
71
81
  /**
72
82
  * Unregister an action handler.
73
83
  *
74
84
  * This will prevent this action from being called.
75
85
  *
76
- * @param actionType - The action type. This is a unqiue identifier for this action.
86
+ * @param actionType - The action type. This is a unique identifier for this action.
77
87
  * @template ActionType - A type union of Action type strings.
78
88
  */
79
89
  unregisterActionHandler<ActionType extends Action['type']>(actionType: ActionType): void;
@@ -89,7 +99,7 @@ export declare class Messenger<Action extends ActionConstraint, Event extends Ev
89
99
  * This function will call the action handler corresponding to the given action type, passing
90
100
  * along any parameters given.
91
101
  *
92
- * @param actionType - The action type. This is a unqiue identifier for this action.
102
+ * @param actionType - The action type. This is a unique identifier for this action.
93
103
  * @param params - The action parameters. These must match the type of the parameters of the
94
104
  * registered action handler.
95
105
  * @throws Will throw when no handler has been registered for the given type.
@@ -1 +1 @@
1
- {"version":3,"file":"Messenger.d.cts","sourceRoot":"","sources":["../src/Messenger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AC,KAAK,EAAE,SAAS,CAAC;IAgBhD;;;;;;;OAOG;IACH,uBAAuB,CAAC,SAAS,SAAS,KAAK,CAAC,MAAM,CAAC,EACrD,SAAS,EAAE,SAAS;IAKtB;;;;OAIG;IACH,kBAAkB;IAIlB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,aAAa,CACX,SAAS,SAAS,MAAM,EACxB,aAAa,SAAS,eAAe,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,EACxE,YAAY,SAAS,eAAe,CAAC,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,EACtE,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GACd,EAAE;QACD,IAAI,EAAE,SAAS,CAAC;QAChB,cAAc,EAAE,eAAe,CAC7B,SAAS,EACT,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,aAAa,CAAC,CACvC,EAAE,CAAC;QACJ,aAAa,EAAE,eAAe,CAC5B,SAAS,EACT,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC,CACrC,EAAE,CAAC;KACL,GAAG,mBAAmB,CACrB,SAAS,EACP,iBAAiB,CAAC,MAAM,EAAE,SAAS,CAAC,GACpC,eAAe,CAAC,MAAM,EAAE,aAAa,CAAC,EACxC,iBAAiB,CAAC,KAAK,EAAE,SAAS,CAAC,GAAG,eAAe,CAAC,KAAK,EAAE,YAAY,CAAC,EAC1E,aAAa,EACb,YAAY,CACb;CAQF"}
@@ -61,19 +61,29 @@ export declare class Messenger<Action extends ActionConstraint, Event extends Ev
61
61
  *
62
62
  * This will make the registered function available to call via the `call` method.
63
63
  *
64
- * @param actionType - The action type. This is a unqiue identifier for this action.
64
+ * @param actionType - The action type. This is a unique identifier for this action.
65
65
  * @param handler - The action handler. This function gets called when the `call` method is
66
66
  * invoked with the given action type.
67
67
  * @throws Will throw when a handler has been registered for this action type already.
68
68
  * @template ActionType - A type union of Action type strings.
69
69
  */
70
70
  registerActionHandler<ActionType extends Action['type']>(actionType: ActionType, handler: ActionHandler<Action, ActionType>): void;
71
+ /**
72
+ * Registers action handlers for a list of methods on a messenger client
73
+ *
74
+ * @param messengerClient - The object that is expected to make use of the messenger.
75
+ * @param methodNames - The names of the methods on the messenger client to register as action
76
+ * handlers
77
+ */
78
+ registerMethodActionHandlers<MessengerClient extends {
79
+ name: string;
80
+ }, MethodNames extends keyof MessengerClient & string>(messengerClient: MessengerClient, methodNames: readonly MethodNames[]): void;
71
81
  /**
72
82
  * Unregister an action handler.
73
83
  *
74
84
  * This will prevent this action from being called.
75
85
  *
76
- * @param actionType - The action type. This is a unqiue identifier for this action.
86
+ * @param actionType - The action type. This is a unique identifier for this action.
77
87
  * @template ActionType - A type union of Action type strings.
78
88
  */
79
89
  unregisterActionHandler<ActionType extends Action['type']>(actionType: ActionType): void;
@@ -89,7 +99,7 @@ export declare class Messenger<Action extends ActionConstraint, Event extends Ev
89
99
  * This function will call the action handler corresponding to the given action type, passing
90
100
  * along any parameters given.
91
101
  *
92
- * @param actionType - The action type. This is a unqiue identifier for this action.
102
+ * @param actionType - The action type. This is a unique identifier for this action.
93
103
  * @param params - The action parameters. These must match the type of the parameters of the
94
104
  * registered action handler.
95
105
  * @throws Will throw when no handler has been registered for the given type.
@@ -1 +1 @@
1
- {"version":3,"file":"Messenger.d.mts","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAE5D,MAAM,MAAM,aAAa,CACvB,MAAM,SAAS,gBAAgB,EAC/B,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IACzB,CACF,GAAG,IAAI,EAAE,uBAAuB,CAAC,MAAM,EAAE,UAAU,CAAC,KACjD,qBAAqB,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAE/C,MAAM,MAAM,uBAAuB,CACjC,MAAM,SAAS,gBAAgB,EAC/B,UAAU,GAAG,MAAM,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AS,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,EACtE,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GACd,EAAE;QACD,IAAI,EAAE,SAAS,CAAC;QAChB,cAAc,EAAE,eAAe,CAC7B,SAAS,EACT,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,aAAa,CAAC,CACvC,EAAE,CAAC;QACJ,aAAa,EAAE,eAAe,CAC5B,SAAS,EACT,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC,CACrC,EAAE,CAAC;KACL,GAAG,mBAAmB,CACrB,SAAS,EACP,iBAAiB,CAAC,MAAM,EAAE,SAAS,CAAC,GACpC,eAAe,CAAC,MAAM,EAAE,aAAa,CAAC,EACxC,iBAAiB,CAAC,KAAK,EAAE,SAAS,CAAC,GAAG,eAAe,CAAC,KAAK,EAAE,YAAY,CAAC,EAC1E,aAAa,EACb,YAAY,CACb;CAQF"}
@@ -35,7 +35,7 @@ export class Messenger {
35
35
  *
36
36
  * This will make the registered function available to call via the `call` method.
37
37
  *
38
- * @param actionType - The action type. This is a unqiue identifier for this action.
38
+ * @param actionType - The action type. This is a unique identifier for this action.
39
39
  * @param handler - The action handler. This function gets called when the `call` method is
40
40
  * invoked with the given action type.
41
41
  * @throws Will throw when a handler has been registered for this action type already.
@@ -47,12 +47,28 @@ export class Messenger {
47
47
  }
48
48
  __classPrivateFieldGet(this, _Messenger_actions, "f").set(actionType, handler);
49
49
  }
50
+ /**
51
+ * Registers action handlers for a list of methods on a messenger client
52
+ *
53
+ * @param messengerClient - The object that is expected to make use of the messenger.
54
+ * @param methodNames - The names of the methods on the messenger client to register as action
55
+ * handlers
56
+ */
57
+ registerMethodActionHandlers(messengerClient, methodNames) {
58
+ for (const methodName of methodNames) {
59
+ const method = messengerClient[methodName];
60
+ if (typeof method === 'function') {
61
+ const actionType = `${messengerClient.name}:${methodName}`;
62
+ this.registerActionHandler(actionType, method.bind(messengerClient));
63
+ }
64
+ }
65
+ }
50
66
  /**
51
67
  * Unregister an action handler.
52
68
  *
53
69
  * This will prevent this action from being called.
54
70
  *
55
- * @param actionType - The action type. This is a unqiue identifier for this action.
71
+ * @param actionType - The action type. This is a unique identifier for this action.
56
72
  * @template ActionType - A type union of Action type strings.
57
73
  */
58
74
  unregisterActionHandler(actionType) {
@@ -72,7 +88,7 @@ export class Messenger {
72
88
  * This function will call the action handler corresponding to the given action type, passing
73
89
  * along any parameters given.
74
90
  *
75
- * @param actionType - The action type. This is a unqiue identifier for this action.
91
+ * @param actionType - The action type. This is a unique identifier for this action.
76
92
  * @param params - The action parameters. These must match the type of the parameters of the
77
93
  * registered action handler.
78
94
  * @throws Will throw when no handler has been registered for the given type.
@@ -1 +1 @@
1
- {"version":3,"file":"Messenger.mjs","sourceRoot":"","sources":["../src/Messenger.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAmH5D;;;;;;;;;GASG;AACH,MAAM,OAAO,SAAS;IAAtB;QAIW,6BAAW,IAAI,GAAG,EAA2B,EAAC;QAE9C,4BAAU,IAAI,GAAG,EAA8C,EAAC;QAEzE;;;;WAIG;QACM,gDAA8B,IAAI,GAAG,EAG3C,EAAC;QAEJ;;WAEG;QACM,uCAAqB,IAAI,GAAG,EAGlC,EAAC;IAsTN,CAAC;IApTC;;;;;;;;;;OAUG;IACH,qBAAqB,CACnB,UAAsB,EACtB,OAA0C;QAE1C,IAAI,uBAAA,IAAI,0BAAS,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE;YACjC,MAAM,IAAI,KAAK,CACb,iBAAiB,UAAU,8BAA8B,CAC1D,CAAC;SACH;QACD,uBAAA,IAAI,0BAAS,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;IAED;;;;;;;OAOG;IACH,uBAAuB,CACrB,UAAsB;QAEtB,uBAAA,IAAI,0BAAS,CAAC,MAAM,CAAC,UAAU,CAAC,CAA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,uBAAA,IAAI,oCAAmB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;SACzC;QAED,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;IAED;;;;;;;OAOG;IACH,uBAAuB,CACrB,SAAoB;QAEpB,uBAAA,IAAI,yBAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,kBAAkB;QAChB,uBAAA,IAAI,yBAAQ,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,aAAa,CAIX,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GAWd;QAQC,OAAO,IAAI,mBAAmB,CAAC;YAC7B,SAAS,EAAE,IAAI;YACf,IAAI;YACJ,cAAc;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { RestrictedMessenger } from './RestrictedMessenger';\n\nexport type ActionHandler<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = (\n ...args: ExtractActionParameters<Action, ActionType>\n) => ExtractActionResponse<Action, ActionType>;\n\nexport type ExtractActionParameters<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = Action extends {\n type: ActionType;\n handler: (...args: infer HandlerArgs) => unknown;\n}\n ? HandlerArgs\n : never;\n\nexport type ExtractActionResponse<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = Action extends {\n type: ActionType;\n handler: (...args: infer _) => infer HandlerReturnValue;\n}\n ? HandlerReturnValue\n : never;\n\nexport type ExtractEventHandler<\n Event extends EventConstraint,\n EventType = Event['type'],\n> = Event extends {\n type: EventType;\n payload: infer Payload;\n}\n ? Payload extends unknown[]\n ? (...payload: Payload) => void\n : never\n : never;\n\nexport type ExtractEventPayload<\n Event extends EventConstraint,\n EventType = Event['type'],\n> = Event extends {\n type: EventType;\n payload: infer Payload;\n}\n ? Payload extends unknown[]\n ? Payload\n : never\n : never;\n\nexport type GenericEventHandler = (...args: unknown[]) => void;\n\nexport type SelectorFunction<\n Event extends EventConstraint,\n EventType extends Event['type'],\n ReturnValue,\n> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: string;\n handler: ((...args: never) => unknown) | ((...args: never[]) => unknown);\n};\nexport type EventConstraint = {\n type: string;\n payload: unknown[];\n};\n\ntype EventSubscriptionMap<\n Event extends EventConstraint,\n ReturnValue = unknown,\n> = Map<\n GenericEventHandler | SelectorEventHandler<ReturnValue>,\n SelectorFunction<Event, Event['type'], ReturnValue> | undefined\n>;\n\n/**\n * A namespaced string\n *\n * This type verifies that the string Name is prefixed by the string Name followed by a colon.\n *\n * @template Namespace - The namespace we're checking for.\n * @template Name - The full string, including the namespace.\n */\nexport type NamespacedBy<\n Namespace extends string,\n Name extends string,\n> = Name extends `${Namespace}:${string}` ? Name : never;\n\nexport type NotNamespacedBy<\n Namespace extends string,\n Name extends string,\n> = Name extends `${Namespace}:${string}` ? never : Name;\n\nexport type NamespacedName<Namespace extends string = string> =\n `${Namespace}:${string}`;\n\ntype NarrowToNamespace<Name, Namespace extends string> = Name extends {\n type: `${Namespace}:${string}`;\n}\n ? Name\n : never;\n\ntype NarrowToAllowed<Name, Allowed extends string> = Name extends {\n type: Allowed;\n}\n ? Name\n : never;\n\n/**\n * A message broker for \"actions\" and \"events\".\n *\n * The messenger allows registering functions as 'actions' that can be called elsewhere,\n * and it allows publishing and subscribing to events. Both actions and events are identified by\n * unique strings.\n *\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n */\nexport class Messenger<\n Action extends ActionConstraint,\n Event extends EventConstraint,\n> {\n readonly #actions = new Map<Action['type'], unknown>();\n\n readonly #events = new Map<Event['type'], EventSubscriptionMap<Event>>();\n\n /**\n * A map of functions for getting the initial event payload.\n *\n * Used only for events that represent state changes.\n */\n readonly #initialEventPayloadGetters = new Map<\n Event['type'],\n () => ExtractEventPayload<Event, Event['type']>\n >();\n\n /**\n * A cache of selector return values for their respective handlers.\n */\n readonly #eventPayloadCache = new Map<\n GenericEventHandler,\n unknown | undefined\n >();\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @param handler - The action handler. This function gets called when the `call` method is\n * invoked with the given action type.\n * @throws Will throw when a handler has been registered for this action type already.\n * @template ActionType - A type union of Action type strings.\n */\n registerActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n handler: ActionHandler<Action, ActionType>,\n ) {\n if (this.#actions.has(actionType)) {\n throw new Error(\n `A handler for ${actionType} has already been registered`,\n );\n }\n this.#actions.set(actionType, handler);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @template ActionType - A type union of Action type strings.\n */\n unregisterActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#actions.delete(actionType);\n }\n\n /**\n * Unregister all action handlers.\n *\n * This prevents all actions from being called.\n */\n clearActions() {\n this.#actions.clear();\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @param params - The action parameters. These must match the type of the parameters of the\n * registered action handler.\n * @throws Will throw when no handler has been registered for the given type.\n * @template ActionType - A type union of Action type strings.\n * @returns The action return value.\n */\n call<ActionType extends Action['type']>(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n const handler = this.#actions.get(actionType) as ActionHandler<\n Action,\n ActionType\n >;\n if (!handler) {\n throw new Error(`A handler for ${actionType} has not been registered`);\n }\n return handler(...params);\n }\n\n /**\n * Register a function for getting the initial payload for an event.\n *\n * This is used for events that represent a state change, where the payload is the state.\n * Registering a function for getting the payload allows event selectors to have a point of\n * comparison the first time state changes.\n *\n * @param args - The arguments to this function\n * @param args.eventType - The event type to register a payload for.\n * @param args.getPayload - A function for retrieving the event payload.\n */\n registerInitialEventPayload<EventType extends Event['type']>({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n this.#initialEventPayloadGetters.set(eventType, getPayload);\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * Note that this method should never throw directly. Any errors from\n * subscribers are captured and re-thrown in a timeout handler.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @template EventType - A type union of Event type strings.\n */\n publish<EventType extends Event['type']>(\n eventType: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n const subscribers = this.#events.get(eventType);\n\n if (subscribers) {\n for (const [handler, selector] of subscribers.entries()) {\n try {\n if (selector) {\n const previousValue = this.#eventPayloadCache.get(handler);\n const newValue = selector(...payload);\n\n if (newValue !== previousValue) {\n this.#eventPayloadCache.set(handler, newValue);\n handler(newValue, previousValue);\n }\n } else {\n (handler as GenericEventHandler)(...payload);\n }\n } catch (error) {\n // Throw error after timeout so that it is capured as a console error\n // (and by Sentry) without interrupting the event publishing.\n setTimeout(() => {\n throw error;\n });\n }\n }\n }\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @template EventType - A type union of Event type strings.\n */\n subscribe<EventType extends Event['type']>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void {\n let subscribers = this.#events.get(eventType);\n if (!subscribers) {\n subscribers = new Map();\n this.#events.set(eventType, subscribers);\n }\n\n subscribers.set(handler, selector);\n\n if (selector) {\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n const initialValue = selector(...getPayload());\n this.#eventPayloadCache.set(handler, initialValue);\n }\n }\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw when the given event handler is not registered for this event.\n * @template EventType - A type union of Event type strings.\n */\n unsubscribe<EventType extends Event['type']>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ) {\n const subscribers = this.#events.get(eventType);\n\n if (!subscribers || !subscribers.has(handler)) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n\n const selector = subscribers.get(handler);\n if (selector) {\n this.#eventPayloadCache.delete(handler);\n }\n\n subscribers.delete(handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @template EventType - A type union of Event type strings.\n */\n clearEventSubscriptions<EventType extends Event['type']>(\n eventType: EventType,\n ) {\n this.#events.delete(eventType);\n }\n\n /**\n * Clear all subscriptions.\n *\n * This will remove all subscribed handlers for all events.\n */\n clearSubscriptions() {\n this.#events.clear();\n }\n\n /**\n * Get a restricted messenger\n *\n * Returns a wrapper around the messenger instance that restricts access to actions and events.\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Messenger options.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted messenger returned.\n * @param options.allowedActions - The list of actions that this restricted messenger should be\n * allowed to call.\n * @param options.allowedEvents - The list of events that this restricted messenger should be\n * allowed to subscribe to.\n * @template Namespace - The namespace for this messenger. Typically this is the name of the\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * This must not include internal actions that are in the messenger's namespace.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * This must not include internal events that are in the messenger's namespace.\n * @returns The restricted messenger.\n */\n getRestricted<\n Namespace extends string,\n AllowedAction extends NotNamespacedBy<Namespace, Action['type']> = never,\n AllowedEvent extends NotNamespacedBy<Namespace, Event['type']> = never,\n >({\n name,\n allowedActions,\n allowedEvents,\n }: {\n name: Namespace;\n allowedActions: NotNamespacedBy<\n Namespace,\n Extract<Action['type'], AllowedAction>\n >[];\n allowedEvents: NotNamespacedBy<\n Namespace,\n Extract<Event['type'], AllowedEvent>\n >[];\n }): RestrictedMessenger<\n Namespace,\n | NarrowToNamespace<Action, Namespace>\n | NarrowToAllowed<Action, AllowedAction>,\n NarrowToNamespace<Event, Namespace> | NarrowToAllowed<Event, AllowedEvent>,\n AllowedAction,\n AllowedEvent\n > {\n return new RestrictedMessenger({\n messenger: this,\n name,\n allowedActions,\n allowedEvents,\n });\n }\n}\n"]}
1
+ {"version":3,"file":"Messenger.mjs","sourceRoot":"","sources":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AC;IAED;;;;;;;OAOG;IACH,uBAAuB,CACrB,SAAoB;QAEpB,uBAAA,IAAI,yBAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,kBAAkB;QAChB,uBAAA,IAAI,yBAAQ,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,aAAa,CAIX,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GAWd;QAQC,OAAO,IAAI,mBAAmB,CAAC;YAC7B,SAAS,EAAE,IAAI;YACf,IAAI;YACJ,cAAc;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { RestrictedMessenger } from './RestrictedMessenger';\n\nexport type ActionHandler<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = (\n ...args: ExtractActionParameters<Action, ActionType>\n) => ExtractActionResponse<Action, ActionType>;\n\nexport type ExtractActionParameters<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = Action extends {\n type: ActionType;\n handler: (...args: infer HandlerArgs) => unknown;\n}\n ? HandlerArgs\n : never;\n\nexport type ExtractActionResponse<\n Action extends ActionConstraint,\n ActionType = Action['type'],\n> = Action extends {\n type: ActionType;\n handler: (...args: infer _) => infer HandlerReturnValue;\n}\n ? HandlerReturnValue\n : never;\n\nexport type ExtractEventHandler<\n Event extends EventConstraint,\n EventType = Event['type'],\n> = Event extends {\n type: EventType;\n payload: infer Payload;\n}\n ? Payload extends unknown[]\n ? (...payload: Payload) => void\n : never\n : never;\n\nexport type ExtractEventPayload<\n Event extends EventConstraint,\n EventType = Event['type'],\n> = Event extends {\n type: EventType;\n payload: infer Payload;\n}\n ? Payload extends unknown[]\n ? Payload\n : never\n : never;\n\nexport type GenericEventHandler = (...args: unknown[]) => void;\n\nexport type SelectorFunction<\n Event extends EventConstraint,\n EventType extends Event['type'],\n ReturnValue,\n> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: string;\n handler: ((...args: never) => unknown) | ((...args: never[]) => unknown);\n};\nexport type EventConstraint = {\n type: string;\n payload: unknown[];\n};\n\ntype EventSubscriptionMap<\n Event extends EventConstraint,\n ReturnValue = unknown,\n> = Map<\n GenericEventHandler | SelectorEventHandler<ReturnValue>,\n SelectorFunction<Event, Event['type'], ReturnValue> | undefined\n>;\n\n/**\n * A namespaced string\n *\n * This type verifies that the string Name is prefixed by the string Name followed by a colon.\n *\n * @template Namespace - The namespace we're checking for.\n * @template Name - The full string, including the namespace.\n */\nexport type NamespacedBy<\n Namespace extends string,\n Name extends string,\n> = Name extends `${Namespace}:${string}` ? Name : never;\n\nexport type NotNamespacedBy<\n Namespace extends string,\n Name extends string,\n> = Name extends `${Namespace}:${string}` ? never : Name;\n\nexport type NamespacedName<Namespace extends string = string> =\n `${Namespace}:${string}`;\n\ntype NarrowToNamespace<Name, Namespace extends string> = Name extends {\n type: `${Namespace}:${string}`;\n}\n ? Name\n : never;\n\ntype NarrowToAllowed<Name, Allowed extends string> = Name extends {\n type: Allowed;\n}\n ? Name\n : never;\n\n/**\n * A message broker for \"actions\" and \"events\".\n *\n * The messenger allows registering functions as 'actions' that can be called elsewhere,\n * and it allows publishing and subscribing to events. Both actions and events are identified by\n * unique strings.\n *\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n */\nexport class Messenger<\n Action extends ActionConstraint,\n Event extends EventConstraint,\n> {\n readonly #actions = new Map<Action['type'], unknown>();\n\n readonly #events = new Map<Event['type'], EventSubscriptionMap<Event>>();\n\n /**\n * A map of functions for getting the initial event payload.\n *\n * Used only for events that represent state changes.\n */\n readonly #initialEventPayloadGetters = new Map<\n Event['type'],\n () => ExtractEventPayload<Event, Event['type']>\n >();\n\n /**\n * A cache of selector return values for their respective handlers.\n */\n readonly #eventPayloadCache = new Map<\n GenericEventHandler,\n unknown | undefined\n >();\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * @param actionType - The action type. This is a unique identifier for this action.\n * @param handler - The action handler. This function gets called when the `call` method is\n * invoked with the given action type.\n * @throws Will throw when a handler has been registered for this action type already.\n * @template ActionType - A type union of Action type strings.\n */\n registerActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n handler: ActionHandler<Action, ActionType>,\n ) {\n if (this.#actions.has(actionType)) {\n throw new Error(\n `A handler for ${actionType} has already been registered`,\n );\n }\n this.#actions.set(actionType, handler);\n }\n\n /**\n * Registers action handlers for a list of methods on a messenger client\n *\n * @param messengerClient - The object that is expected to make use of the messenger.\n * @param methodNames - The names of the methods on the messenger client to register as action\n * handlers\n */\n registerMethodActionHandlers<\n MessengerClient extends { name: string },\n MethodNames extends keyof MessengerClient & string,\n >(messengerClient: MessengerClient, methodNames: readonly MethodNames[]) {\n for (const methodName of methodNames) {\n const method = messengerClient[methodName];\n if (typeof method === 'function') {\n const actionType = `${messengerClient.name}:${methodName}` as const;\n this.registerActionHandler(actionType, method.bind(messengerClient));\n }\n }\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * @param actionType - The action type. This is a unique identifier for this action.\n * @template ActionType - A type union of Action type strings.\n */\n unregisterActionHandler<ActionType extends Action['type']>(\n actionType: ActionType,\n ) {\n this.#actions.delete(actionType);\n }\n\n /**\n * Unregister all action handlers.\n *\n * This prevents all actions from being called.\n */\n clearActions() {\n this.#actions.clear();\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * @param actionType - The action type. This is a unique identifier for this action.\n * @param params - The action parameters. These must match the type of the parameters of the\n * registered action handler.\n * @throws Will throw when no handler has been registered for the given type.\n * @template ActionType - A type union of Action type strings.\n * @returns The action return value.\n */\n call<ActionType extends Action['type']>(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n const handler = this.#actions.get(actionType) as ActionHandler<\n Action,\n ActionType\n >;\n if (!handler) {\n throw new Error(`A handler for ${actionType} has not been registered`);\n }\n return handler(...params);\n }\n\n /**\n * Register a function for getting the initial payload for an event.\n *\n * This is used for events that represent a state change, where the payload is the state.\n * Registering a function for getting the payload allows event selectors to have a point of\n * comparison the first time state changes.\n *\n * @param args - The arguments to this function\n * @param args.eventType - The event type to register a payload for.\n * @param args.getPayload - A function for retrieving the event payload.\n */\n registerInitialEventPayload<EventType extends Event['type']>({\n eventType,\n getPayload,\n }: {\n eventType: EventType;\n getPayload: () => ExtractEventPayload<Event, EventType>;\n }) {\n this.#initialEventPayloadGetters.set(eventType, getPayload);\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * Note that this method should never throw directly. Any errors from\n * subscribers are captured and re-thrown in a timeout handler.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @template EventType - A type union of Event type strings.\n */\n publish<EventType extends Event['type']>(\n eventType: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n const subscribers = this.#events.get(eventType);\n\n if (subscribers) {\n for (const [handler, selector] of subscribers.entries()) {\n try {\n if (selector) {\n const previousValue = this.#eventPayloadCache.get(handler);\n const newValue = selector(...payload);\n\n if (newValue !== previousValue) {\n this.#eventPayloadCache.set(handler, newValue);\n handler(newValue, previousValue);\n }\n } else {\n (handler as GenericEventHandler)(...payload);\n }\n } catch (error) {\n // Throw error after timeout so that it is capured as a console error\n // (and by Sentry) without interrupting the event publishing.\n setTimeout(() => {\n throw error;\n });\n }\n }\n }\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @template EventType - A type union of Event type strings.\n */\n subscribe<EventType extends Event['type']>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<EventType extends Event['type'], SelectorReturnValue>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void {\n let subscribers = this.#events.get(eventType);\n if (!subscribers) {\n subscribers = new Map();\n this.#events.set(eventType, subscribers);\n }\n\n subscribers.set(handler, selector);\n\n if (selector) {\n const getPayload = this.#initialEventPayloadGetters.get(eventType);\n if (getPayload) {\n const initialValue = selector(...getPayload());\n this.#eventPayloadCache.set(handler, initialValue);\n }\n }\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw when the given event handler is not registered for this event.\n * @template EventType - A type union of Event type strings.\n */\n unsubscribe<EventType extends Event['type']>(\n eventType: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n ) {\n const subscribers = this.#events.get(eventType);\n\n if (!subscribers || !subscribers.has(handler)) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n\n const selector = subscribers.get(handler);\n if (selector) {\n this.#eventPayloadCache.delete(handler);\n }\n\n subscribers.delete(handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @template EventType - A type union of Event type strings.\n */\n clearEventSubscriptions<EventType extends Event['type']>(\n eventType: EventType,\n ) {\n this.#events.delete(eventType);\n }\n\n /**\n * Clear all subscriptions.\n *\n * This will remove all subscribed handlers for all events.\n */\n clearSubscriptions() {\n this.#events.clear();\n }\n\n /**\n * Get a restricted messenger\n *\n * Returns a wrapper around the messenger instance that restricts access to actions and events.\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Messenger options.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted messenger returned.\n * @param options.allowedActions - The list of actions that this restricted messenger should be\n * allowed to call.\n * @param options.allowedEvents - The list of events that this restricted messenger should be\n * allowed to subscribe to.\n * @template Namespace - The namespace for this messenger. Typically this is the name of the\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * This must not include internal actions that are in the messenger's namespace.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * This must not include internal events that are in the messenger's namespace.\n * @returns The restricted messenger.\n */\n getRestricted<\n Namespace extends string,\n AllowedAction extends NotNamespacedBy<Namespace, Action['type']> = never,\n AllowedEvent extends NotNamespacedBy<Namespace, Event['type']> = never,\n >({\n name,\n allowedActions,\n allowedEvents,\n }: {\n name: Namespace;\n allowedActions: NotNamespacedBy<\n Namespace,\n Extract<Action['type'], AllowedAction>\n >[];\n allowedEvents: NotNamespacedBy<\n Namespace,\n Extract<Event['type'], AllowedEvent>\n >[];\n }): RestrictedMessenger<\n Namespace,\n | NarrowToNamespace<Action, Namespace>\n | NarrowToAllowed<Action, AllowedAction>,\n NarrowToNamespace<Event, Namespace> | NarrowToAllowed<Event, AllowedEvent>,\n AllowedAction,\n AllowedEvent\n > {\n return new RestrictedMessenger({\n messenger: this,\n name,\n allowedActions,\n allowedEvents,\n });\n }\n}\n"]}
@@ -70,7 +70,7 @@ class RestrictedMessenger {
70
70
  *
71
71
  * The action type this handler is registered under *must* be in the current namespace.
72
72
  *
73
- * @param action - The action type. This is a unqiue identifier for this action.
73
+ * @param action - The action type. This is a unique identifier for this action.
74
74
  * @param handler - The action handler. This function gets called when the `call` method is
75
75
  * invoked with the given action type.
76
76
  * @throws Will throw if an action handler that is not in the current namespace is being registered.
@@ -83,6 +83,16 @@ class RestrictedMessenger {
83
83
  }
84
84
  __classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").registerActionHandler(action, handler);
85
85
  }
86
+ /**
87
+ * Registers action handlers for a list of methods on a messenger client
88
+ *
89
+ * @param messengerClient - The object that is expected to make use of the messenger.
90
+ * @param methodNames - The names of the methods on the messenger client to register as action
91
+ * handlers
92
+ */
93
+ registerMethodActionHandlers(messengerClient, methodNames) {
94
+ __classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").registerMethodActionHandlers(messengerClient, methodNames);
95
+ }
86
96
  /**
87
97
  * Unregister an action handler.
88
98
  *
@@ -109,7 +119,7 @@ class RestrictedMessenger {
109
119
  *
110
120
  * The action type being called must be on the action allowlist.
111
121
  *
112
- * @param actionType - The action type. This is a unqiue identifier for this action.
122
+ * @param actionType - The action type. This is a unique identifier for this action.
113
123
  * @param params - The action parameters. These must match the type of the parameters of the
114
124
  * registered action handler.
115
125
  * @throws Will throw when no handler has been registered for the given type.
@@ -1 +1 @@
1
- {"version":3,"file":"RestrictedMessenger.cjs","sourceRoot":"","sources":["../src/RestrictedMessenger.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AA+BA;;;;;;;;;;;;;;;GAeG;AACH,MAAa,mBAAmB;IAe9B;;;;;;;;;;;;;;;;;OAiBG;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CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,sCAAW,GAAG,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import type {\n ActionConstraint,\n ActionHandler,\n Messenger,\n EventConstraint,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n NamespacedName,\n NotNamespacedBy,\n SelectorEventHandler,\n SelectorFunction,\n} from './Messenger';\n\n/**\n * A universal supertype of all `RestrictedMessenger` instances. This type can be assigned to any\n * `RestrictedMessenger` type.\n *\n * @template Namespace - Name of the module this messenger is for. Optionally can be used to\n * narrow this type to a constraint for the messenger of a specific module.\n */\nexport type RestrictedMessengerConstraint<Namespace extends string = string> =\n RestrictedMessenger<\n Namespace,\n ActionConstraint,\n EventConstraint,\n string,\n string\n >;\n\n/**\n * A restricted messenger.\n *\n * This acts as a wrapper around the messenger instance that restricts access to actions\n * and events.\n *\n * @template Namespace - The namespace for this messenger. Typically this is the name of the controller or\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * This must not include internal actions that are in the messenger's namespace.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * This must not include internal events that are in the messenger's namespace.\n */\nexport class RestrictedMessenger<\n Namespace extends string,\n Action extends ActionConstraint,\n Event extends EventConstraint,\n AllowedAction extends string,\n AllowedEvent extends string,\n> {\n readonly #messenger: Messenger<ActionConstraint, EventConstraint>;\n\n readonly #namespace: Namespace;\n\n readonly #allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n\n readonly #allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n\n /**\n * Constructs a restricted messenger\n *\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Options.\n * @param options.messenger - The messenger instance that is being wrapped.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted messenger returned.\n * @param options.allowedActions - The list of actions that this restricted messenger should be\n * allowed to call.\n * @param options.allowedEvents - The list of events that this restricted messenger should be\n * allowed to subscribe to.\n */\n constructor({\n messenger,\n name,\n allowedActions,\n allowedEvents,\n }: {\n messenger?: Messenger<ActionConstraint, EventConstraint>;\n name: Namespace;\n allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n }) {\n if (!messenger) {\n throw new Error('Messenger not provided');\n }\n // The above condition guarantees that one of these options is defined.\n this.#messenger = messenger;\n this.#namespace = name;\n this.#allowedActions = allowedActions;\n this.#allowedEvents = allowedEvents;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action type this handler is registered under *must* be in the current namespace.\n *\n * @param action - The action type. This is a unqiue identifier for this action.\n * @param handler - The action handler. This function gets called when the `call` method is\n * invoked with the given action type.\n * @throws Will throw if an action handler that is not in the current namespace is being registered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n registerActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType, handler: ActionHandler<Action, ActionType>) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#messenger.registerActionHandler(action, handler);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * The action type being unregistered *must* be in the current namespace.\n *\n * @param action - The action type. This is a unique identifier for this action.\n * @throws Will throw if an action handler that is not in the current namespace is being unregistered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n unregisterActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#messenger.unregisterActionHandler(action);\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * The action type being called must be on the action allowlist.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @param params - The action parameters. These must match the type of the parameters of the\n * registered action handler.\n * @throws Will throw when no handler has been registered for the given type.\n * @template ActionType - A type union of allowed Action type strings.\n * @returns The action return value.\n */\n call<\n ActionType extends\n | AllowedAction\n | (Action['type'] & NamespacedName<Namespace>),\n >(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n if (!this.#isAllowedAction(actionType)) {\n throw new Error(`Action missing from allow list: ${actionType}`);\n }\n const response = this.#messenger.call<ActionType>(actionType, ...params);\n\n return response;\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 in the current namespace\n *\n * @param args - The arguments to this function\n * @param args.eventType - The event type to register a payload for.\n * @param args.getPayload - A function for retrieving the event payload.\n */\n registerInitialEventPayload<\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 publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.registerInitialEventPayload({\n eventType,\n getPayload,\n });\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * The event type being published *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @throws Will throw if an event that is not in the current namespace is being published.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n publish<EventType extends Event['type'] & NamespacedName<Namespace>>(\n event: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.publish(event, ...payload);\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of Event type strings.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(eventType: EventType, handler: ExtractEventHandler<Event, EventType>): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n event: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n\n if (selector) {\n return this.#messenger.subscribe(event, handler, selector);\n }\n return this.#messenger.subscribe(event, handler);\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * The event type being unsubscribed to must be on the event allowlist.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of allowed Event type strings.\n */\n unsubscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(event: EventType, handler: ExtractEventHandler<Event, EventType>) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n this.#messenger.unsubscribe(event, handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * The event type being cleared *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @throws Will throw if a subscription for an event that is not in the current namespace is being cleared.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n clearEventSubscriptions<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >(event: EventType) {\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed clearing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.clearEventSubscriptions(event);\n }\n\n /**\n * Determine whether the given event type is allowed. Event types are\n * allowed if they are in the current namespace or on the list of\n * allowed events.\n *\n * @param eventType - The event type to check.\n * @returns Whether the event type is allowed.\n */\n #isAllowedEvent(\n eventType: Event['type'],\n ): eventType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedEvent> {\n // Safely upcast to allow runtime check\n const allowedEvents: string[] | null = this.#allowedEvents;\n return (\n this.#isInCurrentNamespace(eventType) ||\n (allowedEvents !== null && allowedEvents.includes(eventType))\n );\n }\n\n /**\n * Determine whether the given action type is allowed. Action types\n * are allowed if they are in the current namespace or on the list of\n * allowed actions.\n *\n * @param actionType - The action type to check.\n * @returns Whether the action type is allowed.\n */\n #isAllowedAction(\n actionType: Action['type'],\n ): actionType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedAction> {\n // Safely upcast to allow runtime check\n const allowedActions: string[] | null = this.#allowedActions;\n return (\n this.#isInCurrentNamespace(actionType) ||\n (allowedActions !== null && allowedActions.includes(actionType))\n );\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"]}
1
+ {"version":3,"file":"RestrictedMessenger.cjs","sourceRoot":"","sources":["../src/RestrictedMessenger.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AA+BA;;;;;;;;;;;;;;;GAeG;AACH,MAAa,mBAAmB;IAe9B;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,EACV,SAAS,EACT,IAAI,EACJ,cAAc,EACd,aAAa,GAMd;;QApCQ,iDAAyD;QAEzD,iDAAsB;QAEtB,sDAA6D;QAE7D,qDAA2D;QA+BlE,IAAI,CAAC,SAAS,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;SAC3C;QACD,uEAAuE;QACvE,uBAAA,IAAI,kCAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,kCAAc,IAAI,MAAA,CAAC;QACvB,uBAAA,IAAI,uCAAmB,cAAc,MAAA,CAAC;QACtC,uBAAA,IAAI,sCAAkB,aAAa,MAAA,CAAC;IACtC,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,qBAAqB,CAEnB,MAAkB,EAAE,OAA0C;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EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC;IAED;;;;;;;;;;OAUG;IACH,uBAAuB,CAErB,KAAgB;QAChB,IAAI,CAAC,uBAAA,IAAI,iFAAsB,MAA1B,IAAI,EAAuB,KAAK,CAAC,EAAE;YACtC,MAAM,IAAI,KAAK,CACb,6CAA6C,uBAAA,IAAI,sCAAW,IAAI,CACjE,CAAC;SACH;QACD,uBAAA,IAAI,sCAAW,CAAC,uBAAuB,CAAC,KAAK,CAAC,CAAC;IACjD,CAAC;CAqDF;AAzXD,kDAyXC;4UA1CG,SAAwB;IAIxB,uCAAuC;IACvC,MAAM,aAAa,GAAoB,uBAAA,IAAI,0CAAe,CAAC;IAC3D,OAAO,CACL,uBAAA,IAAI,iFAAsB,MAA1B,IAAI,EAAuB,SAAS,CAAC;QACrC,CAAC,aAAa,KAAK,IAAI,IAAI,aAAa,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAC9D,CAAC;AACJ,CAAC,uFAWC,UAA0B;IAI1B,uCAAuC;IACvC,MAAM,cAAc,GAAoB,uBAAA,IAAI,2CAAgB,CAAC;IAC7D,OAAO,CACL,uBAAA,IAAI,iFAAsB,MAA1B,IAAI,EAAuB,UAAU,CAAC;QACtC,CAAC,cAAc,KAAK,IAAI,IAAI,cAAc,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CACjE,CAAC;AACJ,CAAC,iGAQqB,IAAY;IAChC,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,sCAAW,GAAG,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import type {\n ActionConstraint,\n ActionHandler,\n Messenger,\n EventConstraint,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n NamespacedName,\n NotNamespacedBy,\n SelectorEventHandler,\n SelectorFunction,\n} from './Messenger';\n\n/**\n * A universal supertype of all `RestrictedMessenger` instances. This type can be assigned to any\n * `RestrictedMessenger` type.\n *\n * @template Namespace - Name of the module this messenger is for. Optionally can be used to\n * narrow this type to a constraint for the messenger of a specific module.\n */\nexport type RestrictedMessengerConstraint<Namespace extends string = string> =\n RestrictedMessenger<\n Namespace,\n ActionConstraint,\n EventConstraint,\n string,\n string\n >;\n\n/**\n * A restricted messenger.\n *\n * This acts as a wrapper around the messenger instance that restricts access to actions\n * and events.\n *\n * @template Namespace - The namespace for this messenger. Typically this is the name of the controller or\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * This must not include internal actions that are in the messenger's namespace.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * This must not include internal events that are in the messenger's namespace.\n */\nexport class RestrictedMessenger<\n Namespace extends string,\n Action extends ActionConstraint,\n Event extends EventConstraint,\n AllowedAction extends string,\n AllowedEvent extends string,\n> {\n readonly #messenger: Messenger<ActionConstraint, EventConstraint>;\n\n readonly #namespace: Namespace;\n\n readonly #allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n\n readonly #allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n\n /**\n * Constructs a restricted messenger\n *\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Options.\n * @param options.messenger - The messenger instance that is being wrapped.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted messenger returned.\n * @param options.allowedActions - The list of actions that this restricted messenger should be\n * allowed to call.\n * @param options.allowedEvents - The list of events that this restricted messenger should be\n * allowed to subscribe to.\n */\n constructor({\n messenger,\n name,\n allowedActions,\n allowedEvents,\n }: {\n messenger?: Messenger<ActionConstraint, EventConstraint>;\n name: Namespace;\n allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n }) {\n if (!messenger) {\n throw new Error('Messenger not provided');\n }\n // The above condition guarantees that one of these options is defined.\n this.#messenger = messenger;\n this.#namespace = name;\n this.#allowedActions = allowedActions;\n this.#allowedEvents = allowedEvents;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action type this handler is registered under *must* be in the current namespace.\n *\n * @param action - The action type. This is a 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 if an action handler that is not in the current namespace is being registered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n registerActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType, handler: ActionHandler<Action, ActionType>) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#messenger.registerActionHandler(action, handler);\n }\n\n /**\n * Registers action handlers for a list of methods on a messenger client\n *\n * @param messengerClient - The object that is expected to make use of the messenger.\n * @param methodNames - The names of the methods on the messenger client to register as action\n * handlers\n */\n registerMethodActionHandlers<\n MessengerClient extends { name: string },\n MethodNames extends keyof MessengerClient & string,\n >(messengerClient: MessengerClient, methodNames: readonly MethodNames[]) {\n this.#messenger.registerMethodActionHandlers(messengerClient, methodNames);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * The action type being unregistered *must* be in the current namespace.\n *\n * @param action - The action type. This is a unique identifier for this action.\n * @throws Will throw if an action handler that is not in the current namespace is being unregistered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n unregisterActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#messenger.unregisterActionHandler(action);\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * The action type being called must be on the action allowlist.\n *\n * @param 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 allowed Action type strings.\n * @returns The action return value.\n */\n call<\n ActionType extends\n | AllowedAction\n | (Action['type'] & NamespacedName<Namespace>),\n >(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n if (!this.#isAllowedAction(actionType)) {\n throw new Error(`Action missing from allow list: ${actionType}`);\n }\n const response = this.#messenger.call<ActionType>(actionType, ...params);\n\n return response;\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 in the current namespace\n *\n * @param args - The arguments to this function\n * @param args.eventType - The event type to register a payload for.\n * @param args.getPayload - A function for retrieving the event payload.\n */\n registerInitialEventPayload<\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 publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.registerInitialEventPayload({\n eventType,\n getPayload,\n });\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * The event type being published *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @throws Will throw if an event that is not in the current namespace is being published.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n publish<EventType extends Event['type'] & NamespacedName<Namespace>>(\n event: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.publish(event, ...payload);\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of Event type strings.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(eventType: EventType, handler: ExtractEventHandler<Event, EventType>): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n event: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n\n if (selector) {\n return this.#messenger.subscribe(event, handler, selector);\n }\n return this.#messenger.subscribe(event, handler);\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * The event type being unsubscribed to must be on the event allowlist.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of allowed Event type strings.\n */\n unsubscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(event: EventType, handler: ExtractEventHandler<Event, EventType>) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n this.#messenger.unsubscribe(event, handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * The event type being cleared *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @throws Will throw if a subscription for an event that is not in the current namespace is being cleared.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n clearEventSubscriptions<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >(event: EventType) {\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed clearing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.clearEventSubscriptions(event);\n }\n\n /**\n * Determine whether the given event type is allowed. Event types are\n * allowed if they are in the current namespace or on the list of\n * allowed events.\n *\n * @param eventType - The event type to check.\n * @returns Whether the event type is allowed.\n */\n #isAllowedEvent(\n eventType: Event['type'],\n ): eventType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedEvent> {\n // Safely upcast to allow runtime check\n const allowedEvents: string[] | null = this.#allowedEvents;\n return (\n this.#isInCurrentNamespace(eventType) ||\n (allowedEvents !== null && allowedEvents.includes(eventType))\n );\n }\n\n /**\n * Determine whether the given action type is allowed. Action types\n * are allowed if they are in the current namespace or on the list of\n * allowed actions.\n *\n * @param actionType - The action type to check.\n * @returns Whether the action type is allowed.\n */\n #isAllowedAction(\n actionType: Action['type'],\n ): actionType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedAction> {\n // Safely upcast to allow runtime check\n const allowedActions: string[] | null = this.#allowedActions;\n return (\n this.#isInCurrentNamespace(actionType) ||\n (allowedActions !== null && allowedActions.includes(actionType))\n );\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"]}
@@ -56,13 +56,23 @@ export declare class RestrictedMessenger<Namespace extends string, Action extend
56
56
  *
57
57
  * The action type this handler is registered under *must* be in the current namespace.
58
58
  *
59
- * @param action - The action type. This is a unqiue identifier for this action.
59
+ * @param action - The action type. This is a unique identifier for this action.
60
60
  * @param handler - The action handler. This function gets called when the `call` method is
61
61
  * invoked with the given action type.
62
62
  * @throws Will throw if an action handler that is not in the current namespace is being registered.
63
63
  * @template ActionType - A type union of Action type strings that are namespaced by Namespace.
64
64
  */
65
65
  registerActionHandler<ActionType extends Action['type'] & NamespacedName<Namespace>>(action: ActionType, handler: ActionHandler<Action, ActionType>): void;
66
+ /**
67
+ * Registers action handlers for a list of methods on a messenger client
68
+ *
69
+ * @param messengerClient - The object that is expected to make use of the messenger.
70
+ * @param methodNames - The names of the methods on the messenger client to register as action
71
+ * handlers
72
+ */
73
+ registerMethodActionHandlers<MessengerClient extends {
74
+ name: string;
75
+ }, MethodNames extends keyof MessengerClient & string>(messengerClient: MessengerClient, methodNames: readonly MethodNames[]): void;
66
76
  /**
67
77
  * Unregister an action handler.
68
78
  *
@@ -83,7 +93,7 @@ export declare class RestrictedMessenger<Namespace extends string, Action extend
83
93
  *
84
94
  * The action type being called must be on the action allowlist.
85
95
  *
86
- * @param actionType - The action type. This is a unqiue identifier for this action.
96
+ * @param actionType - The action type. This is a unique identifier for this action.
87
97
  * @param params - The action parameters. These must match the type of the parameters of the
88
98
  * registered action handler.
89
99
  * @throws Will throw when no handler has been registered for the given type.
@@ -1 +1 @@
1
- {"version":3,"file":"RestrictedMessenger.d.cts","sourceRoot":"","sources":["../src/RestrictedMessenger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,SAAS,EACT,eAAe,EACf,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,gBAAgB,EACjB,wBAAoB;AAErB;;;;;;GAMG;AACH,MAAM,MAAM,6BAA6B,CAAC,SAAS,SAAS,MAAM,GAAG,MAAM,IACzE,mBAAmB,CACjB,SAAS,EACT,gBAAgB,EAChB,eAAe,EACf,MAAM,EACN,MAAM,CACP,CAAC;AAEJ;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,mBAAmB,CAC9B,SAAS,SAAS,MAAM,EACxB,MAAM,SAAS,gBAAgB,EAC/B,KAAK,SAAS,eAAe,EAC7B,aAAa,SAAS,MAAM,EAC5B,YAAY,SAAS,MAAM;;IAU3B;;;;;;;;;;;;;;;;;OAiBG;gBACS,EACV,SAAS,EACT,IAAI,EACJ,cAAc,EACd,aAAa,GACd,EAAE;QACD,SAAS,CAAC,EAAE,SAAS,CAAC,gBAAgB,EAAE,eAAe,CAAC,CAAC;QACzD,IAAI,EAAE,SAAS,CAAC;QAChB,cAAc,EAAE,eAAe,CAAC,SAAS,EAAE,aAAa,CAAC,EAAE,CAAC;QAC5D,aAAa,EAAE,eAAe,CAAC,SAAS,EAAE,YAAY,CAAC,EAAE,CAAC;KAC3D;IAWD;;;;;;;;;;;;OAYG;IACH,qBAAqB,CACnB,UAAU,SAAS,MAAM,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC,SAAS,CA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edMessenger<Namespace extends string, Action extend
56
56
  *
57
57
  * The action type this handler is registered under *must* be in the current namespace.
58
58
  *
59
- * @param action - The action type. This is a unqiue identifier for this action.
59
+ * @param action - The action type. This is a unique identifier for this action.
60
60
  * @param handler - The action handler. This function gets called when the `call` method is
61
61
  * invoked with the given action type.
62
62
  * @throws Will throw if an action handler that is not in the current namespace is being registered.
63
63
  * @template ActionType - A type union of Action type strings that are namespaced by Namespace.
64
64
  */
65
65
  registerActionHandler<ActionType extends Action['type'] & NamespacedName<Namespace>>(action: ActionType, handler: ActionHandler<Action, ActionType>): void;
66
+ /**
67
+ * Registers action handlers for a list of methods on a messenger client
68
+ *
69
+ * @param messengerClient - The object that is expected to make use of the messenger.
70
+ * @param methodNames - The names of the methods on the messenger client to register as action
71
+ * handlers
72
+ */
73
+ registerMethodActionHandlers<MessengerClient extends {
74
+ name: string;
75
+ }, MethodNames extends keyof MessengerClient & string>(messengerClient: MessengerClient, methodNames: readonly MethodNames[]): void;
66
76
  /**
67
77
  * Unregister an action handler.
68
78
  *
@@ -83,7 +93,7 @@ export declare class RestrictedMessenger<Namespace extends string, Action extend
83
93
  *
84
94
  * The action type being called must be on the action allowlist.
85
95
  *
86
- * @param actionType - The action type. This is a unqiue identifier for this action.
96
+ * @param actionType - The action type. This is a unique identifier for this action.
87
97
  * @param params - The action parameters. These must match the type of the parameters of the
88
98
  * registered action handler.
89
99
  * @throws Will throw when no handler has been registered for the given type.
@@ -1 +1 @@
1
- {"version":3,"file":"RestrictedMessenger.d.mts","sourceRoot":"","sources":["../src/RestrictedMessenger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,SAAS,EACT,eAAe,EACf,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,gBAAgB,EACjB,wBAAoB;AAErB;;;;;;GAMG;AACH,MAAM,MAAM,6BAA6B,CAAC,SAAS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EAAE,SAAS,CAAC,GAAG,IAAI;IAE7E;;;;;;;;;;;;;;;;;;;OAmBG;IACH,SAAS,CACP,SAAS,SACL,YAAY,GACZ,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC,SAAS,CAAC,CAAC,EAC/C,mBAAmB,EAEnB,SAAS,EAAE,SAAS,EACpB,OAAO,EAAE,oBAAoB,CAAC,mBAAmB,CAAC,EAClD,QAAQ,EAAE,gBAAgB,CAAC,KAAK,EAAE,SAAS,EAAE,mBAAmB,CAAC,GAChE,IAAI;IAsBP;;;;;;;;;;;OAWG;IACH,WAAW,CACT,SAAS,SACL,YAAY,GACZ,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC,SAAS,CAAC,CAAC,EAC/C,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,mBAAmB,CAAC,KAAK,EAAE,SAAS,CAAC;IAOlE;;;;;;;;;;OAUG;IACH,uBAAuB,CACrB,SAAS,SAAS,KAAK,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC,SAAS,CAAC,EAC3D,KAAK,EAAE,SAAS;CA4DnB"}
@@ -67,7 +67,7 @@ export class RestrictedMessenger {
67
67
  *
68
68
  * The action type this handler is registered under *must* be in the current namespace.
69
69
  *
70
- * @param action - The action type. This is a unqiue identifier for this action.
70
+ * @param action - The action type. This is a unique identifier for this action.
71
71
  * @param handler - The action handler. This function gets called when the `call` method is
72
72
  * invoked with the given action type.
73
73
  * @throws Will throw if an action handler that is not in the current namespace is being registered.
@@ -80,6 +80,16 @@ export class RestrictedMessenger {
80
80
  }
81
81
  __classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").registerActionHandler(action, handler);
82
82
  }
83
+ /**
84
+ * Registers action handlers for a list of methods on a messenger client
85
+ *
86
+ * @param messengerClient - The object that is expected to make use of the messenger.
87
+ * @param methodNames - The names of the methods on the messenger client to register as action
88
+ * handlers
89
+ */
90
+ registerMethodActionHandlers(messengerClient, methodNames) {
91
+ __classPrivateFieldGet(this, _RestrictedMessenger_messenger, "f").registerMethodActionHandlers(messengerClient, methodNames);
92
+ }
83
93
  /**
84
94
  * Unregister an action handler.
85
95
  *
@@ -106,7 +116,7 @@ export class RestrictedMessenger {
106
116
  *
107
117
  * The action type being called must be on the action allowlist.
108
118
  *
109
- * @param actionType - The action type. This is a unqiue identifier for this action.
119
+ * @param actionType - The action type. This is a unique identifier for this action.
110
120
  * @param params - The action parameters. These must match the type of the parameters of the
111
121
  * registered action handler.
112
122
  * @throws Will throw when no handler has been registered for the given type.
@@ -1 +1 @@
1
- {"version":3,"file":"RestrictedMessenger.mjs","sourceRoot":"","sources":["../src/RestrictedMessenger.ts"],"names":[],"mappings":";;;;;;;;;;;;AA+BA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,mBAAmB;IAe9B;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,EACV,SAAS,EACT,IAAI,EACJ,cAAc,EACd,aAAa,GAMd;;QApCQ,iDAAyD;QAEzD,iDAAsB;QAEtB,sDAA6D;QAE7D,qDAA2D;QA+BlE,IAAI,CAAC,SAAS,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;SAC3C;QACD,uEAAuE;QACvE,uBAAA,IAAI,kCAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,kCAAc,IAAI,MAAA,CAAC;QACvB,uBAAA,IAAI,uCAAmB,cAAc,MAAA,CAAC;QACtC,uBAAA,IAAI,sCAAkB,aAAa,MAAA,CAAC;IACtC,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,qBAAqB,CAEnB,MAAkB,EAAE,OAA0C;QAC9D,wBAAwB,CAAC,sCAAsC;QAC/D,IAAI,CAAC,uBAAA,IAAI,iFAAsB,MAA1B,IAAI,EAAuB,MAAM,CAAC,EAAE;YACvC,MAAM,IAAI,KAAK,CACb,yDACE,uBAAA,IAAI,sCACN,IAAI,CACL,CAAC;SACH;QACD,uBAAA,IAAI,sCAA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C;IACvC,MAAM,aAAa,GAAoB,uBAAA,IAAI,0CAAe,CAAC;IAC3D,OAAO,CACL,uBAAA,IAAI,iFAAsB,MAA1B,IAAI,EAAuB,SAAS,CAAC;QACrC,CAAC,aAAa,KAAK,IAAI,IAAI,aAAa,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAC9D,CAAC;AACJ,CAAC,uFAWC,UAA0B;IAI1B,uCAAuC;IACvC,MAAM,cAAc,GAAoB,uBAAA,IAAI,2CAAgB,CAAC;IAC7D,OAAO,CACL,uBAAA,IAAI,iFAAsB,MAA1B,IAAI,EAAuB,UAAU,CAAC;QACtC,CAAC,cAAc,KAAK,IAAI,IAAI,cAAc,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CACjE,CAAC;AACJ,CAAC,iGAQqB,IAAY;IAChC,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,sCAAW,GAAG,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import type {\n ActionConstraint,\n ActionHandler,\n Messenger,\n EventConstraint,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n NamespacedName,\n NotNamespacedBy,\n SelectorEventHandler,\n SelectorFunction,\n} from './Messenger';\n\n/**\n * A universal supertype of all `RestrictedMessenger` instances. This type can be assigned to any\n * `RestrictedMessenger` type.\n *\n * @template Namespace - Name of the module this messenger is for. Optionally can be used to\n * narrow this type to a constraint for the messenger of a specific module.\n */\nexport type RestrictedMessengerConstraint<Namespace extends string = string> =\n RestrictedMessenger<\n Namespace,\n ActionConstraint,\n EventConstraint,\n string,\n string\n >;\n\n/**\n * A restricted messenger.\n *\n * This acts as a wrapper around the messenger instance that restricts access to actions\n * and events.\n *\n * @template Namespace - The namespace for this messenger. Typically this is the name of the controller or\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * This must not include internal actions that are in the messenger's namespace.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * This must not include internal events that are in the messenger's namespace.\n */\nexport class RestrictedMessenger<\n Namespace extends string,\n Action extends ActionConstraint,\n Event extends EventConstraint,\n AllowedAction extends string,\n AllowedEvent extends string,\n> {\n readonly #messenger: Messenger<ActionConstraint, EventConstraint>;\n\n readonly #namespace: Namespace;\n\n readonly #allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n\n readonly #allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n\n /**\n * Constructs a restricted messenger\n *\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Options.\n * @param options.messenger - The messenger instance that is being wrapped.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted messenger returned.\n * @param options.allowedActions - The list of actions that this restricted messenger should be\n * allowed to call.\n * @param options.allowedEvents - The list of events that this restricted messenger should be\n * allowed to subscribe to.\n */\n constructor({\n messenger,\n name,\n allowedActions,\n allowedEvents,\n }: {\n messenger?: Messenger<ActionConstraint, EventConstraint>;\n name: Namespace;\n allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n }) {\n if (!messenger) {\n throw new Error('Messenger not provided');\n }\n // The above condition guarantees that one of these options is defined.\n this.#messenger = messenger;\n this.#namespace = name;\n this.#allowedActions = allowedActions;\n this.#allowedEvents = allowedEvents;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action type this handler is registered under *must* be in the current namespace.\n *\n * @param action - The action type. This is a unqiue identifier for this action.\n * @param handler - The action handler. This function gets called when the `call` method is\n * invoked with the given action type.\n * @throws Will throw if an action handler that is not in the current namespace is being registered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n registerActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType, handler: ActionHandler<Action, ActionType>) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#messenger.registerActionHandler(action, handler);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * The action type being unregistered *must* be in the current namespace.\n *\n * @param action - The action type. This is a unique identifier for this action.\n * @throws Will throw if an action handler that is not in the current namespace is being unregistered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n unregisterActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#messenger.unregisterActionHandler(action);\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * The action type being called must be on the action allowlist.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @param params - The action parameters. These must match the type of the parameters of the\n * registered action handler.\n * @throws Will throw when no handler has been registered for the given type.\n * @template ActionType - A type union of allowed Action type strings.\n * @returns The action return value.\n */\n call<\n ActionType extends\n | AllowedAction\n | (Action['type'] & NamespacedName<Namespace>),\n >(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n if (!this.#isAllowedAction(actionType)) {\n throw new Error(`Action missing from allow list: ${actionType}`);\n }\n const response = this.#messenger.call<ActionType>(actionType, ...params);\n\n return response;\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 in the current namespace\n *\n * @param args - The arguments to this function\n * @param args.eventType - The event type to register a payload for.\n * @param args.getPayload - A function for retrieving the event payload.\n */\n registerInitialEventPayload<\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 publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.registerInitialEventPayload({\n eventType,\n getPayload,\n });\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * The event type being published *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @throws Will throw if an event that is not in the current namespace is being published.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n publish<EventType extends Event['type'] & NamespacedName<Namespace>>(\n event: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.publish(event, ...payload);\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of Event type strings.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(eventType: EventType, handler: ExtractEventHandler<Event, EventType>): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n event: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n\n if (selector) {\n return this.#messenger.subscribe(event, handler, selector);\n }\n return this.#messenger.subscribe(event, handler);\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * The event type being unsubscribed to must be on the event allowlist.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of allowed Event type strings.\n */\n unsubscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(event: EventType, handler: ExtractEventHandler<Event, EventType>) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n this.#messenger.unsubscribe(event, handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * The event type being cleared *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @throws Will throw if a subscription for an event that is not in the current namespace is being cleared.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n clearEventSubscriptions<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >(event: EventType) {\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed clearing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.clearEventSubscriptions(event);\n }\n\n /**\n * Determine whether the given event type is allowed. Event types are\n * allowed if they are in the current namespace or on the list of\n * allowed events.\n *\n * @param eventType - The event type to check.\n * @returns Whether the event type is allowed.\n */\n #isAllowedEvent(\n eventType: Event['type'],\n ): eventType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedEvent> {\n // Safely upcast to allow runtime check\n const allowedEvents: string[] | null = this.#allowedEvents;\n return (\n this.#isInCurrentNamespace(eventType) ||\n (allowedEvents !== null && allowedEvents.includes(eventType))\n );\n }\n\n /**\n * Determine whether the given action type is allowed. Action types\n * are allowed if they are in the current namespace or on the list of\n * allowed actions.\n *\n * @param actionType - The action type to check.\n * @returns Whether the action type is allowed.\n */\n #isAllowedAction(\n actionType: Action['type'],\n ): actionType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedAction> {\n // Safely upcast to allow runtime check\n const allowedActions: string[] | null = this.#allowedActions;\n return (\n this.#isInCurrentNamespace(actionType) ||\n (allowedActions !== null && allowedActions.includes(actionType))\n );\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"]}
1
+ {"version":3,"file":"RestrictedMessenger.mjs","sourceRoot":"","sources":["../src/RestrictedMessenger.ts"],"names":[],"mappings":";;;;;;;;;;;;AA+BA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,mBA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B,MAA1B,IAAI,EAAuB,UAAU,CAAC;QACtC,CAAC,cAAc,KAAK,IAAI,IAAI,cAAc,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CACjE,CAAC;AACJ,CAAC,iGAQqB,IAAY;IAChC,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,sCAAW,GAAG,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import type {\n ActionConstraint,\n ActionHandler,\n Messenger,\n EventConstraint,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n NamespacedName,\n NotNamespacedBy,\n SelectorEventHandler,\n SelectorFunction,\n} from './Messenger';\n\n/**\n * A universal supertype of all `RestrictedMessenger` instances. This type can be assigned to any\n * `RestrictedMessenger` type.\n *\n * @template Namespace - Name of the module this messenger is for. Optionally can be used to\n * narrow this type to a constraint for the messenger of a specific module.\n */\nexport type RestrictedMessengerConstraint<Namespace extends string = string> =\n RestrictedMessenger<\n Namespace,\n ActionConstraint,\n EventConstraint,\n string,\n string\n >;\n\n/**\n * A restricted messenger.\n *\n * This acts as a wrapper around the messenger instance that restricts access to actions\n * and events.\n *\n * @template Namespace - The namespace for this messenger. Typically this is the name of the controller or\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * This must not include internal actions that are in the messenger's namespace.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * This must not include internal events that are in the messenger's namespace.\n */\nexport class RestrictedMessenger<\n Namespace extends string,\n Action extends ActionConstraint,\n Event extends EventConstraint,\n AllowedAction extends string,\n AllowedEvent extends string,\n> {\n readonly #messenger: Messenger<ActionConstraint, EventConstraint>;\n\n readonly #namespace: Namespace;\n\n readonly #allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n\n readonly #allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n\n /**\n * Constructs a restricted messenger\n *\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Options.\n * @param options.messenger - The messenger instance that is being wrapped.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted messenger returned.\n * @param options.allowedActions - The list of actions that this restricted messenger should be\n * allowed to call.\n * @param options.allowedEvents - The list of events that this restricted messenger should be\n * allowed to subscribe to.\n */\n constructor({\n messenger,\n name,\n allowedActions,\n allowedEvents,\n }: {\n messenger?: Messenger<ActionConstraint, EventConstraint>;\n name: Namespace;\n allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n }) {\n if (!messenger) {\n throw new Error('Messenger not provided');\n }\n // The above condition guarantees that one of these options is defined.\n this.#messenger = messenger;\n this.#namespace = name;\n this.#allowedActions = allowedActions;\n this.#allowedEvents = allowedEvents;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action type this handler is registered under *must* be in the current namespace.\n *\n * @param action - The action type. This is a 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 if an action handler that is not in the current namespace is being registered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n registerActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType, handler: ActionHandler<Action, ActionType>) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#messenger.registerActionHandler(action, handler);\n }\n\n /**\n * Registers action handlers for a list of methods on a messenger client\n *\n * @param messengerClient - The object that is expected to make use of the messenger.\n * @param methodNames - The names of the methods on the messenger client to register as action\n * handlers\n */\n registerMethodActionHandlers<\n MessengerClient extends { name: string },\n MethodNames extends keyof MessengerClient & string,\n >(messengerClient: MessengerClient, methodNames: readonly MethodNames[]) {\n this.#messenger.registerMethodActionHandlers(messengerClient, methodNames);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * The action type being unregistered *must* be in the current namespace.\n *\n * @param action - The action type. This is a unique identifier for this action.\n * @throws Will throw if an action handler that is not in the current namespace is being unregistered.\n * @template ActionType - A type union of Action type strings that are namespaced by Namespace.\n */\n unregisterActionHandler<\n ActionType extends Action['type'] & NamespacedName<Namespace>,\n >(action: ActionType) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(action)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${\n this.#namespace\n }:'`,\n );\n }\n this.#messenger.unregisterActionHandler(action);\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * The action type being called must be on the action allowlist.\n *\n * @param 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 allowed Action type strings.\n * @returns The action return value.\n */\n call<\n ActionType extends\n | AllowedAction\n | (Action['type'] & NamespacedName<Namespace>),\n >(\n actionType: ActionType,\n ...params: ExtractActionParameters<Action, ActionType>\n ): ExtractActionResponse<Action, ActionType> {\n if (!this.#isAllowedAction(actionType)) {\n throw new Error(`Action missing from allow list: ${actionType}`);\n }\n const response = this.#messenger.call<ActionType>(actionType, ...params);\n\n return response;\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 in the current namespace\n *\n * @param args - The arguments to this function\n * @param args.eventType - The event type to register a payload for.\n * @param args.getPayload - A function for retrieving the event payload.\n */\n registerInitialEventPayload<\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 publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.registerInitialEventPayload({\n eventType,\n getPayload,\n });\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * The event type being published *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @throws Will throw if an event that is not in the current namespace is being published.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n publish<EventType extends Event['type'] & NamespacedName<Namespace>>(\n event: EventType,\n ...payload: ExtractEventPayload<Event, EventType>\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.publish(event, ...payload);\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of Event type strings.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(eventType: EventType, handler: ExtractEventHandler<Event, EventType>): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of Event type strings.\n * @template SelectorReturnValue - The selector return value.\n */\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n eventType: EventType,\n handler: SelectorEventHandler<SelectorReturnValue>,\n selector: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ): void;\n\n subscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n SelectorReturnValue,\n >(\n event: EventType,\n handler: ExtractEventHandler<Event, EventType>,\n selector?: SelectorFunction<Event, EventType, SelectorReturnValue>,\n ) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n\n if (selector) {\n return this.#messenger.subscribe(event, handler, selector);\n }\n return this.#messenger.subscribe(event, handler);\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * The event type being unsubscribed to must be on the event allowlist.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw if the given event is not an allowed event for this messenger.\n * @template EventType - A type union of allowed Event type strings.\n */\n unsubscribe<\n EventType extends\n | AllowedEvent\n | (Event['type'] & NamespacedName<Namespace>),\n >(event: EventType, handler: ExtractEventHandler<Event, EventType>) {\n if (!this.#isAllowedEvent(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n this.#messenger.unsubscribe(event, handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * The event type being cleared *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @throws Will throw if a subscription for an event that is not in the current namespace is being cleared.\n * @template EventType - A type union of Event type strings that are namespaced by Namespace.\n */\n clearEventSubscriptions<\n EventType extends Event['type'] & NamespacedName<Namespace>,\n >(event: EventType) {\n if (!this.#isInCurrentNamespace(event)) {\n throw new Error(\n `Only allowed clearing events prefixed by '${this.#namespace}:'`,\n );\n }\n this.#messenger.clearEventSubscriptions(event);\n }\n\n /**\n * Determine whether the given event type is allowed. Event types are\n * allowed if they are in the current namespace or on the list of\n * allowed events.\n *\n * @param eventType - The event type to check.\n * @returns Whether the event type is allowed.\n */\n #isAllowedEvent(\n eventType: Event['type'],\n ): eventType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedEvent> {\n // Safely upcast to allow runtime check\n const allowedEvents: string[] | null = this.#allowedEvents;\n return (\n this.#isInCurrentNamespace(eventType) ||\n (allowedEvents !== null && allowedEvents.includes(eventType))\n );\n }\n\n /**\n * Determine whether the given action type is allowed. Action types\n * are allowed if they are in the current namespace or on the list of\n * allowed actions.\n *\n * @param actionType - The action type to check.\n * @returns Whether the action type is allowed.\n */\n #isAllowedAction(\n actionType: Action['type'],\n ): actionType is\n | NamespacedName<Namespace>\n | NotNamespacedBy<Namespace, AllowedAction> {\n // Safely upcast to allow runtime check\n const allowedActions: string[] | null = this.#allowedActions;\n return (\n this.#isInCurrentNamespace(actionType) ||\n (allowedActions !== null && allowedActions.includes(actionType))\n );\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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/base-controller",
3
- "version": "8.0.1-preview-c2d112dc",
3
+ "version": "8.0.1-preview-fd1ef74",
4
4
  "description": "Provides scaffolding for controllers as well a communication system for all controllers",
5
5
  "keywords": [
6
6
  "MetaMask",