From a927add8c598c8d66f2244c4a887e23e108e8ece Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 5 Feb 2017 17:35:45 +1300 Subject: [PATCH] client: clearer state transitions, separate disconnected state --- client/dcwebui.js | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/client/dcwebui.js b/client/dcwebui.js index 9012955..041f3b8 100644 --- a/client/dcwebui.js +++ b/client/dcwebui.js @@ -293,7 +293,7 @@ var submit = function() { var str = $("#chatbox").value; if (! str.length) return; - if (hub_state === 0) { + if (hub_state === STATE_READY_FOR_LOGIN) { transition(1); // disables #chatbox hub_last_nick = str.split(":", 2)[0]; @@ -315,7 +315,7 @@ var submit = function() { sock.emit('hello', {'nick' : hub_last_nick, 'pass' : hub_pass}); write("tab-main").system("Connecting..."); - } else if (hub_state === 2) { + } else if (hub_state === STATE_ACTIVE) { if (pm_target !== false) { sock.emit('priv', {'user': pm_target, 'message': str}); writerFor(pm_target).pub(hub_last_nick, str ); @@ -668,6 +668,11 @@ var show_joins = false; var have_cleared_once = false; +var STATE_DISCONNECTED = -1; +var STATE_READY_FOR_LOGIN = 0; +var STATE_CONNECTING = 1; +var STATE_ACTIVE = 2; + /* */ var persistence_set = function(key, value) { @@ -687,17 +692,29 @@ var persistence_get = function(key, fallback) { var transition = function(new_state) { hub_state = new_state; - if (new_state === 0) { - userlist.clear(); - $("#chatbox").spellcheck = false; + switch(new_state) { + case STATE_DISCONNECTED: { + userlist.clear(); + $("#chatbox").disabled = true; + $("#chatbox").value = ''; // clear + } break; - } else if (new_state === 1) { - $("#chatbox").disabled = true; + case STATE_READY_FOR_LOGIN: { + userlist.clear(); + $("#chatbox").spellcheck = false; + $("#chatbox").disabled = false; + $("#chatbox").value = ''; // clear + } break; - } else if (new_state === 2) { - $("#chatbox").disabled = false; - $("#chatbox").spellcheck = true; + case STATE_CONNECTING: { + $("#chatbox").disabled = true; + + } break; + case STATE_ACTIVE: { + $("#chatbox").disabled = false; + $("#chatbox").spellcheck = true; + } break; } }; @@ -765,7 +782,7 @@ window.onload = function() { sock = io.connect(DCWEBUI_CONF.extern); sock.on('cls', function() { - transition(0); + transition(STATE_READY_FOR_LOGIN); var pre_login = persistence_get("login", ""); if (pre_login.indexOf(":") !== -1) { @@ -809,7 +826,7 @@ window.onload = function() { } }); sock.on('hello', function() { - transition(2); + transition(STATE_ACTIVE); }); sock.on('part', function(u) { userlist.del(u.user); @@ -832,11 +849,11 @@ window.onload = function() { } catch (ex) {} }); sock.on('close', function() { - transition(0); + transition(STATE_DISCONNECTED); write("tab-main").system("Connection closed by remote host."); }); sock.on('disconnect', function() { - transition(0); + transition(STATE_DISCONNECTED); write("tab-main").system("Lost connection to the server."); }); sock.on('usercommand', function(data) {