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.
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);
+ }
}
}
}