]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blobdiff - templates/main.js
Do not allow multiple reconnect timers running simultaneously
[coffee/coffee-flask.git] / templates / main.js
index 7160868d34a5be9e0b2b1152d48632b5273adb46..02c956c33a0dd30b5badb90efdcb2c82f7454bf6 100644 (file)
@@ -20,7 +20,7 @@ function replayOfflineQueue() {
         let queue = JSON.parse(localStorage.getItem("offlineQueue")) || [];
         if (Array.isArray(queue)) {
             queue.forEach(function (entry) {
-                updateRemote(entry.data);
+                updateRemote(entry.data, new Date(entry.time));
             });
             localStorage.removeItem("offlineQueue");
         }
@@ -40,10 +40,8 @@ function updateUI()
         document.getElementById("local").style.display = !offline ? "none" : "block";
         document.getElementById("remote").style.display = offline ? "none" : "block";
 
-        if (offline) {
-            loadRemote();       // Try to contact the server periodically
+        if (offline)
             return;
-        }
 
         if (id_user !== undefined) {
             document.getElementById("nextStep").innerHTML = "Now select a beverage on the coffee machineā€¦";
@@ -59,7 +57,7 @@ function updateUI()
     }
 }
 
-function hiddenUpdateRemote(json) {
+function hiddenUpdateRemote(json, time = new Date()) {
     var msg = JSON.parse(json);
 
     switch(msg.type) {
@@ -71,7 +69,7 @@ function hiddenUpdateRemote(json) {
         case "keys":
             var flavor = getFlavor(msg.key);
             if (flavor !== "") {
-                addCoffee(flavor);
+                addCoffee(flavor, time);
             }
             break;
         case "ajax_failure":
@@ -92,6 +90,10 @@ function loadRemote(string) {
                 updateUI();
                 clearTimeout(reloadTimer);
             } else {
+                // Cancel current timer for the case when loadRemote()
+                // was called multiple times (e.g. multiple ajax()
+                // calls failed simultaneously).
+                clearTimeout(reloadTimer);
                 reloadTimer = setTimeout(loadRemote, 1000);
             }
         }
@@ -103,7 +105,6 @@ function loadRemote(string) {
 loadRemote();
 
 function ajax(method, route, data, id) {
-    var now = Date.now();
     var xhr = new XMLHttpRequest();
     xhr.onreadystatechange = function() {
         if (this.readyState == 4) {
@@ -113,6 +114,7 @@ function ajax(method, route, data, id) {
             } else {
                 updateRemote = undefined;
                 updateUI();
+                loadRemote(); // Try to contact the server periodically
 
                 if (localStorage) {
                     var ajax_failure = JSON.stringify({
@@ -184,9 +186,9 @@ function getFlavor(letter) {
     }
 }
 
-function addCoffee(flavor) {
+function addCoffee(flavor, time = new Date()) {
     var data = JSON.stringify({
-        time: new Date().toISOString(),
+        time: time.toISOString(),
         flavor: flavor,
         uid: id_user
     });