a8bcaa2a7f02b6004c21c4d0bc95574b575ab142
1 // (Comp)Game object: {
2 // // Static informations:
3 // vname: string (this is the ID)
6 // // Game (dynamic) state:
8 // moves: array of Move objects,
11 import { store
} from "@/store";
13 function dbOperation(callback
) {
15 let DBOpenRequest
= window
.indexedDB
.open("vchess_comp", 4);
17 DBOpenRequest
.onerror = function(event
) {
18 alert(store
.state
.tr
["Database error: stop private browsing, or update your browser"]);
19 callback("error", null);
22 DBOpenRequest
.onsuccess = function(event
) {
23 db
= DBOpenRequest
.result
;
28 DBOpenRequest
.onupgradeneeded = function(event
) {
29 let db
= event
.target
.result
;
30 let upgradeTransaction
= event
.target
.transaction
;
31 if (!db
.objectStoreNames
.contains("compgames"))
32 db
.createObjectStore("compgames", { keyPath: "vname" });
34 upgradeTransaction
.objectStore("compgames");
38 export const CompgameStorage
= {
40 dbOperation((err
,db
) => {
43 .transaction("compgames", "readwrite")
44 .objectStore("compgames");
45 objectStore
.add(game
);
49 // obj: move and/or fen
50 update: function(gameId
, obj
) {
51 dbOperation((err
,db
) => {
53 .transaction("compgames", "readwrite")
54 .objectStore("compgames");
55 objectStore
.get(gameId
).onsuccess = function(event
) {
56 // Ignoring error silently: shouldn't happen now. TODO?
57 if (event
.target
.result
) {
58 const game
= event
.target
.result
;
59 Object
.keys(obj
).forEach(k
=> {
60 if (k
== "move") game
.moves
.push(obj
[k
]);
61 else game
[k
] = obj
[k
];
63 objectStore
.put(game
); //save updated data
69 // Retrieve any game from its identifier (variant name)
70 // NOTE: need callback because result is obtained asynchronously
71 get: function(gameId
, callback
) {
72 dbOperation((err
,db
) => {
74 .transaction("compgames", "readonly")
75 .objectStore("compgames");
76 objectStore
.get(gameId
).onsuccess = function(event
) {
77 callback(event
.target
.result
);
82 // Delete a game in indexedDB
83 remove: function(gameId
) {
84 dbOperation((err
,db
) => {
86 db
.transaction("compgames", "readwrite")
87 .objectStore("compgames")