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,IAAI;IASxC;;;;;;OAMG;IACI,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,GAAG,CAAC;IAyB5E;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,IAAI;IAElE;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAExD;;OAEG;IACH,OAAO,CAAC,IAAI;IAYZ;;;;OAIG;IACH,OAAO,CAAC,YAAY;CAOvB","file":"socket-signalr.d.ts","sourcesContent":["/// <reference types='signalr' />\r\n\r\nimport { HttpError, HubConnection, HubConnectionBuilder, HubConnectionState } from '@microsoft/signalr';\r\n\r\n/**\r\n * Socket message flags. These can be combined.\r\n */\r\nexport enum SocketMessageFlags {\r\n    None = 0,\r\n    Data = 1,\r\n    Progress = 2,\r\n    Completed = 4,\r\n    Error = 8,\r\n    ConnectionError = 16,\r\n    Exception = 32\r\n}\r\n\r\n/**\r\n * Socket message.\r\n */\r\nexport interface SocketMessage<T> {\r\n    type: SocketMessageFlags;\r\n    connectionError?: SignalR.ConnectionError | HttpError;\r\n    message: T;\r\n}\r\n\r\n/**\r\n * SignalR based socket class.\r\n */\r\nexport abstract class SocketSignalR<T> {\r\n    private connection: HubConnection;\r\n    private connectionV1: SignalR.Hub.Connection;\r\n    private proxy: SignalR.Hub.Proxy;\r\n    private started = false;\r\n    private lastError: SignalR.ConnectionError | HttpError;\r\n    private processHandler = (message: string) => this.processMessage(message);\r\n\r\n    /**\r\n     * Gets signalR connection URL.\r\n     */\r\n    private get url(): string {\r\n        return `${this.gatewayUrl}${this.connectionUrl}`;\r\n    }\r\n\r\n    /**\r\n     * Instantiates a new instance of the SocketSignalR class.\r\n     */\r\n    constructor(private gatewayUrl: string, private connectionUrl: string, private proxyName: string, private isGatewayV1: boolean) {\r\n        gatewayUrl = gatewayUrl ? gatewayUrl : '';\r\n    }\r\n\r\n    /**\r\n     * Subscribe to the proxy with method name and handler.\r\n     *\r\n     * @param name the name of subscription for a method.\r\n     */\r\n    public subscribe(name: string): SignalR.Hub.Proxy | void {\r\n        this.init();\r\n        if (this.lastError) {\r\n            // send last error;\r\n            this.clientHandler({ type: SocketMessageFlags.Error, connectionError: this.lastError, message: null });\r\n        }\r\n\r\n        if (this.isGatewayV1) {\r\n            return this.proxy.on(name, this.processHandler);\r\n        }\r\n\r\n        this.connection.on(name, this.processHandler);\r\n    }\r\n\r\n    /**\r\n     * Unsubscribe to the subscribed method call.\r\n     *\r\n     * @param name the name of subscription for a method.\r\n     */\r\n    public unsubscribe(name: string): void {\r\n        if (this.isGatewayV1) {\r\n            this.proxy.off(name, this.processHandler);\r\n            return;\r\n        }\r\n\r\n        this.connection.off(name, this.processHandler);\r\n    }\r\n\r\n    /**\r\n     * Start request connection.\r\n     */\r\n    public start(): Promise<any> | JQueryPromise<any> {\r\n        if (this.isGatewayV1) {\r\n            return this.connectionV1.start().then(() => this.started = true);\r\n        }\r\n\r\n        return this.connection.start().then(() => this.started = true);\r\n    }\r\n\r\n    /**\r\n     * Stop request connection.\r\n     */\r\n    public stop(): SignalR.Connection | void {\r\n        this.started = false;\r\n        if (this.isGatewayV1) {\r\n            this.connectionV1.stop();\r\n        }\r\n\r\n        this.connection.stop();\r\n    }\r\n\r\n    /**\r\n     * Invoke a method with parameters.\r\n     *\r\n     * @param name the method name to execute.\r\n     * @param args the parameters to pass.\r\n     * @return Promise the promise object.\r\n     */\r\n    public invoke(name: string, ...args: any[]): Promise<T> | JQueryPromise<any> {\r\n        if (!this.started) {\r\n            throw new Error('request socket has not been started.');\r\n        }\r\n\r\n        if (this.isGatewayV1) {\r\n            if (this.connectionV1.state === 4 /* SignalR.ConnectionState.Disconnected: @types/signalr doesn't take conversion properly */) {\r\n                return (<JQueryPromise<any>>this.start()).then(() => this.proxy.invoke(name, ...args));\r\n            }\r\n\r\n            return this.proxy.invoke(name, ...args);\r\n        }\r\n\r\n        if (this.connection.state === HubConnectionState.Disconnected) {\r\n            return (<Promise<T>>this.start()).then(() => this.connection.invoke<T>(name, ...args));\r\n        }\r\n\r\n        try {\r\n            return this.connection.invoke<T>(name, ...args);\r\n        } catch (error) {\r\n            this.errorMessage(error);\r\n            return Promise.reject(error);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * The client handler.\r\n     */\r\n    protected abstract clientHandler(messages: SocketMessage<T>): void;\r\n\r\n    /**\r\n     * Process the message.\r\n     *\r\n     * @param messages the messages.\r\n     */\r\n    protected abstract processMessage(message: string): void;\r\n\r\n    /**\r\n     * Initiate the connection to gateway.\r\n     */\r\n    private init(): void {\r\n        if (this.isGatewayV1) {\r\n            this.connectionV1 = $.hubConnection(this.url);\r\n            this.connectionV1.error((error) => this.errorMessage(error));\r\n            this.proxy = this.connectionV1.createHubProxy(this.proxyName);\r\n            this.proxy.connection.error((error) => this.errorMessage(error));\r\n            return;\r\n        }\r\n\r\n        this.connection = new HubConnectionBuilder().withUrl(this.url).build();\r\n    }\r\n\r\n    /**\r\n     * Error message from signalr connection.\r\n     *\r\n     * @param error the error produced on the connection.\r\n     */\r\n    private errorMessage(error: SignalR.ConnectionError | HttpError): void {\r\n        if (this.clientHandler) {\r\n            this.clientHandler({ type: SocketMessageFlags.ConnectionError, connectionError: error, message: null });\r\n        } else {\r\n            this.lastError = error;\r\n        }\r\n    }\r\n}\r\n"]}