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");
}
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ā¦";
}
}
-function hiddenUpdateRemote(json) {
+function hiddenUpdateRemote(json, time = new Date()) {
var msg = JSON.parse(json);
switch(msg.type) {
case "keys":
var flavor = getFlavor(msg.key);
if (flavor !== "") {
- addCoffee(flavor);
+ addCoffee(flavor, time);
}
break;
- case "fuck":
+ case "ajax_failure":
ajax(msg.method, msg.route, msg.data, msg.id);
break;
}
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);
}
}
loadRemote();
function ajax(method, route, data, id) {
- var now = Date.now();
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
} else {
updateRemote = undefined;
updateUI();
+ loadRemote(); // Try to contact the server periodically
if (localStorage) {
- var fuck = JSON.stringify({
- type: "fuck",
+ var ajax_failure = JSON.stringify({
+ type: "ajax_failure",
method: method,
route: route,
data: data,
id: id
});
let queue = JSON.parse(localStorage.getItem("offlineQueue")) || [];
- queue.push({ time: Date.now(), data: fuck });
+ queue.push({ time: Date.now(), data: ajax_failure });
try {
localStorage.setItem("offlineQueue", JSON.stringify(queue));
}
function login(id) {
ajax("POST", "login", id, "user");
id_user = id;
- clearTimeout(logoutTimer);
- timeToLogout = undefined;
+ countingTimeLogout(120);
}
function logout() {
function countingTimeLogout(count_time)
{
+ clearTimeout(logoutTimer);
timeToLogout = count_time;
updateUI();
if (count_time == 0) {
}
}
-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
});