+ });
+ } else {
+ // live
+ dbOperation((err,db) => {
+ let objectStore = db
+ .transaction("games", "readwrite")
+ .objectStore("games");
+ objectStore.get(gameId).onsuccess = function(event) {
+ // Ignoring error silently: shouldn't happen now. TODO?
+ if (event.target.result) {
+ let game = event.target.result;
+ // Hidden tabs are delayed, to prevent multi-updates:
+ if (obj.moveIdx < game.moves.length) return;
+ Object.keys(obj).forEach(k => {
+ if (k == "move") game.moves.push(obj[k]);
+ else game[k] = obj[k];
+ });
+ objectStore.put(game); //save updated data
+ }
+ };
+ });
+ }
+ },
+
+ // Retrieve all local games (running, completed, imported...)
+ // light: do not retrieve moves or clocks (TODO: this is the only usage)
+ getAll: function(light, callback) {
+ dbOperation((err,db) => {
+ let objectStore = db.transaction("games").objectStore("games");
+ let games = [];
+ objectStore.openCursor().onsuccess = function(event) {
+ let cursor = event.target.result;
+ // if there is still another cursor to go, keep running this code
+ if (cursor) {
+ let g = cursor.value;
+ if (light) {
+ g.movesCount = g.moves.length;
+ delete g.moves;
+ delete g.clocks;
+ delete g.initime;
+ }
+ games.push(g);
+ cursor.continue();
+ } else callback(games);
+ };