import * as signalR from "@microsoft/signalr"; import { EvaContext } from "@springtree/eva-sdk-core-service"; import { Publisher } from "@springtree/eva-sdk-core-settings"; import { GetApplicationConfigurationResponse } from "@springtree/eva-services-core"; import { IJoinHub } from "../request/base-hub.js"; export interface IHubConnectionOptions { /** * A specific application token to use for this signalr connection * By default the current application token from the settings will be used. * The setting will be updated whenever a new EVA-App-Token is returned. * * @type {string} */ applicationToken?: string; /** * Can be used to set the `EVA-Requested-OrganizationUnitID` header. * This is used to select a specific organization unit when making calls without * a specific user token by ID. * * @type {string} */ requestedOrganizationUnitID?: string; /** * Can be used to set the `EVA-Requested-OrganizationUnit-Query` header. * This is used to select a specific organization unit when making calls without * a specific user token using an exact match query on name and backend id. * * * @type {{ Name: string; BackendID: string; }} */ requestedOrganizationUnitQuery?: { Name: string; BackendID: string; }; /** * The Global Blue group id as returned from GlobalBlueStartTaxFree. * Required for connecting to the Global Blue hub * * @type {string} */ globalBlueGroupID?: string; /** * Sets the SignalR timeout for the server to respond. * So if the server does not send anything within this time interval the connection will error out. * Default is 30s but we're matching the EVA backend default of 10s * * See: https://learn.microsoft.com/en-gb/javascript/api/@microsoft/signalr/hubconnection?view=signalr-js-latest#@microsoft-signalr-hubconnection-servertimeoutinmilliseconds */ serverTimeoutInMilliseconds?: number; /** * Sets the SignalR keep alive interval. The default is 15s but we're matching the EVA backend default of 5s * * See: https://learn.microsoft.com/en-gb/javascript/api/@microsoft/signalr/hubconnection?view=signalr-js-latest#@microsoft-signalr-hubconnection-keepaliveintervalinmilliseconds */ keepAliveIntervalInMilliseconds?: number; /** * Default behavior is for the signalr hubs to try to reconnect once every 3 seconds. * This can be disabled by setting this value to 0. * Reconnect attempts will first validate any provided user token and if the token is expired * the reconnect attempts wil cease */ reconnectIntervalSeconds?: number; /** * Token validation attempts done during a reconnect have a default timeout of 3000ms * This setting can be used to alter this timeout */ validateTokenTimeoutMs?: number; /** * Can be used to set the `EVA-IDs-Mode` header which can be used to let the EVA * backend to use external identifiers instead of EVA identifiers for * things like products. * Is usually set using the service settings but can be overridden per call * using this setting */ backendIdMode?: string; /** * Normally the authentication token is supplied from the EvaContext * This option takes precedence and is also used for sending a LiveGuardGetTrustToken response for unauthenticated users * to ensure they are in the actual store */ authenticationToken?: string; } /** * The hub connection event handler type */ export type THubConnectionEventHandler = (event: { connected: boolean; }) => void; export declare class BaseHub extends Publisher { /** * This is the connection to the signalr server which will become active * after you call join(...) * * @type {signalR.HubConnection} */ connection: signalR.HubConnection; /** * Indicates the hub instance has joined (connected) the SignalR server * * @type {boolean} */ connected: boolean; /** * The timer for reconnection attempts * * @private * @type {ReturnType|undefined} */ private reconnectTimer; /** * The subscription used to join the hub * Is stored internally to facilitate leaving the hub without making the * caller hang on to the object * * @protected * @type {T} */ protected subscription: T | undefined; /** * Extending classes can register their event handlers here to leverage the * automatic handling of receiving these events. Leaving the hub will cleanup * all event handlers. * * @protected */ protected eventHandlers: { [eventName: string]: unknown; }; /** * This is the collection of messages that need to resent when a connection * is re-established * * @protected */ protected connectMessages: { [id: string]: () => Promise; }; /** * This tracks the current reconnect attempt count * Non zero values will trigger automatic reconnection attempts * * @protected * @type {number} */ protected reconnectCount: number; /** * Set to true when the hub is asked to disconnect when this * is not set and a close is detected the reconnect logic will * kick in * * @private * @type {boolean} */ private disconnectRequested; /** * Used to track any in flight reconnect attempts so we don't end up creating multiple * * @private * @type {Promise} */ private pendingReconnectAttempt?; /** * The time interval between reconnect attempts. Can be disabled by setting to 0 * * @protected * @type {number} */ protected reconnectIntervalSeconds: number; /** * Service call timeout for validate token calls. * Defaults to 3000ms * * @protected * @type {number} */ protected validateTokenTimeoutMs: number; /** * The name of the hub this instance is for * * @protected * @type {string} */ protected hubName: string; /** * The context (endpoint + user) to connect to the signalr server on * * @protected * @type {EvaContext} */ protected context: EvaContext; /** * Sets or un-sets an event event handler for a named event * This protected method will be overridden by the hub classes with the exact * event names and handler types * * @protected * @param {string} eventName * @param {(any|undefined)} handler */ setEventHandler(eventName: string, handler: unknown): void; /** * Creates an instance of a SignalR hub connection * Will use the provided endpoint and user token to prepare the connection */ constructor({ configuration, context, events, hubName, options, }: { configuration: GetApplicationConfigurationResponse; context: EvaContext; events: string[]; hubName: string; options?: IHubConnectionOptions; }); /** * Connect to the SignalR hub and setup event listeners * * @param {T} subscription */ join(subscription: T): Promise; /** * Disconnects from the SignalR hub and remove event listeners * */ leave(): Promise; /** * Generic event handler that will call the corresponding handler method in the *current eventHandlers mapping * * @protected * @param {*} event The event payload * @param {string} eventName The name of the event */ protected handleEvent(event: unknown, eventName: string): void; /** * Extending classes can use this to setup their required listeners * * @protected */ protected setupEventListeners(): void; /** * Extending classes can use this to remove their required listeners * * @protected */ protected teardownEventListeners(): void; /** * Registers a message handler that is to be triggered when a the signalr * connection is re-established * * @protected * @param {{ * id: string, * messageFunc: () => Promise, * }} { * id, * messageFunc, * } */ protected registerConnectMessage({ id, messageFunc, }: { id: string; messageFunc: () => Promise; }): void; /** * Unregister a message handler that is to be triggered when a the signalr * connection is re-established * * @protected * @param {{ * id: string, * }} { * id, * } */ protected unregisterConnectMessage({ id }: { id: string; }): void; /** * Helper method to send messages that should be sent when a connection is * re-established * * @protected */ protected resendConnectionMessages(): Promise; /** * Helper method to try to reconnect to the SignalR server * We will use the reconnect count and multiply it with a small interval * which will effectively increment it for each attempt * Max delay between attempts is set to 10 minutes * * @protected * @param {IHubConnection} hubDetails */ protected reconnectAttempt(): Promise; private validateToken; /** * Force an immediate reconnection attempt. Can be useful for clients coming * out of a sleep or apps being resumed */ reconnect(): Promise; /** * Prevent reconnect attempt until resumed with .reconnect() */ pauseReconnect(): Promise; } //# sourceMappingURL=base-hub.d.ts.map