Mark rules as read also when visiting /variants/Somevariant page
[vchess.git] / client / src / utils / importgameStorage.js
1 // Game object struct: see gameStorgae.js
2
3 import { store } from "@/store";
4
5 function dbOperation(callback) {
6 let db = null;
7 let DBOpenRequest = window.indexedDB.open("vchess_import", 4);
8
9 DBOpenRequest.onerror = function(event) {
10 alert(store.state.tr[
11 "Database error: stop private browsing, or update your browser"]);
12 callback("error", null);
13 };
14
15 DBOpenRequest.onsuccess = function() {
16 db = DBOpenRequest.result;
17 callback(null, db);
18 db.close();
19 };
20
21 DBOpenRequest.onupgradeneeded = function(event) {
22 let db = event.target.result;
23 let upgradeTransaction = event.target.transaction;
24 let objectStore = undefined;
25 if (!db.objectStoreNames.contains("importgames"))
26 objectStore = db.createObjectStore("importgames", { keyPath: "id" });
27 else
28 objectStore = upgradeTransaction.objectStore("importgames");
29 if (!objectStore.indexNames.contains("created"))
30 // To search by date intervals. Two games could start at the same time
31 objectStore.createIndex("created", "created", { unique: false });
32 };
33 }
34
35 export const ImportgameStorage = {
36 // Optional callback to get error status
37 add: function(game, callback) {
38 dbOperation((err, db) => {
39 if (!!err) {
40 callback("error");
41 return;
42 }
43 let transaction = db.transaction("importgames", "readwrite");
44 transaction.oncomplete = function() {
45 // Everything's fine
46 callback();
47 };
48 transaction.onerror = function(err) {
49 // Duplicate key error (most likely)
50 callback(err.target.error);
51 };
52 transaction.objectStore("importgames").add(game);
53 });
54 },
55
56 // Retrieve next imported games
57 getNext: function(upperDt, callback) {
58 dbOperation((err, db) => {
59 let objectStore = db
60 .transaction("importgames", "readonly")
61 .objectStore("importgames");
62 let index = objectStore.index("created");
63 const range = IDBKeyRange.upperBound(upperDt);
64 let games = [];
65 index.openCursor(range).onsuccess = function(event) {
66 let cursor = event.target.result;
67 if (!cursor) {
68 // Most recent games first:
69 games = games.sort((g1, g2) => g2.created - g1.created);
70 // TODO: 20 games showed per request is arbitrary
71 callback(games.slice(0, 20));
72 }
73 else {
74 // If there is still another cursor to go, keep running this code
75 let g = cursor.value;
76 // Do not retrieve moves or clocks (unused in list mode)
77 g.movesCount = g.moves.length;
78 delete g.moves;
79 delete g.clocks;
80 delete g.initime;
81 games.push(g);
82 cursor.continue();
83 }
84 };
85 });
86 },
87
88 // Retrieve any game from its identifier.
89 // NOTE: need callback because result is obtained asynchronously
90 get: function(gameId, callback) {
91 dbOperation((err, db) => {
92 let objectStore =
93 db.transaction("importgames").objectStore("importgames");
94 objectStore.get(gameId).onsuccess = function(event) {
95 // event.target.result is null if game not found
96 callback(event.target.result);
97 };
98 });
99 },
100
101 // Delete a game in indexedDB
102 remove: function(gameId, callback) {
103 dbOperation((err, db) => {
104 if (!err) {
105 let transaction = db.transaction("importgames", "readwrite");
106 transaction.oncomplete = function() {
107 callback(); //everything's fine
108 };
109 transaction.objectStore("importgames").delete(gameId);
110 }
111 });
112 }
113 };