]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/commitdiff
Store offline data in an array rather than in a hash
authorMichal Sojka <michal.sojka@cvut.cz>
Thu, 16 Aug 2018 16:10:01 +0000 (18:10 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Thu, 16 Aug 2018 16:10:48 +0000 (18:10 +0200)
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

index 9c77744f93363bffee035ed773849cf8ecf21c5a..49a1f9301a2bbe48e9e9f88d9e55529f8f8108a0 100644 (file)
                 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);
+                   }
                 }
             }
         }