From d0ec8b66e9786a764141378d9da80cb71b96ded0 Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Thu, 1 Sep 2022 18:38:25 +0200
Subject: [PATCH] chore: commit save point

---
 application/source/server/inject.go        |  9 +++++-
 application/source/watch/resources.go      | 12 ++++----
 application/source/websocket/connection.go | 34 ++++++++++++++++++++--
 development/examples/e1/web/hello.html     |  5 +++-
 release.json                               |  2 +-
 5 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/application/source/server/inject.go b/application/source/server/inject.go
index dbd8bb8..dfa594a 100644
--- a/application/source/server/inject.go
+++ b/application/source/server/inject.go
@@ -54,6 +54,7 @@ func init() {
     <script type="module">
 
         try {
+            let died = false;
             let counter = 0;
             let socket
             let url = ""
@@ -79,6 +80,11 @@ func init() {
                 socket = new WebSocket(url)
 
                 socket.onopen = function (e) {
+                    
+                    if (died===true ){
+                        window.location.reload();
+                    }
+                    
                     console.log("[conan] Connection established");
                     counter = 0
                 };
@@ -97,12 +103,13 @@ func init() {
                         console.log("[conan] Connection closed cleanly, code=" + event?.code + " reason=" + event?.reason + "");
                     } else {
                         console.error("[conan] Connection died");
+                        died = true
                         setTimeout(connectWebsocket, 3000*counter)
                     }
                 };
 
                 socket.onerror = function (error) {
-                    console.error("[conan] " + error?.message);
+                    console.error("[conan] error", error);
                 };
 
             }
diff --git a/application/source/watch/resources.go b/application/source/watch/resources.go
index e004f06..340b476 100644
--- a/application/source/watch/resources.go
+++ b/application/source/watch/resources.go
@@ -116,8 +116,6 @@ func executeWatchAction(watchPath string) {
 		r, err := result.String()
 		logging.LogDebug("%s", r)
 
-		websocket.SendReloadMessage()
-
 		if err != nil {
 			logging.LogError("watching: execute watch action error: %v", err.Error())
 			continue
@@ -201,9 +199,13 @@ func InitWatch() {
 		})
 	}
 
-	watchList = append(watchList, configuration.Watch{
-		Path: path.Join(configuration.GetConfigurationPath(), constants.ConfigFileName),
-	})
+	c := path.Join(configuration.GetConfigurationPath(), constants.ConfigFileName)
+
+	if _, err := os.Stat(c); err == nil {
+		watchList = append(watchList, configuration.Watch{
+			Path: path.Join(configuration.GetConfigurationPath(), constants.ConfigFileName),
+		})
+	}
 
 	for _, w := range watchList {
 		scanPath(w.Path)
diff --git a/application/source/websocket/connection.go b/application/source/websocket/connection.go
index 52d6127..a079fc6 100644
--- a/application/source/websocket/connection.go
+++ b/application/source/websocket/connection.go
@@ -6,6 +6,8 @@ import (
 	"gitlab.schukai.com/oss/utilities/conan/logging"
 	"log"
 	"net/http"
+	"sync"
+	"time"
 )
 
 var (
@@ -14,7 +16,7 @@ var (
 )
 
 func SendMessageToAll(message []byte) {
-
+	logging.LogInfo("sending message to all")
 	for _, conn := range connections {
 		conn.WriteMessage(websocket.TextMessage, message)
 	}
@@ -30,8 +32,35 @@ func closeConnection(session string) error {
 	return nil
 }
 
-func SendReloadMessage() {
+var timerMutex *sync.Mutex
+var waitForReload bool
+
+func init() {
+	timerMutex = &sync.Mutex{}
+	waitForReload = false
+}
+
+func doReload() {
+	timerMutex.Lock()
+	defer timerMutex.Unlock()
+
 	SendMessageToAll([]byte("reload"))
+	waitForReload = false
+}
+
+func SendReloadMessage() {
+
+	timerMutex.Lock()
+	defer timerMutex.Unlock()
+
+	if waitForReload {
+		return
+	}
+
+	time.AfterFunc(time.Second*2, doReload)
+	logging.LogInfo("timer ...")
+	waitForReload = true
+
 }
 
 func GetWebsocketHandler() http.Handler {
@@ -49,7 +78,6 @@ func GetWebsocketHandler() http.Handler {
 
 		conn.SetCloseHandler(func(code int, text string) error {
 			return closeConnection(session)
-
 		})
 
 		connections[session] = conn
diff --git a/development/examples/e1/web/hello.html b/development/examples/e1/web/hello.html
index 7c0da15..b26c12f 100644
--- a/development/examples/e1/web/hello.html
+++ b/development/examples/e1/web/hello.html
@@ -7,6 +7,9 @@
 </head>
 <body>
     <h1>Hello ...ddd</h1>
-
 <div id="app"></div>
 </body>
+1
+1
+1
+1
diff --git a/release.json b/release.json
index 0bea138..72d452f 100644
--- a/release.json
+++ b/release.json
@@ -1 +1 @@
-{"version":"0.1.19"}
+{"version":"0.1.29"}
-- 
GitLab