From 985405cd4345315d0502fe9e75ef879ad0c452b3 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Thu, 16 Aug 2018 18:10:01 +0200 Subject: [PATCH] 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. --- html/index.html | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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); + } } } } -- 2.39.2