UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

1 lines 5.37 kB
{"version":3,"sources":["../../../packages/core/notification/socket-signalr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAA2D,MAAM,oBAAoB,CAAC;AAExG;;GAEG;AACH,oBAAY,kBAAkB;IAC1B,IAAI,IAAI;IACR,IAAI,IAAI;IACR,QAAQ,IAAI;IACZ,SAAS,IAAI;IACb,KAAK,IAAI;IACT,eAAe,KAAK;IACpB,SAAS,KAAK;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC;IAC5B,IAAI,EAAE,kBAAkB,CAAC;IACzB,eAAe,CAAC,EAAE,SAAS,CAAC;IAC5B,OAAO,EAAE,CAAC,CAAC;CACd;AAED;;GAEG;AACH,8BAAsB,aAAa,CAAC,CAAC;IAgBrB,OAAO,CAAC,UAAU;IAAU,OAAO,CAAC,aAAa;IAf7D,OAAO,CAAC,UAAU,CAAgB;IAClC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,cAAc,CAAqD;IAE3E;;OAEG;IACH,OAAO,KAAK,GAAG,GAEd;IAED;;OAEG;gBACiB,UAAU,EAAE,MAAM,EAAU,aAAa,EAAE,MAAM;IAIrE;;;;OAIG;IACI,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAUpC;;;;OAIG;IACI,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAItC;;OAEG;IACI,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC;IAI5B;;OAEG;IACI,IAAI,IAAI,IAAI;IAKnB;;;;;;OAMG;IACI,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC;IAiBvD;;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;IAIZ;;;;OAIG;IACH,OAAO,CAAC,YAAY;CAOvB","file":"socket-signalr.d.ts","sourcesContent":["import { 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?: 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 started = false;\r\n private lastError: 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) {\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): 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 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 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> {\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(): void {\r\n this.started = false;\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> {\r\n if (!this.started) {\r\n throw new Error('request socket has not been started.');\r\n }\r\n\r\n if (this.connection.state === HubConnectionState.Disconnected) {\r\n return 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 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: 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"]}