Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to keep alive interval and add unsaved changes warning #65

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions configurator/actions-for-nautilus-configurator.html
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
23 changes: 19 additions & 4 deletions configurator/actions-for-nautilus-configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -197,7 +211,7 @@ def forbidden(self):

actions = {
"/restart": restart_nautilus,
"/terminate": terminate_server,
"/keep-alive": keep_alive_server,
"/config": save_config
}

Expand Down Expand Up @@ -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")
14 changes: 14 additions & 0 deletions configurator/javascript/jsonEditorHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down