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,CAAC,CAAC;YApBF,CAoBE,CACL,CAAC;QACJ,CAAC;QAES,yCAAc,GAAxB,UAAyB,OAAY;YACnC,IAAM,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC;YACtB,IAAI,CAAC,UAAU,CAAC,EAAE,CAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YACtC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC;QAClC,CAAC;QAES,uCAAY,GAAtB,UAAuB,KAAY;YAAnC,iBAWC;YATC,IAAI,CAAC,MAAM,GAAG,aAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAGjC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC;iBACzB,MAAM,CAAC,UAAC,EAAE,IAAK,OAAA,KAAI,CAAC,UAAU,CAAC,EAAE,CAAC,IAAI,IAAI,EAA3B,CAA2B,CAAC;iBAC3C,OAAO,CAAC,UAAC,EAAE;gBACV,KAAI,CAAC,UAAU,CAAC,EAAE,CAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBACnC,KAAI,CAAC,UAAU,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC;YAClC,CAAC,CAAC,CAAC;QACP,CAAC;QACH,uBAAC;IAAD,CAAC,AA7FD,CAA8C,cAAW,GA6FxD","sourcesContent":["import { Task, CancellablePromise, global } from '@theintern/common';\n\nimport BaseChannel, { ChannelOptions, Message } from './Base';\nimport { parseUrl } from '../browser/util';\nimport { stringify } from '../common/util';\n\nexport default class WebSocketChannel extends BaseChannel {\n /** Time to wait for response before rejecting a send */\n timeout!: number;\n\n protected _socket: WebSocket;\n protected _sendQueue: {\n [key: string]:\n | { resolve: (value: any) => void; reject: (error: Error) => void }\n | undefined;\n };\n protected _ready: CancellablePromise<any>;\n protected _sequence: number;\n\n constructor(options: ChannelOptions) {\n super(options);\n this.timeout = 10000;\n if (options.timeout !== undefined) {\n this.timeout = options.timeout;\n }\n\n if (!options.port) {\n throw new Error('A port is required for a WebSocket channel');\n }\n\n const url = parseUrl(options.url);\n const host = url!.hostname;\n const protocol = url!.protocol === 'https' ? 'wss' : 'ws';\n this._socket = new global.WebSocket(\n `${protocol}://${host}:${options.port}`\n );\n\n this._ready = new Task((resolve, reject) => {\n this._socket.addEventListener('open', resolve);\n this._socket.addEventListener('error', reject);\n });\n\n this._socket.addEventListener('message', (event) => {\n this._handleMessage(JSON.parse(event.data));\n });\n\n this._socket.addEventListener('error', (_event) => {\n this._handleError(new Error('WebSocket error'));\n });\n\n this._sendQueue = Object.create(null);\n this._sequence = 1;\n }\n\n protected _sendData(name: string, data: any) {\n return this._ready.then(\n () =>\n new Task<void>((resolve, reject) => {\n const id = String(this._sequence++);\n const sessionId = this.sessionId;\n const message: Message = { id, sessionId, name, data };\n\n this._socket.send(stringify(message));\n\n const timer = setTimeout(() => {\n reject(new Error('Send timed out'));\n }, this.timeout);\n\n this._sendQueue[id] = {\n resolve(data: any) {\n clearTimeout(timer);\n resolve(data);\n },\n reject(error: Error) {\n reject(error);\n },\n };\n })\n );\n }\n\n protected _handleMessage(message: any) {\n const id = message.id;\n this._sendQueue[id]!.resolve(message);\n this._sendQueue[id] = undefined;\n }\n\n protected _handleError(error: Error) {\n // Make the _ready task a reject to reject future _sendData calls\n this._ready = Task.reject(error);\n\n // Reject any open sends\n Object.keys(this._sendQueue)\n .filter((id) => this._sendQueue[id] != null)\n .forEach((id) => {\n this._sendQueue[id]!.reject(error);\n this._sendQueue[id] = undefined;\n });\n }\n}\n"]} |