From 538f95f94c5cfbae698b536d3303cd65f41c6d62 Mon Sep 17 00:00:00 2001
From: Vyacheslav Tumanov <me@slavatumanov.me>
Date: Fri, 15 Sep 2023 13:32:53 +0500
Subject: [PATCH] Prevent from using variables before initialization on
 connection (#3702)

Signed-off-by: Vyacheslav Tumanov <me@slavatumanov.me>
---
 plugins/workbench-resources/src/connect.ts | 45 +++++++++++-----------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/plugins/workbench-resources/src/connect.ts b/plugins/workbench-resources/src/connect.ts
index 3efd9373eb..aacafe6b77 100644
--- a/plugins/workbench-resources/src/connect.ts
+++ b/plugins/workbench-resources/src/connect.ts
@@ -64,7 +64,10 @@ export async function connect (title: string): Promise<Client | undefined> {
   let clientSet = false
 
   let version: Version | undefined
-
+  let serverEndpoint = endpoint.replace(/^ws/g, 'http')
+  if (serverEndpoint.endsWith('/')) {
+    serverEndpoint = serverEndpoint.substring(0, serverEndpoint.length - 1)
+  }
   const clientFactory = await getResource(client.function.GetClient)
   _client = await clientFactory(
     token,
@@ -87,24 +90,28 @@ export async function connect (title: string): Promise<Client | undefined> {
         }
 
         void (async () => {
-          const newVersion = await _client?.findOne<Version>(core.class.Version, {})
-          console.log('Reconnect Model version', newVersion)
+          if (_client !== undefined) {
+            const newVersion = await _client.findOne<Version>(core.class.Version, {})
+            console.log('Reconnect Model version', newVersion)
 
-          const currentVersionStr = versionToString(version as Version)
-          const reconnectVersionStr = versionToString(newVersion as Version)
+            const currentVersionStr = versionToString(version as Version)
+            const reconnectVersionStr = versionToString(newVersion as Version)
 
-          if (currentVersionStr !== reconnectVersionStr) {
-            // It seems upgrade happened
-            location.reload()
-          }
-          const serverVersion: { version: string } = await (await fetch(serverEndpoint + '/api/v1/version', {})).json()
+            if (currentVersionStr !== reconnectVersionStr) {
+              // It seems upgrade happened
+              location.reload()
+            }
+            const serverVersion: { version: string } = await (
+              await fetch(serverEndpoint + '/api/v1/version', {})
+            ).json()
 
-          console.log('Server version', serverVersion.version)
-          if (serverVersion.version !== '' && serverVersion.version !== currentVersionStr) {
-            versionError = `${currentVersionStr} => ${serverVersion.version}`
-            setTimeout(() => {
-              window.location.reload()
-            }, 5000)
+            console.log('Server version', serverVersion.version)
+            if (serverVersion.version !== '' && serverVersion.version !== currentVersionStr) {
+              versionError = `${currentVersionStr} => ${serverVersion.version}`
+              setTimeout(() => {
+                window.location.reload()
+              }, 5000)
+            }
           }
         })()
       } catch (err) {
@@ -131,12 +138,6 @@ export async function connect (title: string): Promise<Client | undefined> {
     clientSet = true
     return
   }
-
-  let serverEndpoint = endpoint.replace(/^ws/g, 'http')
-  if (serverEndpoint.endsWith('/')) {
-    serverEndpoint = serverEndpoint.substring(0, serverEndpoint.length - 1)
-  }
-
   try {
     version = await _client.findOne<Version>(core.class.Version, {})
     console.log('Model version', version)