@metamask-previews/base-controller 7.0.2-preview-e58ce4ab → 7.0.2-preview-1d772cc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/dist/BaseControllerV2.cjs.map +1 -1
- package/dist/BaseControllerV2.d.cts +2 -2
- package/dist/BaseControllerV2.d.cts.map +1 -1
- package/dist/BaseControllerV2.d.mts +2 -2
- package/dist/BaseControllerV2.d.mts.map +1 -1
- package/dist/BaseControllerV2.mjs.map +1 -1
- package/dist/{ControllerMessenger.mjs → Messenger.cjs} +47 -42
- package/dist/Messenger.cjs.map +1 -0
- package/dist/{ControllerMessenger.d.mts → Messenger.d.cts} +19 -19
- package/dist/Messenger.d.cts.map +1 -0
- package/dist/{ControllerMessenger.d.cts → Messenger.d.mts} +19 -19
- package/dist/Messenger.d.mts.map +1 -0
- package/dist/{ControllerMessenger.cjs → Messenger.mjs} +43 -46
- package/dist/Messenger.mjs.map +1 -0
- package/dist/{RestrictedControllerMessenger.mjs → RestrictedMessenger.cjs} +68 -54
- package/dist/RestrictedMessenger.cjs.map +1 -0
- package/dist/{RestrictedControllerMessenger.d.cts → RestrictedMessenger.d.cts} +35 -23
- package/dist/RestrictedMessenger.d.cts.map +1 -0
- package/dist/{RestrictedControllerMessenger.d.mts → RestrictedMessenger.d.mts} +35 -23
- package/dist/RestrictedMessenger.d.mts.map +1 -0
- package/dist/{RestrictedControllerMessenger.cjs → RestrictedMessenger.mjs} +64 -58
- package/dist/RestrictedMessenger.mjs.map +1 -0
- package/dist/index.cjs +7 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/ControllerMessenger.cjs.map +0 -1
- package/dist/ControllerMessenger.d.cts.map +0 -1
- package/dist/ControllerMessenger.d.mts.map +0 -1
- package/dist/ControllerMessenger.mjs.map +0 -1
- package/dist/RestrictedControllerMessenger.cjs.map +0 -1
- package/dist/RestrictedControllerMessenger.d.cts.map +0 -1
- package/dist/RestrictedControllerMessenger.d.mts.map +0 -1
- package/dist/RestrictedControllerMessenger.mjs.map +0 -1
package/dist/index.d.cts
CHANGED
|
@@ -2,8 +2,8 @@ export type { BaseConfig, BaseControllerV1Instance, BaseState, ConfigConstraint
|
|
|
2
2
|
export { BaseControllerV1, isBaseControllerV1 } from "./BaseControllerV1.cjs";
|
|
3
3
|
export type { BaseControllerInstance, ControllerInstance, Listener as ListenerV2, StateConstraint, LegacyControllerStateConstraint, StateDeriver, StateDeriverConstraint, StateMetadata, StateMetadataConstraint, StatePropertyMetadata, StatePropertyMetadataConstraint, ControllerGetStateAction, ControllerStateChangeEvent, } from "./BaseControllerV2.cjs";
|
|
4
4
|
export { BaseController, getAnonymizedState, getPersistentState, isBaseController, } from "./BaseControllerV2.cjs";
|
|
5
|
-
export type { ActionHandler, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, GenericEventHandler, SelectorFunction, ActionConstraint, EventConstraint, NamespacedBy, NotNamespacedBy, NamespacedName, } from "./
|
|
6
|
-
export { ControllerMessenger } from "./
|
|
7
|
-
export type { RestrictedControllerMessengerConstraint } from "./
|
|
8
|
-
export { RestrictedControllerMessenger } from "./
|
|
5
|
+
export type { ActionHandler, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, GenericEventHandler, SelectorFunction, ActionConstraint, EventConstraint, NamespacedBy, NotNamespacedBy, NamespacedName, } from "./Messenger.cjs";
|
|
6
|
+
export { ControllerMessenger, Messenger } from "./Messenger.cjs";
|
|
7
|
+
export type { RestrictedControllerMessengerConstraint, RestrictedMessengerConstraint, } from "./RestrictedMessenger.cjs";
|
|
8
|
+
export { RestrictedControllerMessenger, RestrictedMessenger, } from "./RestrictedMessenger.cjs";
|
|
9
9
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,UAAU,EACV,wBAAwB,EACxB,SAAS,EACT,gBAAgB,IAAI,kBAAkB,EACtC,QAAQ,EACR,eAAe,IAAI,iBAAiB,GACrC,+BAA2B;AAC5B,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,+BAA2B;AAC1E,YAAY,EACV,sBAAsB,EACtB,kBAAkB,EAClB,QAAQ,IAAI,UAAU,EACtB,eAAe,EACf,+BAA+B,EAC/B,YAAY,EACZ,sBAAsB,EACtB,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,+BAA+B,EAC/B,wBAAwB,EACxB,0BAA0B,GAC3B,+BAA2B;AAC5B,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,GACjB,+BAA2B;AAC5B,YAAY,EACV,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,eAAe,EACf,cAAc,GACf,
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,UAAU,EACV,wBAAwB,EACxB,SAAS,EACT,gBAAgB,IAAI,kBAAkB,EACtC,QAAQ,EACR,eAAe,IAAI,iBAAiB,GACrC,+BAA2B;AAC5B,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,+BAA2B;AAC1E,YAAY,EACV,sBAAsB,EACtB,kBAAkB,EAClB,QAAQ,IAAI,UAAU,EACtB,eAAe,EACf,+BAA+B,EAC/B,YAAY,EACZ,sBAAsB,EACtB,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,+BAA+B,EAC/B,wBAAwB,EACxB,0BAA0B,GAC3B,+BAA2B;AAC5B,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,GACjB,+BAA2B;AAC5B,YAAY,EACV,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,eAAe,EACf,cAAc,GACf,wBAAoB;AACrB,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAE,wBAAoB;AAC7D,YAAY,EACV,uCAAuC,EACvC,6BAA6B,GAC9B,kCAA8B;AAC/B,OAAO,EACL,6BAA6B,EAC7B,mBAAmB,GACpB,kCAA8B"}
|
package/dist/index.d.mts
CHANGED
|
@@ -2,8 +2,8 @@ export type { BaseConfig, BaseControllerV1Instance, BaseState, ConfigConstraint
|
|
|
2
2
|
export { BaseControllerV1, isBaseControllerV1 } from "./BaseControllerV1.mjs";
|
|
3
3
|
export type { BaseControllerInstance, ControllerInstance, Listener as ListenerV2, StateConstraint, LegacyControllerStateConstraint, StateDeriver, StateDeriverConstraint, StateMetadata, StateMetadataConstraint, StatePropertyMetadata, StatePropertyMetadataConstraint, ControllerGetStateAction, ControllerStateChangeEvent, } from "./BaseControllerV2.mjs";
|
|
4
4
|
export { BaseController, getAnonymizedState, getPersistentState, isBaseController, } from "./BaseControllerV2.mjs";
|
|
5
|
-
export type { ActionHandler, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, GenericEventHandler, SelectorFunction, ActionConstraint, EventConstraint, NamespacedBy, NotNamespacedBy, NamespacedName, } from "./
|
|
6
|
-
export { ControllerMessenger } from "./
|
|
7
|
-
export type { RestrictedControllerMessengerConstraint } from "./
|
|
8
|
-
export { RestrictedControllerMessenger } from "./
|
|
5
|
+
export type { ActionHandler, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, GenericEventHandler, SelectorFunction, ActionConstraint, EventConstraint, NamespacedBy, NotNamespacedBy, NamespacedName, } from "./Messenger.mjs";
|
|
6
|
+
export { ControllerMessenger, Messenger } from "./Messenger.mjs";
|
|
7
|
+
export type { RestrictedControllerMessengerConstraint, RestrictedMessengerConstraint, } from "./RestrictedMessenger.mjs";
|
|
8
|
+
export { RestrictedControllerMessenger, RestrictedMessenger, } from "./RestrictedMessenger.mjs";
|
|
9
9
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,UAAU,EACV,wBAAwB,EACxB,SAAS,EACT,gBAAgB,IAAI,kBAAkB,EACtC,QAAQ,EACR,eAAe,IAAI,iBAAiB,GACrC,+BAA2B;AAC5B,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,+BAA2B;AAC1E,YAAY,EACV,sBAAsB,EACtB,kBAAkB,EAClB,QAAQ,IAAI,UAAU,EACtB,eAAe,EACf,+BAA+B,EAC/B,YAAY,EACZ,sBAAsB,EACtB,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,+BAA+B,EAC/B,wBAAwB,EACxB,0BAA0B,GAC3B,+BAA2B;AAC5B,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,GACjB,+BAA2B;AAC5B,YAAY,EACV,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,eAAe,EACf,cAAc,GACf,
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,UAAU,EACV,wBAAwB,EACxB,SAAS,EACT,gBAAgB,IAAI,kBAAkB,EACtC,QAAQ,EACR,eAAe,IAAI,iBAAiB,GACrC,+BAA2B;AAC5B,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,+BAA2B;AAC1E,YAAY,EACV,sBAAsB,EACtB,kBAAkB,EAClB,QAAQ,IAAI,UAAU,EACtB,eAAe,EACf,+BAA+B,EAC/B,YAAY,EACZ,sBAAsB,EACtB,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,+BAA+B,EAC/B,wBAAwB,EACxB,0BAA0B,GAC3B,+BAA2B;AAC5B,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,GACjB,+BAA2B;AAC5B,YAAY,EACV,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,eAAe,EACf,cAAc,GACf,wBAAoB;AACrB,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAE,wBAAoB;AAC7D,YAAY,EACV,uCAAuC,EACvC,6BAA6B,GAC9B,kCAA8B;AAC/B,OAAO,EACL,6BAA6B,EAC7B,mBAAmB,GACpB,kCAA8B"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { BaseControllerV1, isBaseControllerV1 } from "./BaseControllerV1.mjs";
|
|
2
2
|
export { BaseController, getAnonymizedState, getPersistentState, isBaseController } from "./BaseControllerV2.mjs";
|
|
3
|
-
export { ControllerMessenger } from "./
|
|
4
|
-
export { RestrictedControllerMessenger } from "./
|
|
3
|
+
export { ControllerMessenger, Messenger } from "./Messenger.mjs";
|
|
4
|
+
export { RestrictedControllerMessenger, RestrictedMessenger } from "./RestrictedMessenger.mjs";
|
|
5
5
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,+BAA2B;AAgB1E,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,EACjB,+BAA2B;AAe5B,OAAO,EAAE,mBAAmB,EAAE,
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,+BAA2B;AAgB1E,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,EACjB,+BAA2B;AAe5B,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAE,wBAAoB;AAK7D,OAAO,EACL,6BAA6B,EAC7B,mBAAmB,EACpB,kCAA8B","sourcesContent":["export type {\n BaseConfig,\n BaseControllerV1Instance,\n BaseState,\n ConfigConstraint as ConfigConstraintV1,\n Listener,\n StateConstraint as StateConstraintV1,\n} from './BaseControllerV1';\nexport { BaseControllerV1, isBaseControllerV1 } from './BaseControllerV1';\nexport type {\n BaseControllerInstance,\n ControllerInstance,\n Listener as ListenerV2,\n StateConstraint,\n LegacyControllerStateConstraint,\n StateDeriver,\n StateDeriverConstraint,\n StateMetadata,\n StateMetadataConstraint,\n StatePropertyMetadata,\n StatePropertyMetadataConstraint,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from './BaseControllerV2';\nexport {\n BaseController,\n getAnonymizedState,\n getPersistentState,\n isBaseController,\n} from './BaseControllerV2';\nexport type {\n ActionHandler,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n GenericEventHandler,\n SelectorFunction,\n ActionConstraint,\n EventConstraint,\n NamespacedBy,\n NotNamespacedBy,\n NamespacedName,\n} from './Messenger';\nexport { ControllerMessenger, Messenger } from './Messenger';\nexport type {\n RestrictedControllerMessengerConstraint,\n RestrictedMessengerConstraint,\n} from './RestrictedMessenger';\nexport {\n RestrictedControllerMessenger,\n RestrictedMessenger,\n} from './RestrictedMessenger';\n"]}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ControllerMessenger.cjs","sourceRoot":"","sources":["../src/ControllerMessenger.ts"],"names":[],"mappings":";;;;;;;;;AAAA,uFAAgF;AAmHhF;;;;;;;;;GASG;AACH,MAAa,mBAAmB;IAAhC;QAIW,uCAAW,IAAI,GAAG,EAA2B,EAAC;QAE9C,sCAAU,IAAI,GAAG,EAA8C,EAAC;QAEzE;;;;WAIG;QACM,0DAA8B,IAAI,GAAG,EAG3C,EAAC;QAEJ;;WAEG;QACM,iDAAqB,IAAI,GAAG,EAGlC,EAAC;IAsTN,CAAC;IApTC;;;;;;;;;;OAUG;IACH,qBAAqB,CACnB,UAAsB,EACtB,OAA0C;QAE1C,IAAI,uBAAA,IAAI,oCAAS,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE;YACjC,MAAM,IAAI,KAAK,CACb,iBAAiB,UAAU,8BAA8B,CAC1D,CAAC;SACH;QACD,uBAAA,IAAI,oCAAS,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;IAED;;;;;;;OAOG;IACH,uBAAuB,CACrB,UAAsB;QAEtB,uBAAA,IAAI,oCAAS,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACH,YAAY;QACV,uBAAA,IAAI,oCAAS,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,IAAI,CACF,UAAsB,EACtB,GAAG,MAAmD;QAEtD,MAAM,OAAO,GAAG,uBAAA,IAAI,oCAAS,CAAC,GAAG,CAAC,UAAU,CAG3C,CAAC;QACF,IAAI,CAAC,OAAO,EAAE;YACZ,MAAM,IAAI,KAAK,CAAC,iBAAiB,UAAU,0BAA0B,CAAC,CAAC;SACxE;QACD,OAAO,OAA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;;;;;;;OAyBG;IACH,aAAa,CAIX,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GAWd;QAQC,OAAO,IAAI,6DAA6B,CAAC;YACvC,mBAAmB,EAAE,IAAI;YACzB,IAAI;YACJ,cAAc;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF;AA9UD,kDA8UC","sourcesContent":["import { RestrictedControllerMessenger } from './RestrictedControllerMessenger';\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 messaging system for controllers.\n *\n * The controller 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 ControllerMessenger<\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 controller messenger\n *\n * Returns a wrapper around the controller messenger instance that restricts access to actions\n * and events. The provided allowlists grant the ability to call the listed actions and subscribe\n * to the listed events. The \"name\" provided grants ownership of any actions and events under\n * that namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Controller 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 controller messenger returned.\n * @param options.allowedActions - The list of actions that this restricted controller messenger\n * should be alowed to call.\n * @param options.allowedEvents - The list of events that this restricted controller messenger\n * should be allowed to subscribe to.\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 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 controller 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 }): RestrictedControllerMessenger<\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 RestrictedControllerMessenger({\n controllerMessenger: this,\n name,\n allowedActions,\n allowedEvents,\n });\n }\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ControllerMessenger.d.cts","sourceRoot":"","sources":["../src/ControllerMessenger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,4CAAwC;AAEhF,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,U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AAqC,SAAS,EAAE,CAAC,CAAC;SACnE;QAED,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC1C,IAAI,QAAQ,EAAE;YACZ,uBAAA,IAAI,8CAAmB,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,mCAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,kBAAkB;QAChB,uBAAA,IAAI,mCAAQ,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,aAAa,CAIX,EACA,IAAI,EACJ,cAAc,EACd,aAAa,GAWd;QAQC,OAAO,IAAI,6BAA6B,CAAC;YACvC,mBAAmB,EAAE,IAAI;YACzB,IAAI;YACJ,cAAc;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { RestrictedControllerMessenger } from './RestrictedControllerMessenger';\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 messaging system for controllers.\n *\n * The controller 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 ControllerMessenger<\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 controller messenger\n *\n * Returns a wrapper around the controller messenger instance that restricts access to actions\n * and events. The provided allowlists grant the ability to call the listed actions and subscribe\n * to the listed events. The \"name\" provided grants ownership of any actions and events under\n * that namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Controller 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 controller messenger returned.\n * @param options.allowedActions - The list of actions that this restricted controller messenger\n * should be alowed to call.\n * @param options.allowedEvents - The list of events that this restricted controller messenger\n * should be allowed to subscribe to.\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 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 controller 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 }): RestrictedControllerMessenger<\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 RestrictedControllerMessenger({\n controllerMessenger: this,\n name,\n allowedActions,\n allowedEvents,\n });\n }\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RestrictedControllerMessenger.cjs","sourceRoot":"","sources":["../src/RestrictedControllerMessenger.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAgCA;;;;;;;;;;;;;;;GAeG;AACH,MAAa,6BAA6B;IAkBxC;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,EACV,mBAAmB,EACnB,IAAI,EACJ,cAAc,EACd,aAAa,GAMd;;QAvCQ,qEAGP;QAEO,gEAA2B;QAE3B,gEAA6D;QAE7D,+DAA2D;QA+BlE,uBAAA,IAAI,sDAAwB,mBAAmB,MAAA,CAAC;QAChD,uBAAA,IAAI,iDAAmB,IAAI,MAAA,CAAC;QAC5B,uBAAA,IAAI,iDAAmB,cAAc,MAAA,CAAC;QACtC,uBAAA,IAAI,gDAAkB,aAAa,MAAA,CAAC;IACtC,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,qBAAqB,CAEnB,MAAkB,EAAE,OAA0C;QAC9D,wBAAwB,CAAC,sCAAsC;QAC/D,IAAI,CAAC,uBAAA,IAAI,qGAAsB,MAA1B,IAAI,EAAuB,MAAM,CAAC,EAAE;YACvC,MAAM,IAAI,KAAK,CACb,yDACE,uBAAA,IAAI,qDACN,IAAI,CACL,CAAC;SACH;QACD,uBAAA,IAAI,0DAAqB,CAAC,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnE,CAAC;IAED;;;;;;;;;;OAUG;IACH,uBAAuB,CAErB,MAAkB;QAClB,wBAAwB,CAAC,sCAAsC;QAC/D,IAAI,CAAC,uBAAA,IAAI,qGAAsB,MAA1B,IAAI,EAAuB,MAAM,CAAC,EAAE;YACvC,MAAM,IAAI,KAAK,CACb,2DACE,uBAAA,IAAI,qDACN,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B,uCAAuC;IACvC,MAAM,cAAc,GAAoB,uBAAA,IAAI,qDAAgB,CAAC;IAC7D,OAAO,CACL,uBAAA,IAAI,qGAAsB,MAA1B,IAAI,EAAuB,UAAU,CAAC;QACtC,CAAC,cAAc,KAAK,IAAI,IAAI,cAAc,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CACjE,CAAC;AACJ,CAAC,qHAQqB,IAAY;IAChC,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,qDAAgB,GAAG,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import type {\n ActionConstraint,\n ActionHandler,\n ControllerMessenger,\n EventConstraint,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n NamespacedName,\n NotNamespacedBy,\n SelectorEventHandler,\n SelectorFunction,\n} from './ControllerMessenger';\n\n/**\n * A universal supertype of all `RestrictedControllerMessenger` instances.\n * This type can be assigned to any `RestrictedControllerMessenger` type.\n *\n * @template ControllerName - Name of the controller. Optionally can be used to\n * narrow this type to a constraint for the messenger of a specific controller.\n */\nexport type RestrictedControllerMessengerConstraint<\n ControllerName extends string = string,\n> = RestrictedControllerMessenger<\n ControllerName,\n ActionConstraint,\n EventConstraint,\n string,\n string\n>;\n\n/**\n * A restricted controller messenger.\n *\n * This acts as a wrapper around the controller 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 RestrictedControllerMessenger<\n Namespace extends string,\n Action extends ActionConstraint,\n Event extends EventConstraint,\n AllowedAction extends string,\n AllowedEvent extends string,\n> {\n readonly #controllerMessenger: ControllerMessenger<\n ActionConstraint,\n EventConstraint\n >;\n\n readonly #controllerName: Namespace;\n\n readonly #allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n\n readonly #allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n\n /**\n * Constructs a restricted controller 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 - The controller options.\n * @param options.controllerMessenger - The controller 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 controller messenger returned.\n * @param options.allowedActions - The list of actions that this restricted controller messenger\n * should be alowed to call.\n * @param options.allowedEvents - The list of events that this restricted controller messenger\n * should be allowed to subscribe to.\n */\n constructor({\n controllerMessenger,\n name,\n allowedActions,\n allowedEvents,\n }: {\n controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;\n name: Namespace;\n allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n }) {\n this.#controllerMessenger = controllerMessenger;\n this.#controllerName = 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.#controllerName\n }:'`,\n );\n }\n this.#controllerMessenger.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.#controllerName\n }:'`,\n );\n }\n this.#controllerMessenger.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.#controllerMessenger.call<ActionType>(\n actionType,\n ...params,\n );\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.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.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.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.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 controller 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 controller 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.#controllerMessenger.subscribe(event, handler, selector);\n }\n return this.#controllerMessenger.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 controller 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.#controllerMessenger.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.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.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.#controllerName}:`);\n }\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RestrictedControllerMessenger.d.cts","sourceRoot":"","sources":["../src/RestrictedControllerMessenger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,gB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,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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RestrictedControllerMessenger.d.mts","sourceRoot":"","sources":["../src/RestrictedControllerMessenger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,gBAAgB,EACjB,kCAA8B;AAE/B;;;;;;GAMG;AACH,MAAM,MAAM,uCAAuC,CACjD,cAAc,SAAS,MAAM,GAAG,MAAM,IACpC,6BAA6B,CAC/B,cAAc,EACd,gBAAgB,EAChB,eAAe,EACf,MAAM,EACN,MAAM,CACP,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,6BAA6B,CACxC,SAAS,SAAS,MAAM,EACxB,MAAM,SAAS,gBAAgB,EAC/B,KAAK,SAAS,eAAe,EAC7B,aAAa,SAAS,MAAM,EAC5B,YAAY,SAAS,MAAM;;IAa3B;;;;;;;;;;;;;;;;;OAiBG;gBACS,EACV,mBAAmB,EACnB,IAAI,EACJ,cAAc,EACd,aAAa,GACd,EAAE;QACD,mBAAmB,EAAE,mBAAmB,CAAC,gBAAgB,EAAE,eAAe,CAAC,CAAC;QAC5E,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;IAOD;;;;;;;;;;;;OAYG;IACH,qBAAqB,CACnB,UAAU,SAAS,MAAM,CAAC,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AsB,MAA1B,IAAI,EAAuB,KAAK,CAAC,EAAE;YACtC,MAAM,IAAI,KAAK,CACb,6CAA6C,uBAAA,IAAI,qDAAgB,IAAI,CACtE,CAAC;SACH;QACD,uBAAA,IAAI,0DAAqB,CAAC,uBAAuB,CAAC,KAAK,CAAC,CAAC;IAC3D,CAAC;CAqDF;iaA1CG,SAAwB;IAIxB,uCAAuC;IACvC,MAAM,aAAa,GAAoB,uBAAA,IAAI,oDAAe,CAAC;IAC3D,OAAO,CACL,uBAAA,IAAI,qGAAsB,MAA1B,IAAI,EAAuB,SAAS,CAAC;QACrC,CAAC,aAAa,KAAK,IAAI,IAAI,aAAa,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAC9D,CAAC;AACJ,CAAC,2GAWC,UAA0B;IAI1B,uCAAuC;IACvC,MAAM,cAAc,GAAoB,uBAAA,IAAI,qDAAgB,CAAC;IAC7D,OAAO,CACL,uBAAA,IAAI,qGAAsB,MAA1B,IAAI,EAAuB,UAAU,CAAC;QACtC,CAAC,cAAc,KAAK,IAAI,IAAI,cAAc,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CACjE,CAAC;AACJ,CAAC,qHAQqB,IAAY;IAChC,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,qDAAgB,GAAG,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import type {\n ActionConstraint,\n ActionHandler,\n ControllerMessenger,\n EventConstraint,\n ExtractActionParameters,\n ExtractActionResponse,\n ExtractEventHandler,\n ExtractEventPayload,\n NamespacedName,\n NotNamespacedBy,\n SelectorEventHandler,\n SelectorFunction,\n} from './ControllerMessenger';\n\n/**\n * A universal supertype of all `RestrictedControllerMessenger` instances.\n * This type can be assigned to any `RestrictedControllerMessenger` type.\n *\n * @template ControllerName - Name of the controller. Optionally can be used to\n * narrow this type to a constraint for the messenger of a specific controller.\n */\nexport type RestrictedControllerMessengerConstraint<\n ControllerName extends string = string,\n> = RestrictedControllerMessenger<\n ControllerName,\n ActionConstraint,\n EventConstraint,\n string,\n string\n>;\n\n/**\n * A restricted controller messenger.\n *\n * This acts as a wrapper around the controller 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 RestrictedControllerMessenger<\n Namespace extends string,\n Action extends ActionConstraint,\n Event extends EventConstraint,\n AllowedAction extends string,\n AllowedEvent extends string,\n> {\n readonly #controllerMessenger: ControllerMessenger<\n ActionConstraint,\n EventConstraint\n >;\n\n readonly #controllerName: Namespace;\n\n readonly #allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n\n readonly #allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n\n /**\n * Constructs a restricted controller 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 - The controller options.\n * @param options.controllerMessenger - The controller 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 controller messenger returned.\n * @param options.allowedActions - The list of actions that this restricted controller messenger\n * should be alowed to call.\n * @param options.allowedEvents - The list of events that this restricted controller messenger\n * should be allowed to subscribe to.\n */\n constructor({\n controllerMessenger,\n name,\n allowedActions,\n allowedEvents,\n }: {\n controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;\n name: Namespace;\n allowedActions: NotNamespacedBy<Namespace, AllowedAction>[];\n allowedEvents: NotNamespacedBy<Namespace, AllowedEvent>[];\n }) {\n this.#controllerMessenger = controllerMessenger;\n this.#controllerName = 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.#controllerName\n }:'`,\n );\n }\n this.#controllerMessenger.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.#controllerName\n }:'`,\n );\n }\n this.#controllerMessenger.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.#controllerMessenger.call<ActionType>(\n actionType,\n ...params,\n );\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.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.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.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.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 controller 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 controller 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.#controllerMessenger.subscribe(event, handler, selector);\n }\n return this.#controllerMessenger.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 controller 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.#controllerMessenger.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.#controllerName}:'`,\n );\n }\n this.#controllerMessenger.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.#controllerName}:`);\n }\n}\n"]}
|