import { EventDetailMap } from '@genesislcap/foundation-events'; import { InterfaceSymbol } from '@microsoft/fast-foundation'; import { Logger } from '@genesislcap/foundation-logger'; /** * Creates a strongly typed broadcast channel. * * @remarks * `registerTypedBroadcastChannel` may be preferred, as it will create and register the channel in the DI. * * @example * ```ts * const channel = createTypedBroadcastChannel(GenesisResourcesChannelId); * ``` * @param name - The channel name. * @returns The created TypedBroadcastChannel. * @public */ export declare const createTypedBroadcastChannel: (name: string) => DefaultTypedBroadcastChannel; /** * Default TypedBroadcastChannel implementation. * @public */ export declare class DefaultTypedBroadcastChannel extends BroadcastChannel implements TypedBroadcastChannel { /** {@inheritDoc TypedBroadcastChannel.postMessage} */ postMessage: PostMessage; /** {@inheritDoc TypedBroadcastChannel.isMessageType} */ isMessageType: (type: T, event: TypedMessageEvent) => event is TypedMessageEvent; } /** * @public */ export declare const logger: Logger; /** * @internal */ declare type OnMessage = ((this: BroadcastChannel, ev: TypedMessageEvent) => any) | null; /** * @internal */ declare type OnMessageError = OnMessage; /** * @internal */ declare type PostMessage = (...args: TEventDetailMap[K] extends void ? [type: K] : [type: K, detail: TEventDetailMap[K]]) => void; /** * Creates a dependency injection key for the broadcast channel being registered which is lazily created when injected. * @example In a package's config.ts * ```ts * export interface GenesisResourcesChannel extends TypedBroadcastChannel {} * ... * export const GenesisResourcesChannel = registerTypedBroadcastChannel(GenesisResourcesChannelId); * ``` * @param name - The channel name. * @returns The created key. * @public */ export declare const registerTypedBroadcastChannel: (name: string) => InterfaceSymbol>; /** * A TypedBroadcastChannel for sending and receiving typed messages based on {@link @genesislcap/foundation-events#EventDetailMap} input. * * @example * ```ts * export type GenesisResourcesEvents = { * 'resources-loaded': ResourceItem[]; * 'resources-unloaded': void; * }; * ... * protected channel: TypedBroadcastChannel; * ``` * * See the {@link registerTypedBroadcastChannel} and {@link createTypedBroadcastChannel} APIs below. * * @public */ export declare interface TypedBroadcastChannel extends Omit { /** * Post strongly typed messages. * @example * The event detail map provided to the channel on creation will provide `postMessage` typing. * ```ts * type GenesisResourcesEvents = { * 'resources-loaded': ResourceItem[]; * 'resources-unloaded': void; * }; * ... * channel.postMessage('resources-loaded', this.resources); // ok, as this.resources is an array of ResourceItems. * channel.postMessage('resources-loaded'); // not ok, as an array is expected for this message type. * channel.postMessage('resources-unloaded'); // ok, as this message type maps to void for its detail. * ``` */ postMessage: PostMessage; /** * On message handler. * @example Use the `channel.isMessageType` method to narrow based on message types. * ```ts * import { GenesisResourcesChannel } from '@genesislcap/foundation-comms'; * ... * @GenesisResourcesChannel protected channel: GenesisResourcesChannel; * ... * this.channel.onmessage = (ev) => { * if (this.channel.isMessageType('resources-loaded', ev)) { * if (!ev.data.detail.length) { * throw Error('No backend resources available'); * } * } * }; * ... * this.channel.close(); * ``` */ onmessage: OnMessage; onmessageerror: OnMessageError; /** * A type predicate function to narrow down message events based on their 'type' property. * @param type - The message type. * @param event - The message event. */ isMessageType(type: T, event: TypedMessageEvent): event is TypedMessageEvent; } /** * A TypedMessageEvent for BroadcastChannel transmission. * @public */ export declare type TypedMessageEvent = MessageEvent<{ type: T; detail: TEventDetailMap[T]; }>; export { }