From 5a44f3ac1a388a0cfa8dbd705279da2f151f0153 Mon Sep 17 00:00:00 2001 From: garzj Date: Tue, 28 May 2024 02:40:40 +0200 Subject: [PATCH 1/2] Switch to keep alive interval, drop /terminate --- .../actions-for-nautilus-configurator.html | 17 ++++++++------ .../actions-for-nautilus-configurator.py | 23 +++++++++++++++---- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/configurator/actions-for-nautilus-configurator.html b/configurator/actions-for-nautilus-configurator.html index 6755fbe..d1c5d9b 100644 --- a/configurator/actions-for-nautilus-configurator.html +++ b/configurator/actions-for-nautilus-configurator.html @@ -37,17 +37,20 @@ /* * Because this is a local UI tightly related to - * backend server that it is using, unload should - * stop that server :) + * backend server that it is using, pings will + * keep it alive :) */ - function onUnload(event) { + const KEEP_ALIVE_INTERVAL = 7500; + + function sendKeepAlive() { $.ajax({ - url: '/terminate', + url: '/keep-alive', type: 'post', - data: "" - }); + data: '' + }) + setTimeout(sendKeepAlive, KEEP_ALIVE_INTERVAL); } - addEventListener("beforeunload", onUnload); + sendKeepAlive(); let editor; diff --git a/configurator/actions-for-nautilus-configurator.py b/configurator/actions-for-nautilus-configurator.py index cc6d35a..eef0f34 100755 --- a/configurator/actions-for-nautilus-configurator.py +++ b/configurator/actions-for-nautilus-configurator.py @@ -5,10 +5,12 @@ import os import shutil import datetime +import threading PORT = 8000 HOME = os.environ.get('HOME') JQUERY = os.environ.get('JQUERY') +KEEP_ALIVE_TIMEOUT_S = 10 # config_html = "./actions-for-nautilus-configurator.html" # cmdline_help = "./command-line-help.html" @@ -146,10 +148,22 @@ def restart_nautilus(self): return -def terminate_server(self): - self.send_error(204, "terminated") +keep_alive_timer = None + +def restart_dead_interval(server): + global keep_alive_timer + + if keep_alive_timer is not None: + keep_alive_timer.cancel() + + keep_alive_timer = threading.Timer(KEEP_ALIVE_TIMEOUT_S, server.shutdown) + keep_alive_timer.start() + +def keep_alive_server(self): + restart_dead_interval(self.server) + + self.send_response(200, "OK") self.end_headers() - self.server.shutdown() return @@ -197,7 +211,7 @@ def forbidden(self): actions = { "/restart": restart_nautilus, - "/terminate": terminate_server, + "/keep-alive": keep_alive_server, "/config": save_config } @@ -226,6 +240,7 @@ def do_POST(self): print("localhost:" + str(httpd.server_address[1])) try: httpd.serve_forever() + restart_dead_interval(httpd) except KeyboardInterrupt: httpd.shutdown() print("terminating") From 8ce4cee30bff2cf964599d3d606ff630e0ebdae5 Mon Sep 17 00:00:00 2001 From: garzj Date: Tue, 28 May 2024 02:43:19 +0200 Subject: [PATCH 2/2] Warn user if exiting with unsaved changes --- configurator/javascript/jsonEditorHooks.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/configurator/javascript/jsonEditorHooks.js b/configurator/javascript/jsonEditorHooks.js index 692b5e6..93581eb 100644 --- a/configurator/javascript/jsonEditorHooks.js +++ b/configurator/javascript/jsonEditorHooks.js @@ -131,6 +131,20 @@ function finalizeEditorConfig(e) { save_button.disabled = true button_holder.appendChild(save_button); + /* + * Warn if exiting with unsaved changes + */ + window.addEventListener( + 'beforeunload', + function (e) { + if (save_button.disabled) return; + e.preventDefault(); + e.returnValue = 'You have unsaved changes!'; + return true + }, + true + ); + /* * Create a edit JSON config button and disable it */