export class UIClient {
private static instance: UIClient | null = null
- private ws!: WebSocket
+ private ws?: WebSocket
private responseHandlers: Map<string, ResponseHandler>
private constructor(private uiServerConfiguration: UIServerConfigurationSection) {
}
public setConfiguration(uiServerConfiguration: UIServerConfigurationSection): void {
- this.ws.close()
+ if (this.ws?.readyState === WebSocket.OPEN) {
+ this.ws.close()
+ delete this.ws
+ }
this.uiServerConfiguration = uiServerConfiguration
this.openWS()
}
listener: (event: WebSocketEventMap[K]) => void,
options?: boolean | AddEventListenerOptions
) {
- this.ws.addEventListener(event, listener, options)
+ this.ws?.addEventListener(event, listener, options)
}
public async startSimulator(): Promise<ResponsePayload> {
payload: RequestPayload
): Promise<ResponsePayload> {
return new Promise<ResponsePayload>((resolve, reject) => {
- if (this.ws.readyState === WebSocket.OPEN) {
+ if (this.ws?.readyState === WebSocket.OPEN) {
const uuid = crypto.randomUUID()
const msg = JSON.stringify([uuid, procedureName, payload])
const sendTimeout = setTimeout(() => {