From: Michal Sojka Date: Thu, 16 Aug 2018 16:10:01 +0000 (+0200) Subject: Store offline data in an array rather than in a hash X-Git-Url: https://rtime.felk.cvut.cz/gitweb/coffee/mt-apps.git/commitdiff_plain/985405cd4345315d0502fe9e75ef879ad0c452b3 Store offline data in an array rather than in a hash This has several reasons: 1) The hash (i.e. localStorage) used timestamps as keys. When doing automated testing, multiple events can happen to have the same key, which causes problems - see the next point. 2) Events from localStorage has to be sent to the web server in the order of their creation. This means that before sending, we had to convert the hash into an array and sort it according to the timestamp. This can be avoided if we use an array from the beginning. --- diff --git a/html/index.html b/html/index.html index 9c77744..49a1f93 100644 --- a/html/index.html +++ b/html/index.html @@ -43,9 +43,14 @@ updateRemote(msg.data); } else { if (localStorage) { - var now = Date.now(); - localStorage.setItem(now, msg.data); - console.log(now + ": " + msg.data); + let queue = JSON.parse(localStorage.getItem("offlineQueue")) || []; + queue.push({ time: Date.now(), data: msg.data }); + try { + localStorage.setItem("offlineQueue", JSON.stringify(queue)); + } + catch (err) { + console.log(err); + } } } }